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
  • Loading branch information
hdost committed Feb 5, 2022
1 parent 561426e commit de1902a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions opentelemetry-zipkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ http = "0.2"
reqwest = { version = "0.11", optional = true, default-features = false }
surf = { version = "2.0", optional = true, default-features = false }
thiserror = { version = "1.0"}
duration-str = "0.3.8"

[dev-dependencies]
bytes = "1"
Expand Down
58 changes: 58 additions & 0 deletions opentelemetry-zipkin/src/exporter/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::ZipkinPipelineBuilder;

/// HTTP endpoint for Zipkin collector.
/// e.g. "http://localhost:9411/api/v2/spans"
#[cfg(feature = "collector_client")]
const ENV_ENDPOINT: &str = "OTEL_EXPORTER_ZIPKIN_ENDPOINT";

/// Maximum time the Zipkin exporter will wait for each batch export
#[cfg(feature = "collector_client")]
const ENV_TIMEOUT: &str = "OTEL_EXPORTER_ZIPKIN_TIMEOUT";

/// Assign builder attributes from env
pub(crate) fn assign_attrs(builder: ZipkinPipelineBuilder) -> ZipkinPipelineBuilder {

#[cfg(any(
feature = "reqwest-client",
feature = "surf-client",
feature = "reqwest-blocking-client"
))]
{
if let Some(endpoint) = env::var(ENV_ENDPOINT).ok().filter(|var| !var.is_empty()) {
builder = builder.with_collector_endpoint(endpoint);
}
}

#[cfg(any(
feature = "reqwest-client",
feature = "surf-client",
feature = "reqwest-blocking-client"
))]
{
// TODO: Need to parse from strings
if let Some(timeout) = env::var(ENV_TIMEOUT).ok().filter(|var| !var.is_empty()) {
let timeout = duration_str::parse(timeout)
.expect(format!("{} is not formatted correctly.",ENV_TIMEOUT));

#[cfg(feature = "reqwest-blocking-client")]
let client = reqwest::blocking::Client::builder().timeout(timeout).build().unwrap();
#[cfg(all(
not(feature = "reqwest-blocking-client"),
not(feature = "surf-client"),
feature = "reqwest-client"
))]
let client = reqwest::Client::builder().timeout(timeout).build().unwrap();
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "reqwest-blocking-client"),
feature = "surf-client"
))]
let client: surf::Client = surf::Config::new()
.set_timeout(Some()).into();

builder = builder.with_http_client(client);
}
}

builder
}
8 changes: 6 additions & 2 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 Down Expand Up @@ -57,7 +58,7 @@ pub struct ZipkinPipelineBuilder {

impl Default for ZipkinPipelineBuilder {
fn default() -> Self {
ZipkinPipelineBuilder {
let builder_defaults = ZipkinPipelineBuilder {
#[cfg(feature = "reqwest-blocking-client")]
client: Some(Box::new(reqwest::blocking::Client::new())),
#[cfg(all(
Expand All @@ -83,7 +84,10 @@ impl Default for ZipkinPipelineBuilder {
service_addr: None,
collector_endpoint: DEFAULT_COLLECTOR_ENDPOINT.to_string(),
trace_config: None,
}
};

// Override above defaults with env vars if set
env::assign_attrs(builder_defaults)
}
}

Expand Down

0 comments on commit de1902a

Please sign in to comment.