Skip to content

Commit a7bb668

Browse files
authored
Telemetry: late-binding for service name (#11392)
`TelemetryArgs` can now be instantiated without a service name, but initialization won't work until one is specified. This allows end-users to specify a service name at runtime without having to use `set_var`.
1 parent 553a767 commit a7bb668

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

crates/utils/re_perf_telemetry/src/args.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,19 @@ pub struct TelemetryArgs {
6666
/// feature flags instead: <https://docs.rs/tracing/0.1.41/tracing/level_filters/index.html>.
6767
#[cfg_attr(
6868
feature = "enabled",
69-
clap(long, env = "TELEMETRY_ENABLED", default_value_t = true)
69+
clap(
70+
long = "telemetry-enabled",
71+
env = "TELEMETRY_ENABLED",
72+
default_value_t = true
73+
)
7074
)]
7175
#[cfg_attr(
7276
not(feature = "enabled"),
73-
clap(long, env = "TELEMETRY_ENABLED", default_value_t = false)
77+
clap(
78+
long = "telemetry-enabled",
79+
env = "TELEMETRY_ENABLED",
80+
default_value_t = false
81+
)
7482
)]
7583
pub enabled: bool,
7684

@@ -113,9 +121,12 @@ pub struct TelemetryArgs {
113121

114122
/// The service name used for all things telemetry.
115123
///
124+
/// This is mandatory, but we leave it as optional to give users a chance to set it at initialization
125+
/// time (as opposed to e.g. via env configuration) if needed.
126+
///
116127
/// Part of the `OpenTelemetry` spec.
117128
#[clap(long, env = "OTEL_SERVICE_NAME")]
118-
pub service_name: String,
129+
pub service_name: Option<String>,
119130

120131
/// The service attributes used for all things telemetry.
121132
///

crates/utils/re_perf_telemetry/src/telemetry.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ impl Telemetry {
169169
});
170170
}
171171

172+
let Some(service_name) = service_name else {
173+
anyhow::bail!(
174+
"either `OTEL_SERVICE_NAME` or `TelemetryArgs::service_name` must be set in order to initialize telemetry"
175+
);
176+
};
177+
172178
// For these things, all we need to do is make sure that the right OTEL env var is set.
173179
// All the downstream libraries will do the right thing if they are.
174180
//

0 commit comments

Comments
 (0)