Skip to content

Commit f2d57b1

Browse files
authored
fix(sdk, zipkin, otlp): use temp_env for testing (#1403)
Fixes #1383
1 parent 075d26c commit f2d57b1

File tree

10 files changed

+209
-171
lines changed

10 files changed

+209
-171
lines changed

opentelemetry-otlp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ opentelemetry_sdk = { features = ["trace", "rt-tokio", "testing"], path = "../op
5252
time = { version = "0.3", features = ["macros"] }
5353
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
5454
futures-util = { version = "0.3", default-features = false }
55+
temp-env = "0.3.6"
5556

5657
[features]
5758
# telemetry pillars and functions

opentelemetry-otlp/src/exporter/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,20 @@ fn parse_header_key_value_string(key_value_string: &str) -> Option<(&str, &str)>
243243
#[cfg(test)]
244244
#[cfg(any(feature = "grpc-tonic", feature = "http-proto"))]
245245
mod tests {
246-
// Make sure env tests are not running concurrently
247-
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
248246

249247
pub(crate) fn run_env_test<T, F>(env_vars: T, f: F)
250248
where
251249
F: FnOnce(),
252250
T: Into<Vec<(&'static str, &'static str)>>,
253251
{
254-
let _env_lock = ENV_LOCK.lock().expect("env test lock poisoned");
255-
let env_vars = env_vars.into();
256-
for (k, v) in env_vars.iter() {
257-
std::env::set_var(k, v);
258-
}
259-
f();
260-
for (k, _) in env_vars {
261-
std::env::remove_var(k);
262-
}
252+
temp_env::with_vars(
253+
env_vars
254+
.into()
255+
.iter()
256+
.map(|&(k, v)| (k, Some(v)))
257+
.collect::<Vec<(&'static str, Option<&'static str>)>>(),
258+
f,
259+
)
263260
}
264261

265262
#[test]

opentelemetry-sdk/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ rustdoc-args = ["--cfg", "docsrs"]
3838
[dev-dependencies]
3939
indexmap = "2.0"
4040
criterion = { version = "0.5", features = ["html_reports"] }
41+
temp-env = "0.3.6"
42+
4143
[target.'cfg(not(target_os = "windows"))'.dev-dependencies]
4244
pprof = { version = "0.13", features = ["flamegraph", "criterion"] }
4345

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -247,51 +247,54 @@ mod tests {
247247
.build();
248248
assert_service_name(custom_meter_provider, Some("test_service"));
249249

250-
// If `OTEL_RESOURCE_ATTRIBUTES` is set, read them automatically
251-
let reader3 = TestMetricReader {};
252-
env::set_var("OTEL_RESOURCE_ATTRIBUTES", "key1=value1, k2, k3=value2");
253-
let env_resource_provider = super::SdkMeterProvider::builder()
254-
.with_reader(reader3)
255-
.build();
256-
assert_eq!(
257-
env_resource_provider.pipes.0[0].resource,
258-
Resource::new(vec![
259-
KeyValue::new("telemetry.sdk.name", "opentelemetry"),
260-
KeyValue::new("telemetry.sdk.version", env!("CARGO_PKG_VERSION")),
261-
KeyValue::new("telemetry.sdk.language", "rust"),
262-
KeyValue::new("key1", "value1"),
263-
KeyValue::new("k3", "value2"),
264-
KeyValue::new("service.name", "unknown_service"),
265-
])
250+
temp_env::with_var(
251+
"OTEL_RESOURCE_ATTRIBUTES",
252+
Some("key1=value1, k2, k3=value2"),
253+
|| {
254+
// If `OTEL_RESOURCE_ATTRIBUTES` is set, read them automatically
255+
let reader3 = TestMetricReader {};
256+
let env_resource_provider = super::SdkMeterProvider::builder()
257+
.with_reader(reader3)
258+
.build();
259+
assert_eq!(
260+
env_resource_provider.pipes.0[0].resource,
261+
Resource::new(vec![
262+
KeyValue::new("telemetry.sdk.name", "opentelemetry"),
263+
KeyValue::new("telemetry.sdk.version", env!("CARGO_PKG_VERSION")),
264+
KeyValue::new("telemetry.sdk.language", "rust"),
265+
KeyValue::new("key1", "value1"),
266+
KeyValue::new("k3", "value2"),
267+
KeyValue::new("service.name", "unknown_service"),
268+
])
269+
);
270+
},
266271
);
267272

268273
// When `OTEL_RESOURCE_ATTRIBUTES` is set and also user provided config
269-
env::set_var(
274+
temp_env::with_var(
270275
"OTEL_RESOURCE_ATTRIBUTES",
271-
"my-custom-key=env-val,k2=value2",
272-
);
273-
let reader4 = TestMetricReader {};
274-
let user_provided_resource_config_provider = super::SdkMeterProvider::builder()
275-
.with_reader(reader4)
276-
.with_resource(
277-
Resource::default().merge(&mut Resource::new(vec![KeyValue::new(
278-
"my-custom-key",
279-
"my-custom-value",
280-
)])),
281-
)
282-
.build();
283-
assert_eq!(
284-
user_provided_resource_config_provider.pipes.0[0].resource,
285-
Resource::new(vec![
286-
KeyValue::new("telemetry.sdk.name", "opentelemetry"),
287-
KeyValue::new("telemetry.sdk.version", env!("CARGO_PKG_VERSION")),
288-
KeyValue::new("telemetry.sdk.language", "rust"),
289-
KeyValue::new("my-custom-key", "my-custom-value"),
290-
KeyValue::new("k2", "value2"),
291-
KeyValue::new("service.name", "unknown_service"),
292-
])
276+
Some("my-custom-key=env-val,k2=value2"),
277+
|| {
278+
let reader4 = TestMetricReader {};
279+
let user_provided_resource_config_provider = super::SdkMeterProvider::builder()
280+
.with_reader(reader4)
281+
.with_resource(Resource::default().merge(&mut Resource::new(vec![
282+
KeyValue::new("my-custom-key", "my-custom-value"),
283+
])))
284+
.build();
285+
assert_eq!(
286+
user_provided_resource_config_provider.pipes.0[0].resource,
287+
Resource::new(vec![
288+
KeyValue::new("telemetry.sdk.name", "opentelemetry"),
289+
KeyValue::new("telemetry.sdk.version", env!("CARGO_PKG_VERSION")),
290+
KeyValue::new("telemetry.sdk.language", "rust"),
291+
KeyValue::new("my-custom-key", "my-custom-value"),
292+
KeyValue::new("k2", "value2"),
293+
KeyValue::new("service.name", "unknown_service"),
294+
])
295+
);
296+
},
293297
);
294-
env::remove_var("OTEL_RESOURCE_ATTRIBUTES");
295298

296299
// If user provided a resource, it takes priority during collision.
297300
let reader5 = TestMetricReader {};

opentelemetry-sdk/src/resource/env.rs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,68 +99,80 @@ mod tests {
9999
use crate::resource::{EnvResourceDetector, Resource, ResourceDetector};
100100
use opentelemetry::{Key, KeyValue, Value};
101101
use std::time::Duration;
102-
use std::{env, time};
103102

104103
#[test]
105104
fn test_read_from_env() {
106-
env::set_var(OTEL_RESOURCE_ATTRIBUTES, "key=value, k = v , a= x, a=z");
107-
env::set_var("irrelevant".to_uppercase(), "20200810");
108-
109-
let detector = EnvResourceDetector::new();
110-
let resource = detector.detect(time::Duration::from_secs(5));
111-
assert_eq!(
112-
resource,
113-
Resource::new(vec![
114-
KeyValue::new("key", "value"),
115-
KeyValue::new("k", "v"),
116-
KeyValue::new("a", "x"),
117-
KeyValue::new("a", "z"),
118-
])
105+
temp_env::with_vars(
106+
[
107+
(
108+
"OTEL_RESOURCE_ATTRIBUTES",
109+
Some("key=value, k = v , a= x, a=z"),
110+
),
111+
("IRRELEVANT", Some("20200810")),
112+
],
113+
|| {
114+
let detector = EnvResourceDetector::new();
115+
let resource = detector.detect(Duration::from_secs(5));
116+
assert_eq!(
117+
resource,
118+
Resource::new(vec![
119+
KeyValue::new("key", "value"),
120+
KeyValue::new("k", "v"),
121+
KeyValue::new("a", "x"),
122+
KeyValue::new("a", "z"),
123+
])
124+
);
125+
},
119126
);
120127

121-
// Test this case in same test to avoid race condition when running tests in parallel.
122-
env::set_var(OTEL_RESOURCE_ATTRIBUTES, "");
123-
124128
let detector = EnvResourceDetector::new();
125-
let resource = detector.detect(time::Duration::from_secs(5));
129+
let resource = detector.detect(Duration::from_secs(5));
126130
assert!(resource.is_empty());
127131
}
128132

129133
#[test]
130134
fn test_sdk_provided_resource_detector() {
131135
const SERVICE_NAME: &str = "service.name";
132136
// Ensure no env var set
133-
env::remove_var(OTEL_RESOURCE_ATTRIBUTES);
134137
let no_env = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
135138
assert_eq!(
136139
no_env.get(Key::from_static_str(SERVICE_NAME)),
137140
Some(Value::from("unknown_service")),
138141
);
139142

140-
env::set_var(OTEL_SERVICE_NAME, "test service");
141-
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
142-
assert_eq!(
143-
with_service.get(Key::from_static_str(SERVICE_NAME)),
144-
Some(Value::from("test service")),
145-
);
146-
env::set_var(OTEL_SERVICE_NAME, ""); // clear the env var
147-
148-
// Fall back to OTEL_RESOURCE_ATTRIBUTES
149-
env::set_var(OTEL_RESOURCE_ATTRIBUTES, "service.name=test service1");
150-
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
151-
assert_eq!(
152-
with_service.get(Key::from_static_str(SERVICE_NAME)),
153-
Some(Value::from("test service1"))
143+
temp_env::with_var(OTEL_SERVICE_NAME, Some("test service"), || {
144+
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
145+
assert_eq!(
146+
with_service.get(Key::from_static_str(SERVICE_NAME)),
147+
Some(Value::from("test service")),
148+
)
149+
});
150+
151+
temp_env::with_var(
152+
OTEL_RESOURCE_ATTRIBUTES,
153+
Some("service.name=test service1"),
154+
|| {
155+
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
156+
assert_eq!(
157+
with_service.get(Key::from_static_str(SERVICE_NAME)),
158+
Some(Value::from("test service1")),
159+
)
160+
},
154161
);
155162

156163
// OTEL_SERVICE_NAME takes priority
157-
env::set_var(OTEL_SERVICE_NAME, "test service");
158-
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
159-
assert_eq!(
160-
with_service.get(Key::from_static_str(SERVICE_NAME)),
161-
Some(Value::from("test service"))
164+
temp_env::with_vars(
165+
[
166+
(OTEL_SERVICE_NAME, Some("test service")),
167+
(OTEL_RESOURCE_ATTRIBUTES, Some("service.name=test service3")),
168+
],
169+
|| {
170+
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
171+
assert_eq!(
172+
with_service.get(Key::from_static_str(SERVICE_NAME)),
173+
Some(Value::from("test service"))
174+
);
175+
},
162176
);
163-
env::set_var(OTEL_RESOURCE_ATTRIBUTES, "");
164-
env::set_var(OTEL_SERVICE_NAME, ""); // clear the env var
165177
}
166178
}

opentelemetry-sdk/src/resource/mod.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ mod tests {
255255
use super::*;
256256
use crate::resource::EnvResourceDetector;
257257
use std::collections::HashMap;
258-
use std::{env, time};
258+
use std::time;
259259

260260
#[test]
261261
fn new_resource() {
@@ -339,20 +339,30 @@ mod tests {
339339

340340
#[test]
341341
fn detect_resource() {
342-
env::set_var("OTEL_RESOURCE_ATTRIBUTES", "key=value, k = v , a= x, a=z");
343-
env::set_var("irrelevant".to_uppercase(), "20200810");
344-
345-
let detector = EnvResourceDetector::new();
346-
let resource =
347-
Resource::from_detectors(time::Duration::from_secs(5), vec![Box::new(detector)]);
348-
assert_eq!(
349-
resource,
350-
Resource::new(vec![
351-
KeyValue::new("key", "value"),
352-
KeyValue::new("k", "v"),
353-
KeyValue::new("a", "x"),
354-
KeyValue::new("a", "z"),
355-
])
342+
temp_env::with_vars(
343+
[
344+
(
345+
"OTEL_RESOURCE_ATTRIBUTES",
346+
Some("key=value, k = v , a= x, a=z"),
347+
),
348+
("IRRELEVANT", Some("20200810")),
349+
],
350+
|| {
351+
let detector = EnvResourceDetector::new();
352+
let resource = Resource::from_detectors(
353+
time::Duration::from_secs(5),
354+
vec![Box::new(detector)],
355+
);
356+
assert_eq!(
357+
resource,
358+
Resource::new(vec![
359+
KeyValue::new("key", "value"),
360+
KeyValue::new("k", "v"),
361+
KeyValue::new("a", "x"),
362+
KeyValue::new("a", "z"),
363+
])
364+
)
365+
},
356366
)
357367
}
358368
}

0 commit comments

Comments
 (0)