Skip to content

Commit 5cf0bcd

Browse files
committed
All variation method names.
1 parent ab55f09 commit 5cf0bcd

File tree

8 files changed

+82
-56
lines changed

8 files changed

+82
-56
lines changed

libs/server-sdk/include/launchdarkly/server_side/config/config_builder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ConfigBuilder {
7979

8080
/**
8181
* Adds a hook to the SDK configuration.
82-
* Hooks are executed in the order they are added.
82+
*
8383
* @param hook A shared pointer to a hook implementation.
8484
* @return Reference to this.
8585
*/

libs/server-sdk/include/launchdarkly/server_side/hooks/hook.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ namespace launchdarkly::server_side::hooks {
1717
/**
1818
* HookContext allows passing arbitrary data from the caller through to hooks.
1919
*
20-
* This is particularly useful for async frameworks where thread-local storage
21-
* doesn't work, such as passing OpenTelemetry span parents or other contextual
22-
* information that hooks need to access.
23-
*
2420
* Example use case:
2521
* @code
2622
* // Caller creates context with span parent

libs/server-sdk/src/client_impl.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ auto const kAsioConcurrencyHint = 1;
3737
auto const kDataSourceShutdownWait = std::chrono::milliseconds(100);
3838

3939
// Hook method names
40-
static const std::string kMethodVariation = "Variation";
41-
static const std::string kMethodVariationDetail = "VariationDetail";
40+
// Method names for hooks
41+
static const std::string kMethodBoolVariation = "BoolVariation";
42+
static const std::string kMethodBoolVariationDetail = "BoolVariationDetail";
43+
static const std::string kMethodStringVariation = "StringVariation";
44+
static const std::string kMethodStringVariationDetail = "StringVariationDetail";
45+
static const std::string kMethodDoubleVariation = "DoubleVariation";
46+
static const std::string kMethodDoubleVariationDetail = "DoubleVariationDetail";
47+
static const std::string kMethodIntVariation = "IntVariation";
48+
static const std::string kMethodIntVariationDetail = "IntVariationDetail";
49+
static const std::string kMethodJsonVariation = "JsonVariation";
50+
static const std::string kMethodJsonVariationDetail = "JsonVariationDetail";
4251

4352
static std::unique_ptr<data_interfaces::IDataSystem> MakeDataSystem(
4453
config::built::HttpProperties const& http_properties,
@@ -319,9 +328,10 @@ Value ClientImpl::Variation(Context const& ctx,
319328
enum Value::Type value_type,
320329
IClient::FlagKey const& key,
321330
Value const& default_value,
322-
hooks::HookContext const& hook_context) {
331+
hooks::HookContext const& hook_context,
332+
std::string const& method_name) {
323333
auto result = *VariationInternal(ctx, key, default_value, events_default_,
324-
hook_context);
334+
hook_context, method_name);
325335
if (result.Type() != value_type) {
326336
return default_value;
327337
}
@@ -333,17 +343,14 @@ EvaluationDetail<Value> ClientImpl::VariationInternal(
333343
IClient::FlagKey const& key,
334344
Value const& default_value,
335345
EventScope const& event_scope,
336-
hooks::HookContext const& hook_context) {
337-
// Determine method name based on which event scope is being used
338-
std::string const& method = (&event_scope == &events_with_reasons_)
339-
? kMethodVariationDetail
340-
: kMethodVariation;
341-
346+
hooks::HookContext const& hook_context,
347+
std::string const& method_name) {
342348
// Execute beforeEvaluation hooks
343349
std::optional<hooks::EvaluationSeriesExecutor> executor;
344350
if (!config_.Hooks().empty()) {
345351
hooks::EvaluationSeriesContext series_context(
346-
key, context, default_value, method, hook_context, std::nullopt);
352+
key, context, default_value, method_name, hook_context, std::nullopt);
353+
// Executor only created if there are hooks.
347354
executor.emplace(config_.Hooks(), logger_);
348355
executor->BeforeEvaluation(series_context);
349356
}
@@ -355,7 +362,7 @@ EvaluationDetail<Value> ClientImpl::VariationInternal(
355362
// Execute afterEvaluation hooks
356363
if (executor) {
357364
hooks::EvaluationSeriesContext series_context(
358-
key, context, default_value, method, hook_context, std::nullopt);
365+
key, context, default_value, method_name, hook_context, std::nullopt);
359366
executor->AfterEvaluation(series_context, detail);
360367
}
361368

@@ -376,7 +383,7 @@ EvaluationDetail<Value> ClientImpl::VariationInternal(
376383
// Execute afterEvaluation hooks
377384
if (executor) {
378385
hooks::EvaluationSeriesContext series_context(
379-
key, context, default_value, method, hook_context, std::nullopt);
386+
key, context, default_value, method_name, hook_context, std::nullopt);
380387
executor->AfterEvaluation(series_context, detail);
381388
}
382389

@@ -391,7 +398,7 @@ EvaluationDetail<Value> ClientImpl::VariationInternal(
391398
// Execute afterEvaluation hooks
392399
if (executor) {
393400
hooks::EvaluationSeriesContext series_context(
394-
key, context, default_value, method, hook_context, std::nullopt);
401+
key, context, default_value, method_name, hook_context, std::nullopt);
395402
executor->AfterEvaluation(series_context, detail);
396403
}
397404

@@ -459,7 +466,7 @@ EvaluationDetail<bool> ClientImpl::BoolVariationDetail(
459466
bool default_value) {
460467
static hooks::HookContext empty_hook_context;
461468
return VariationDetail<bool>(ctx, Value::Type::kBool, key, default_value,
462-
empty_hook_context);
469+
empty_hook_context, kMethodBoolVariationDetail);
463470
}
464471

465472
EvaluationDetail<bool> ClientImpl::BoolVariationDetail(
@@ -468,23 +475,23 @@ EvaluationDetail<bool> ClientImpl::BoolVariationDetail(
468475
bool default_value,
469476
hooks::HookContext const& hook_context) {
470477
return VariationDetail<bool>(ctx, Value::Type::kBool, key, default_value,
471-
hook_context);
478+
hook_context, kMethodBoolVariationDetail);
472479
}
473480

474481
bool ClientImpl::BoolVariation(Context const& ctx,
475482
IClient::FlagKey const& key,
476483
bool default_value) {
477484
static hooks::HookContext empty_hook_context;
478485
return Variation(ctx, Value::Type::kBool, key, default_value,
479-
empty_hook_context);
486+
empty_hook_context, kMethodBoolVariation);
480487
}
481488

482489
bool ClientImpl::BoolVariation(Context const& ctx,
483490
IClient::FlagKey const& key,
484491
bool default_value,
485492
hooks::HookContext const& hook_context) {
486493
return Variation(ctx, Value::Type::kBool, key, default_value,
487-
hook_context);
494+
hook_context, kMethodBoolVariation);
488495
}
489496

490497
EvaluationDetail<std::string> ClientImpl::StringVariationDetail(
@@ -493,7 +500,8 @@ EvaluationDetail<std::string> ClientImpl::StringVariationDetail(
493500
std::string default_value) {
494501
static hooks::HookContext empty_hook_context;
495502
return VariationDetail<std::string>(ctx, Value::Type::kString, key,
496-
default_value, empty_hook_context);
503+
default_value, empty_hook_context,
504+
kMethodStringVariationDetail);
497505
}
498506

499507
EvaluationDetail<std::string> ClientImpl::StringVariationDetail(
@@ -502,23 +510,24 @@ EvaluationDetail<std::string> ClientImpl::StringVariationDetail(
502510
std::string default_value,
503511
hooks::HookContext const& hook_context) {
504512
return VariationDetail<std::string>(ctx, Value::Type::kString, key,
505-
default_value, hook_context);
513+
default_value, hook_context,
514+
kMethodStringVariationDetail);
506515
}
507516

508517
std::string ClientImpl::StringVariation(Context const& ctx,
509518
IClient::FlagKey const& key,
510519
std::string default_value) {
511520
static hooks::HookContext empty_hook_context;
512521
return Variation(ctx, Value::Type::kString, key, default_value,
513-
empty_hook_context);
522+
empty_hook_context, kMethodStringVariation);
514523
}
515524

516525
std::string ClientImpl::StringVariation(Context const& ctx,
517526
IClient::FlagKey const& key,
518527
std::string default_value,
519528
hooks::HookContext const& hook_context) {
520529
return Variation(ctx, Value::Type::kString, key, default_value,
521-
hook_context);
530+
hook_context, kMethodStringVariation);
522531
}
523532

524533
EvaluationDetail<double> ClientImpl::DoubleVariationDetail(
@@ -527,7 +536,8 @@ EvaluationDetail<double> ClientImpl::DoubleVariationDetail(
527536
double default_value) {
528537
static hooks::HookContext empty_hook_context;
529538
return VariationDetail<double>(ctx, Value::Type::kNumber, key,
530-
default_value, empty_hook_context);
539+
default_value, empty_hook_context,
540+
kMethodDoubleVariationDetail);
531541
}
532542

533543
EvaluationDetail<double> ClientImpl::DoubleVariationDetail(
@@ -536,23 +546,24 @@ EvaluationDetail<double> ClientImpl::DoubleVariationDetail(
536546
double default_value,
537547
hooks::HookContext const& hook_context) {
538548
return VariationDetail<double>(ctx, Value::Type::kNumber, key,
539-
default_value, hook_context);
549+
default_value, hook_context,
550+
kMethodDoubleVariationDetail);
540551
}
541552

542553
double ClientImpl::DoubleVariation(Context const& ctx,
543554
IClient::FlagKey const& key,
544555
double default_value) {
545556
static hooks::HookContext empty_hook_context;
546557
return Variation(ctx, Value::Type::kNumber, key, default_value,
547-
empty_hook_context);
558+
empty_hook_context, kMethodDoubleVariation);
548559
}
549560

550561
double ClientImpl::DoubleVariation(Context const& ctx,
551562
IClient::FlagKey const& key,
552563
double default_value,
553564
hooks::HookContext const& hook_context) {
554565
return Variation(ctx, Value::Type::kNumber, key, default_value,
555-
hook_context);
566+
hook_context, kMethodDoubleVariation);
556567
}
557568

558569
EvaluationDetail<int> ClientImpl::IntVariationDetail(
@@ -561,7 +572,7 @@ EvaluationDetail<int> ClientImpl::IntVariationDetail(
561572
int default_value) {
562573
static hooks::HookContext empty_hook_context;
563574
return VariationDetail<int>(ctx, Value::Type::kNumber, key, default_value,
564-
empty_hook_context);
575+
empty_hook_context, kMethodIntVariationDetail);
565576
}
566577

567578
EvaluationDetail<int> ClientImpl::IntVariationDetail(
@@ -570,23 +581,23 @@ EvaluationDetail<int> ClientImpl::IntVariationDetail(
570581
int default_value,
571582
hooks::HookContext const& hook_context) {
572583
return VariationDetail<int>(ctx, Value::Type::kNumber, key, default_value,
573-
hook_context);
584+
hook_context, kMethodIntVariationDetail);
574585
}
575586

576587
int ClientImpl::IntVariation(Context const& ctx,
577588
IClient::FlagKey const& key,
578589
int default_value) {
579590
static hooks::HookContext empty_hook_context;
580591
return Variation(ctx, Value::Type::kNumber, key, default_value,
581-
empty_hook_context);
592+
empty_hook_context, kMethodIntVariation);
582593
}
583594

584595
int ClientImpl::IntVariation(Context const& ctx,
585596
IClient::FlagKey const& key,
586597
int default_value,
587598
hooks::HookContext const& hook_context) {
588599
return Variation(ctx, Value::Type::kNumber, key, default_value,
589-
hook_context);
600+
hook_context, kMethodIntVariation);
590601
}
591602

592603
EvaluationDetail<Value> ClientImpl::JsonVariationDetail(
@@ -595,7 +606,7 @@ EvaluationDetail<Value> ClientImpl::JsonVariationDetail(
595606
Value default_value) {
596607
static hooks::HookContext empty_hook_context;
597608
return VariationInternal(ctx, key, default_value, events_with_reasons_,
598-
empty_hook_context);
609+
empty_hook_context, kMethodJsonVariationDetail);
599610
}
600611

601612
EvaluationDetail<Value> ClientImpl::JsonVariationDetail(
@@ -604,23 +615,23 @@ EvaluationDetail<Value> ClientImpl::JsonVariationDetail(
604615
Value default_value,
605616
hooks::HookContext const& hook_context) {
606617
return VariationInternal(ctx, key, default_value, events_with_reasons_,
607-
hook_context);
618+
hook_context, kMethodJsonVariationDetail);
608619
}
609620

610621
Value ClientImpl::JsonVariation(Context const& ctx,
611622
IClient::FlagKey const& key,
612623
Value default_value) {
613624
static hooks::HookContext empty_hook_context;
614625
return *VariationInternal(ctx, key, default_value, events_default_,
615-
empty_hook_context);
626+
empty_hook_context, kMethodJsonVariation);
616627
}
617628

618629
Value ClientImpl::JsonVariation(Context const& ctx,
619630
IClient::FlagKey const& key,
620631
Value default_value,
621632
hooks::HookContext const& hook_context) {
622633
return *VariationInternal(ctx, key, default_value, events_default_,
623-
hook_context);
634+
hook_context, kMethodJsonVariation);
624635
}
625636

626637
IDataSourceStatusProvider& ClientImpl::DataSourceStatus() {

libs/server-sdk/src/client_impl.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,20 @@ class ClientImpl : public IClient {
184184
FlagKey const& key,
185185
Value const& default_value,
186186
EventScope const& scope,
187-
hooks::HookContext const& hook_context);
187+
hooks::HookContext const& hook_context,
188+
std::string const& method_name);
188189

189190
template <typename T>
190191
[[nodiscard]] EvaluationDetail<T> VariationDetail(
191192
Context const& ctx,
192193
enum Value::Type value_type,
193194
IClient::FlagKey const& key,
194195
Value const& default_value,
195-
hooks::HookContext const& hook_context) {
196+
hooks::HookContext const& hook_context,
197+
std::string const& method_name) {
196198
auto result =
197199
VariationInternal(ctx, key, default_value, events_with_reasons_,
198-
hook_context);
200+
hook_context, method_name);
199201
if (result.Value().Type() == value_type) {
200202
return EvaluationDetail<T>{result.Value(), result.VariationIndex(),
201203
result.Reason()};
@@ -208,7 +210,8 @@ class ClientImpl : public IClient {
208210
enum Value::Type value_type,
209211
std::string const& key,
210212
Value const& default_value,
211-
hooks::HookContext const& hook_context);
213+
hooks::HookContext const& hook_context,
214+
std::string const& method_name);
212215

213216
[[nodiscard]] EvaluationDetail<Value> PostEvaluation(
214217
std::string const& key,

libs/server-sdk/src/hooks/hook.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ HookContext& HookContext::Set(std::string key,
1414

1515
std::optional<std::shared_ptr<std::any>> HookContext::Get(
1616
std::string const& key) const {
17-
auto it = data_.find(key);
18-
if (it != data_.end()) {
17+
if (const auto it = data_.find(key); it != data_.end()) {
1918
return it->second;
2019
}
2120
return std::nullopt;
@@ -51,8 +50,7 @@ std::optional<Value> EvaluationSeriesData::Get(std::string const& key) const {
5150

5251
std::optional<std::shared_ptr<std::any>> EvaluationSeriesData::GetShared(
5352
std::string const& key) const {
54-
auto it = data_.find(key);
55-
if (it != data_.end() && it->second.shared) {
53+
if (const auto it = data_.find(key); it != data_.end() && it->second.shared) {
5654
return it->second.shared;
5755
}
5856
return std::nullopt;

libs/server-sdk/src/hooks/hook_executor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void EvaluationSeriesExecutor::LogHookError(
5858
std::string const& stage,
5959
std::string const& hook_name,
6060
std::string const& flag_name,
61-
std::string const& error_message) {
61+
std::string const& error_message) const {
6262
LD_LOG(logger_, LogLevel::kError)
6363
<< "[hooks] During evaluation of flag \"" << flag_name << "\", stage \""
6464
<< stage << "\" of hook \"" << hook_name
@@ -67,7 +67,7 @@ void EvaluationSeriesExecutor::LogHookError(
6767

6868
void ExecuteAfterTrack(std::vector<std::shared_ptr<Hook>> const& hooks,
6969
TrackSeriesContext const& context,
70-
Logger& logger) {
70+
const Logger& logger) {
7171
// Execute hooks in order of registration
7272
for (auto const& hook : hooks) {
7373
try {

libs/server-sdk/src/hooks/hook_executor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ class EvaluationSeriesExecutor {
5454
void LogHookError(std::string const& stage,
5555
std::string const& hook_name,
5656
std::string const& flag_name,
57-
std::string const& error_message);
57+
std::string const& error_message) const;
5858
};
5959

6060
/**
6161
* Utility for executing track hooks.
6262
*/
6363
void ExecuteAfterTrack(std::vector<std::shared_ptr<Hook>> const& hooks,
6464
TrackSeriesContext const& context,
65-
Logger& logger);
65+
const Logger& logger);
6666

6767
} // namespace launchdarkly::server_side::hooks

0 commit comments

Comments
 (0)