@@ -62,18 +62,13 @@ struct ConfigMetadata {
6262 : name(n), value(std::move(v)), origin(orig), error(std::move(err)) {}
6363};
6464
65- // Return the chosen configuration value from one of the specified `from_env`,
66- // `from_user`, and `fallback`. This function defines the relative precedence
67- // among configuration values, from lower to higher, originating from the
68- // default configuration, programmatic configuration, and environment.
69- //
65+ // Return the chosen configuration value, in order of precedence: `from_env`,
66+ // `from_user`, and `fallback`.
7067// This overload directly populates both telemetry_configs[config_name] and
71- // metadata[config_name] using the stringified value, avoiding duplicate
72- // string computations. Both telemetry_configs and config_name are required.
73- // Returns the chosen value directly (no origin needed since it's in metadata).
68+ // metadata[config_name] using the stringified value, with all values found, from lowest to highest precedence.
69+ // Returns the chosen value directly.
7470//
75- // The fallback parameter accepts both raw values (e.g., true, 42, "hello")
76- // and Optional<Value>. It defaults to nullptr (converted to nullopt internally).
71+ // The fallback parameter is optional and defaults to nullptr.
7772template <typename Value, typename Stringifier = std::nullptr_t , typename DefaultValue = std::nullptr_t >
7873Value pick (
7974 const Optional<Value>& from_env, const Optional<Value>& from_user,
@@ -104,10 +99,9 @@ Value pick(
10499 chosen_value = val;
105100 };
106101
107- // Add DEFAULT entry if fallback was provided (known at compile time )
102+ // Add DEFAULT entry if fallback was provided (detected by type )
108103 if constexpr (!std::is_same_v<DefaultValue, std::nullptr_t >) {
109- Value fallback_value = fallback; // Implicit conversion for raw values
110- add_entry (ConfigMetadata::Origin::DEFAULT, fallback_value);
104+ add_entry (ConfigMetadata::Origin::DEFAULT, fallback);
111105 }
112106
113107 if (from_user) {
@@ -119,33 +113,30 @@ Value pick(
119113 }
120114
121115 (*telemetry_configs)[config_name] = std::move (telemetry_entries);
122- (*metadata)[config_name] = (*telemetry_configs)[config_name].back ();
116+ if (!telemetry_entries.empty ()) {
117+ (*metadata)[config_name] = (*telemetry_configs)[config_name].back ();
118+ }
123119
124120 return chosen_value.value_or (Value{});
125121 }
126-
127- // Overload without telemetry - returns a pair for backward compatibility
128- // with std::tie usage pattern (used in telemetry/configuration.cpp).
129- template <typename Value, typename DefaultValue = Value>
130- std::pair<ConfigMetadata::Origin, Value> pick (
131- const Optional<Value>& from_env,
132- const Optional<Value>& from_user,
133- DefaultValue fallback = DefaultValue{}) {
134-
135- ConfigMetadata::Origin chosen_origin = ConfigMetadata::Origin::DEFAULT;
136- Value chosen_value = Value (fallback);
137-
138- if (from_user) {
139- chosen_origin = ConfigMetadata::Origin::CODE;
140- chosen_value = *from_user;
141- }
142-
143- if (from_env) {
144- chosen_origin = ConfigMetadata::Origin::ENVIRONMENT_VARIABLE;
145- chosen_value = *from_env;
146- }
147122
148- return {chosen_origin, chosen_value};
123+
124+ // Return a pair containing the configuration origin and value of a
125+ // configuration value chosen from one of the specified `from_env`,
126+ // `from_config`, and `fallback`. This function defines the relative precedence
127+ // among configuration values originating from the environment, programmatic
128+ // configuration, and default configuration.
129+ template <typename Value, typename DefaultValue>
130+ std::pair<ConfigMetadata::Origin, Value> pick (const Optional<Value> &from_env,
131+ const Optional<Value> &from_user,
132+ DefaultValue fallback) {
133+ if (from_env) {
134+ return {ConfigMetadata::Origin::ENVIRONMENT_VARIABLE, *from_env};
135+ } else if (from_user) {
136+ return {ConfigMetadata::Origin::CODE, *from_user};
149137 }
138+ return {ConfigMetadata::Origin::DEFAULT, fallback};
139+ }
140+
150141} // namespace tracing
151142} // namespace datadog
0 commit comments