Skip to content

Commit ad685c8

Browse files
committed
4
1 parent 7934833 commit ad685c8

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

include/datadog/config.h

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7772
template <typename Value, typename Stringifier = std::nullptr_t, typename DefaultValue = std::nullptr_t>
7873
Value 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

src/datadog/tracer_config.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig& user_config,
290290
final_config.defaults.service =
291291
pick(env_config->service, user_config.service,
292292
&telemetry_configs, &final_config.metadata, ConfigName::SERVICE_NAME,
293-
Optional<std::string>(get_process_name()));
293+
get_process_name());
294294

295295
// Service type
296296
final_config.defaults.service_type =
@@ -387,14 +387,14 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig& user_config,
387387
pick(env_config->baggage_max_items, user_config.baggage_max_items,
388388
&telemetry_configs, &final_config.metadata,
389389
ConfigName::TRACE_BAGGAGE_MAX_ITEMS,
390-
64, [](const int& i) { return std::to_string(i); });
390+
64UL, [](const int& i) { return std::to_string(i); });
391391

392392
// Baggage - max bytes
393393
final_config.baggage_opts.max_bytes =
394394
pick(env_config->baggage_max_bytes, user_config.baggage_max_bytes,
395395
&telemetry_configs, &final_config.metadata,
396396
ConfigName::TRACE_BAGGAGE_MAX_BYTES,
397-
8192, [](const int& i) { return std::to_string(i); });
397+
8192UL, [](const int& i) { return std::to_string(i); });
398398

399399
if (final_config.baggage_opts.max_items <= 0 ||
400400
final_config.baggage_opts.max_bytes < 3) {

0 commit comments

Comments
 (0)