Skip to content

Commit 60af6e1

Browse files
authored
opentelemetry-zipkin: Add Environment Variables (open-telemetry#718)
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 * Add remove_var to flaky test Closes open-telemetry#534 Signed-off-by: Harold Dost <[email protected]>
1 parent d818d9b commit 60af6e1

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

opentelemetry-zipkin/CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## unreleased
4+
5+
## Added
6+
7+
- Add support for OTEL_EXPORTER_ZIPKIN_* variables. #718
8+
9+
## Changed
10+
11+
- Add defaults for timeouts to HTTP clients #718
12+
313
## v0.15.0
414

515
### Changed
@@ -116,5 +126,5 @@
116126

117127
### Added
118128

119-
- Exporter to Zipkin collector through HTTP API
129+
- Exporter to Zipkin collector through HTTP API
120130

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::env;
2+
use std::time::Duration;
3+
4+
/// Default Zipkin collector endpoint
5+
const DEFAULT_COLLECTOR_ENDPOINT: &str = "http://127.0.0.1:9411/api/v2/spans";
6+
7+
/// HTTP endpoint for Zipkin collector.
8+
/// e.g. "http://localhost:9411/api/v2/spans"
9+
const ENV_ENDPOINT: &str = "OTEL_EXPORTER_ZIPKIN_ENDPOINT";
10+
11+
/// Maximum time the Zipkin exporter will wait for each batch export
12+
const ENV_TIMEOUT: &str = "OTEL_EXPORTER_ZIPKIN_TIMEOUT";
13+
14+
/// Default Zipkin timeout in milliseconds
15+
const DEFAULT_COLLECTOR_TIMEOUT: Duration = Duration::from_millis(10_000);
16+
17+
pub(crate) fn get_timeout() -> Duration {
18+
match env::var(ENV_TIMEOUT).ok().filter(|var| !var.is_empty()) {
19+
Some(timeout) => match timeout.parse() {
20+
Ok(timeout) => Duration::from_millis(timeout),
21+
Err(e) => {
22+
eprintln!("{} malformed defaulting to 10000: {}", ENV_TIMEOUT, e);
23+
DEFAULT_COLLECTOR_TIMEOUT
24+
}
25+
},
26+
None => DEFAULT_COLLECTOR_TIMEOUT,
27+
}
28+
}
29+
30+
pub(crate) fn get_endpoint() -> String {
31+
match env::var(ENV_ENDPOINT).ok().filter(|var| !var.is_empty()) {
32+
Some(endpoint) => endpoint,
33+
None => DEFAULT_COLLECTOR_ENDPOINT.to_string(),
34+
}
35+
}
36+
37+
#[test]
38+
fn test_collector_defaults() {
39+
// Ensure the variables are undefined.
40+
env::remove_var(ENV_TIMEOUT);
41+
env::remove_var(ENV_ENDPOINT);
42+
assert_eq!(DEFAULT_COLLECTOR_TIMEOUT, get_timeout());
43+
assert_eq!(DEFAULT_COLLECTOR_ENDPOINT, get_endpoint());
44+
}
45+
46+
#[test]
47+
fn test_collector_bad_timeout() {
48+
env::set_var(ENV_TIMEOUT, "a");
49+
assert_eq!(DEFAULT_COLLECTOR_TIMEOUT, get_timeout());
50+
}
51+
52+
#[test]
53+
fn test_collector_good_timeout() {
54+
env::set_var(ENV_TIMEOUT, "777");
55+
assert_eq!(Duration::from_millis(777), get_timeout());
56+
}
57+
58+
#[test]
59+
fn test_collector_custom_endpoint() {
60+
let custom_endpoint = "https://example.com/api/v2/spans";
61+
env::set_var(ENV_ENDPOINT, custom_endpoint);
62+
assert_eq!(custom_endpoint, get_endpoint());
63+
}

opentelemetry-zipkin/src/exporter/mod.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod env;
12
mod model;
23
mod uploader;
34

@@ -17,13 +18,16 @@ use opentelemetry::{
1718
};
1819
use opentelemetry_http::HttpClient;
1920
use opentelemetry_semantic_conventions as semcov;
21+
#[cfg(all(
22+
not(feature = "reqwest-client"),
23+
not(feature = "reqwest-blocking-client"),
24+
feature = "surf-client"
25+
))]
26+
use std::convert::TryFrom;
2027
use std::net::SocketAddr;
2128
use std::sync::Arc;
2229
use std::time::Duration;
2330

24-
/// Default Zipkin collector endpoint
25-
const DEFAULT_COLLECTOR_ENDPOINT: &str = "http://127.0.0.1:9411/api/v2/spans";
26-
2731
/// Zipkin span exporter
2832
#[derive(Debug)]
2933
pub struct Exporter {
@@ -57,21 +61,35 @@ pub struct ZipkinPipelineBuilder {
5761

5862
impl Default for ZipkinPipelineBuilder {
5963
fn default() -> Self {
64+
let timeout = env::get_timeout();
6065
ZipkinPipelineBuilder {
6166
#[cfg(feature = "reqwest-blocking-client")]
62-
client: Some(Box::new(reqwest::blocking::Client::new())),
67+
client: Some(Box::new(
68+
reqwest::blocking::Client::builder()
69+
.timeout(timeout)
70+
.build()
71+
.unwrap_or_else(|_| reqwest::blocking::Client::new()),
72+
)),
6373
#[cfg(all(
6474
not(feature = "reqwest-blocking-client"),
6575
not(feature = "surf-client"),
6676
feature = "reqwest-client"
6777
))]
68-
client: Some(Box::new(reqwest::Client::new())),
78+
client: Some(Box::new(
79+
reqwest::Client::builder()
80+
.timeout(timeout)
81+
.build()
82+
.unwrap_or_else(|_| reqwest::Client::new()),
83+
)),
6984
#[cfg(all(
7085
not(feature = "reqwest-client"),
7186
not(feature = "reqwest-blocking-client"),
7287
feature = "surf-client"
7388
))]
74-
client: Some(Box::new(surf::Client::new())),
89+
client: Some(Box::new(
90+
surf::Client::try_from(surf::Config::new().set_timeout(Some(timeout)))
91+
.unwrap_or_else(|_| surf::Client::new()),
92+
)),
7593
#[cfg(all(
7694
not(feature = "reqwest-client"),
7795
not(feature = "surf-client"),
@@ -81,7 +99,7 @@ impl Default for ZipkinPipelineBuilder {
8199

82100
service_name: None,
83101
service_addr: None,
84-
collector_endpoint: DEFAULT_COLLECTOR_ENDPOINT.to_string(),
102+
collector_endpoint: env::get_endpoint(),
85103
trace_config: None,
86104
}
87105
}

opentelemetry/src/sdk/resource/env.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ mod tests {
132132
#[test]
133133
fn test_sdk_provided_resource_detector() {
134134
const SERVICE_NAME: &str = "service.name";
135+
// Ensure no env var set
136+
env::remove_var(OTEL_RESOURCE_ATTRIBUTES);
135137
let no_env = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
136138
assert_eq!(
137139
no_env.get(Key::from_static_str(SERVICE_NAME)),

0 commit comments

Comments
 (0)