Skip to content

Commit be38941

Browse files
committed
Avoid env mutation in telemetry test
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
1 parent a042952 commit be38941

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

rewatch/src/telemetry.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ impl Drop for TelemetryGuard {
5151
/// tracing calls become no-ops.
5252
pub fn init_telemetry() -> TelemetryGuard {
5353
// Presence of either the general or trace-specific endpoint env var enables OTLP export.
54-
let enabled = std::env::var_os("OTEL_EXPORTER_OTLP_ENDPOINT").is_some()
55-
|| std::env::var_os("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT").is_some();
54+
init_telemetry_for_endpoint_presence(
55+
std::env::var_os("OTEL_EXPORTER_OTLP_ENDPOINT").is_some(),
56+
std::env::var_os("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT").is_some(),
57+
)
58+
}
5659

57-
if enabled { init_with_otlp() } else { init_noop() }
60+
fn init_telemetry_for_endpoint_presence(endpoint_set: bool, traces_endpoint_set: bool) -> TelemetryGuard {
61+
if endpoint_set || traces_endpoint_set {
62+
init_with_otlp()
63+
} else {
64+
init_noop()
65+
}
5866
}
5967

6068
/// Initialize with OTLP exporter.
@@ -146,16 +154,7 @@ mod tests {
146154

147155
#[test]
148156
fn init_telemetry_without_env_is_noop() {
149-
// SAFETY: These env vars are process-global; tests run serially in a
150-
// single thread by default for this crate (no #[test(flavor = ...)]).
151-
// If parallelised later, wrap in a Mutex.
152-
// Ensure the env is clean for this check.
153-
unsafe {
154-
std::env::remove_var("OTEL_EXPORTER_OTLP_ENDPOINT");
155-
std::env::remove_var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT");
156-
}
157-
158-
let guard = init_telemetry();
157+
let guard = init_telemetry_for_endpoint_presence(false, false);
159158
assert!(
160159
!guard.otel_enabled(),
161160
"expected no-op guard when OTEL endpoint env vars are unset"

0 commit comments

Comments
 (0)