Skip to content

Commit

Permalink
opentelemetry-zipkin: Add Environment Variables
Browse files Browse the repository at this point in the history
As described in https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md
* Add support for:
** OTEL_EXPORTER_ZIPKIN_ENDPOINT
** OTEL_EXPORTER_ZIPKIN_TIMEOUT
* Update Default to use

Closes open-telemetry#534

Signed-off-by: Harold Dost <h.dost@criteo.com>
  • Loading branch information
hdost committed Feb 6, 2022
1 parent 561426e commit ab0bd66
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions opentelemetry-zipkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
typed-builder = "0.9"
lazy_static = "1.4"
log = "0.4"
http = "0.2"
reqwest = { version = "0.11", optional = true, default-features = false }
surf = { version = "2.0", optional = true, default-features = false }
Expand Down
35 changes: 35 additions & 0 deletions opentelemetry-zipkin/src/exporter/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use log::warn;
use std::env;
use std::time::Duration;

/// Default Zipkin collector endpoint
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";

/// Maximum time the Zipkin exporter will wait for each batch export
const ENV_TIMEOUT: &str = "OTEL_EXPORTER_ZIPKIN_TIMEOUT";
/// Default Zipkin timeout milliseconds
const DEFAULT_COLLECTOR_TIMEOUT: u64 = 10_000;

pub(crate) fn get_timeout() -> Duration {
match env::var(ENV_TIMEOUT).ok().filter(|var| !var.is_empty()) {
Some(timeout) => match timeout.parse() {
Ok(timeout) => Duration::from_millis(timeout),
Err(e) => {
warn!("{} malformed defaulting to 10000: {}", ENV_TIMEOUT, e);
Duration::from_millis(DEFAULT_COLLECTOR_TIMEOUT)
}
},
None => Duration::from_millis(DEFAULT_COLLECTOR_TIMEOUT),
}
}

pub(crate) fn get_endpoint() -> String {
match env::var(ENV_ENDPOINT).ok().filter(|var| !var.is_empty()) {
Some(endpoint) => endpoint,
None => DEFAULT_COLLECTOR_ENDPOINT.to_string(),
}
}
22 changes: 15 additions & 7 deletions opentelemetry-zipkin/src/exporter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod env;
mod model;
mod uploader;

Expand All @@ -21,9 +22,6 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

/// Default Zipkin collector endpoint
const DEFAULT_COLLECTOR_ENDPOINT: &str = "http://127.0.0.1:9411/api/v2/spans";

/// Zipkin span exporter
#[derive(Debug)]
pub struct Exporter {
Expand Down Expand Up @@ -57,21 +55,31 @@ pub struct ZipkinPipelineBuilder {

impl Default for ZipkinPipelineBuilder {
fn default() -> Self {
let timeout = env::get_timeout();
ZipkinPipelineBuilder {
#[cfg(feature = "reqwest-blocking-client")]
client: Some(Box::new(reqwest::blocking::Client::new())),
client: Some(Box::new(
reqwest::blocking::Client::builder()
.timeout(timeout)
.build()
.unwrap(),
)),
#[cfg(all(
not(feature = "reqwest-blocking-client"),
not(feature = "surf-client"),
feature = "reqwest-client"
))]
client: Some(Box::new(reqwest::Client::new())),
client: Some(Box::new(
reqwest::Client::builder().timeout(timeout).build().unwrap(),
)),
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "reqwest-blocking-client"),
feature = "surf-client"
))]
client: Some(Box::new(surf::Client::new())),
client: Some(Box::new(
surf::Config::new().set_timeout(Some(timeout)).into(),
)),
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "surf-client"),
Expand All @@ -81,7 +89,7 @@ impl Default for ZipkinPipelineBuilder {

service_name: None,
service_addr: None,
collector_endpoint: DEFAULT_COLLECTOR_ENDPOINT.to_string(),
collector_endpoint: env::get_endpoint(),
trace_config: None,
}
}
Expand Down

0 comments on commit ab0bd66

Please sign in to comment.