Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: validate code-based config is preferred over env vars in ZipkinExporterBuilder #2836

Merged
merged 3 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opentelemetry-zipkin/src/exporter/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
32 changes: 32 additions & 0 deletions opentelemetry-zipkin/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is for timeout, but this method is for setting client?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, you can only set the timeout manually, if you create a client, because the timeout is used during the client creation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you modify the comment to make it clearer?

when using with_http_client, the timeout will depend on the provided client implementation, and may not respect timeout set via the environment variable OTEL_EXPORTER_ZIPKIN_TIMEOUT.

pub fn with_http_client<T: HttpClient + 'static>(mut self, client: T) -> Self {
self.client = Some(Arc::new(client));
self
Expand All @@ -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<T: Into<String>>(mut self, endpoint: T) -> Self {
self.collector_endpoint = endpoint.into();
self
Expand Down Expand Up @@ -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"
);
});
}
}