From 33b4dfea8e4787a16b59573cebf3185840487843 Mon Sep 17 00:00:00 2001 From: gruebel Date: Wed, 19 Mar 2025 23:24:13 +0100 Subject: [PATCH] validate code-based config is preferred over env vars in ZipkinExporterBuilder --- opentelemetry-zipkin/src/exporter/env.rs | 2 +- opentelemetry-zipkin/src/exporter/mod.rs | 32 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/opentelemetry-zipkin/src/exporter/env.rs b/opentelemetry-zipkin/src/exporter/env.rs index 75d552b24f..66d4fd5fd8 100644 --- a/opentelemetry-zipkin/src/exporter/env.rs +++ b/opentelemetry-zipkin/src/exporter/env.rs @@ -6,7 +6,7 @@ const DEFAULT_COLLECTOR_ENDPOINT: &str = "http://127.0.0.1:9411/api/v2/spans"; /// HTTP endpoint for Zipkin collector. /// e.g. "http://localhost:9411/api/v2/spans" -const ENV_ENDPOINT: &str = "OTEL_EXPORTER_ZIPKIN_ENDPOINT"; +pub(crate) const ENV_ENDPOINT: &str = "OTEL_EXPORTER_ZIPKIN_ENDPOINT"; /// Maximum time the Zipkin exporter will wait for each batch export const ENV_TIMEOUT: &str = "OTEL_EXPORTER_ZIPKIN_TIMEOUT"; diff --git a/opentelemetry-zipkin/src/exporter/mod.rs b/opentelemetry-zipkin/src/exporter/mod.rs index 24fe931c0e..fa0386f597 100644 --- a/opentelemetry-zipkin/src/exporter/mod.rs +++ b/opentelemetry-zipkin/src/exporter/mod.rs @@ -93,6 +93,9 @@ impl ZipkinExporterBuilder { } /// Assign client implementation + /// + /// Note: Programmatically setting the timeout will override any value + /// set via the environment variable `OTEL_EXPORTER_ZIPKIN_TIMEOUT`. pub fn with_http_client(mut self, client: T) -> Self { self.client = Some(Arc::new(client)); self @@ -105,6 +108,9 @@ impl ZipkinExporterBuilder { } /// Assign the Zipkin collector endpoint + /// + /// Note: Programmatically setting this will override any value + /// set via the environment variable `OTEL_EXPORTER_ZIPKIN_ENDPOINT`. pub fn with_collector_endpoint>(mut self, endpoint: T) -> Self { self.collector_endpoint = endpoint.into(); self @@ -161,3 +167,29 @@ impl ExportError for Error { "zipkin" } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::exporter::env::ENV_ENDPOINT; + + #[test] + fn test_priority_of_code_based_config_over_envs_for_endpoint() { + temp_env::with_vars([(ENV_ENDPOINT, Some("http://127.0.0.1:1234"))], || { + let builder = + ZipkinExporterBuilder::default().with_collector_endpoint("http://127.0.0.1:2345"); + assert_eq!(builder.collector_endpoint, "http://127.0.0.1:2345"); + }); + } + + #[test] + fn test_use_default_when_others_missing_for_endpoint() { + temp_env::with_vars([(ENV_ENDPOINT, None::<&str>)], || { + let builder = ZipkinExporterBuilder::default(); + assert_eq!( + builder.collector_endpoint, + "http://127.0.0.1:9411/api/v2/spans" + ); + }); + } +}