Skip to content

Commit 238e797

Browse files
authored
[OTLP Exporter] Use env for endpoint, timeout (#451)
Add `with_env` method for `OtlpPipelineBuilder`. This method currently only configures `endpoint` and `timeout` for OTLP Traces exporter. Add default constants for those parameters and use them for the default `ExporterConfig`. Use values defined in specification.
1 parent 2269d1f commit 238e797

File tree

2 files changed

+103
-7
lines changed

2 files changed

+103
-7
lines changed

opentelemetry-otlp/src/lib.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ use opentelemetry::{global, sdk, trace::TracerProvider};
123123
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
124124
use std::collections::HashMap;
125125

126+
use std::str::FromStr;
126127
use std::time::Duration;
127128

128129
#[cfg(all(feature = "tonic", not(feature = "integration-testing")))]
@@ -197,6 +198,20 @@ pub struct OtlpPipelineBuilder {
197198
trace_config: Option<sdk::trace::Config>,
198199
}
199200

201+
/// Target to which the exporter is going to send spans or metrics, defaults to https://localhost:4317.
202+
const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
203+
/// Default target to which the exporter is going to send spans or metrics.
204+
const OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT: &str = "https://localhost:4317";
205+
/// Max waiting time for the backend to process each spans or metrics batch, defaults to 10 seconds.
206+
const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";
207+
/// Default max waiting time for the backend to process each spans or metrics batch.
208+
const OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT: u64 = 10;
209+
210+
/// Target to which the exporter is going to send spans, defaults to https://localhost:4317.
211+
const OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
212+
/// Max waiting time for the backend to process each spans batch, defaults to 10s.
213+
const OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT";
214+
200215
impl OtlpPipelineBuilder {
201216
/// Set the address of the OTLP collector. If not set, the default address is used.
202217
pub fn with_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self {
@@ -271,6 +286,27 @@ impl OtlpPipelineBuilder {
271286
self
272287
}
273288

289+
/// Set the trace provider configuration from the given environment variables.
290+
///
291+
/// If the value in environment variables is illegal, will fall back to use default value.
292+
pub fn with_env(mut self) -> Self {
293+
let endpoint = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
294+
Ok(val) => val,
295+
Err(_) => std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT)
296+
.unwrap_or_else(|_| OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string()),
297+
};
298+
self.exporter_config.endpoint = endpoint;
299+
300+
let timeout = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT) {
301+
Ok(val) => u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
302+
Err(_) => std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT)
303+
.map(|val| u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT))
304+
.unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
305+
};
306+
self.exporter_config.timeout = Duration::from_secs(timeout);
307+
self
308+
}
309+
274310
/// Install the OTLP exporter pipeline with the recommended defaults.
275311
#[cfg(feature = "tonic")]
276312
pub fn install(mut self) -> Result<(sdk::trace::Tracer, Uninstall), TraceError> {
@@ -348,3 +384,65 @@ pub enum Protocol {
348384
// HttpJson,
349385
// HttpProto,
350386
}
387+
388+
#[cfg(test)]
389+
mod tests {
390+
use crate::{
391+
new_pipeline, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TIMEOUT,
392+
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
393+
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
394+
};
395+
396+
#[test]
397+
fn test_pipeline_builder_from_otlp_env() {
398+
std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, "https://otlp_endpoint:4317");
399+
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "bad_timeout");
400+
401+
let mut pipeline_builder = new_pipeline().with_env();
402+
assert_eq!(
403+
pipeline_builder.exporter_config.timeout,
404+
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
405+
);
406+
407+
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "60");
408+
409+
pipeline_builder = new_pipeline().with_env();
410+
assert_eq!(
411+
pipeline_builder.exporter_config.timeout,
412+
std::time::Duration::from_secs(60)
413+
);
414+
415+
std::env::remove_var(OTEL_EXPORTER_OTLP_ENDPOINT);
416+
std::env::remove_var(OTEL_EXPORTER_OTLP_TIMEOUT);
417+
assert!(std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT).is_err());
418+
assert!(std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT).is_err());
419+
}
420+
421+
#[test]
422+
fn test_pipeline_builder_from_otlp_traces_env() {
423+
std::env::set_var(
424+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
425+
"https://otlp_traces_endpoint:4317",
426+
);
427+
std::env::set_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, "bad_timeout");
428+
429+
let mut pipeline_builder = new_pipeline().with_env();
430+
assert_eq!(
431+
pipeline_builder.exporter_config.timeout,
432+
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
433+
);
434+
435+
std::env::set_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, "60");
436+
437+
pipeline_builder = new_pipeline().with_env();
438+
assert_eq!(
439+
pipeline_builder.exporter_config.timeout,
440+
std::time::Duration::from_secs(60)
441+
);
442+
443+
std::env::remove_var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
444+
std::env::remove_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT);
445+
assert!(std::env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT).is_err());
446+
assert!(std::env::var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT).is_err());
447+
}
448+
}

opentelemetry-otlp/src/span.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::fmt::Debug;
4242
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
4343
use std::sync::Arc;
4444

45-
use crate::Protocol;
45+
use crate::{Protocol, OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT};
4646
use opentelemetry::sdk::export::trace::{ExportResult, SpanData, SpanExporter};
4747
use std::time::Duration;
4848

@@ -138,18 +138,16 @@ impl Into<grpcio::CompressionAlgorithms> for Compression {
138138
}
139139
}
140140

141-
const DEFAULT_OTLP_PORT: u16 = 4317;
142-
143141
impl Default for ExporterConfig {
144142
#[cfg(feature = "tonic")]
145143
fn default() -> Self {
146144
ExporterConfig {
147-
endpoint: format!("http://localhost:{}", DEFAULT_OTLP_PORT),
145+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
148146
protocol: Protocol::Grpc,
149147
#[cfg(all(feature = "tonic", feature = "tls"))]
150148
tls_config: None,
151149
metadata: None,
152-
timeout: Duration::from_secs(60),
150+
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
153151
#[cfg(not(feature = "async"))]
154152
runtime: None,
155153
}
@@ -158,13 +156,13 @@ impl Default for ExporterConfig {
158156
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
159157
fn default() -> Self {
160158
ExporterConfig {
161-
endpoint: format!("localhost:{}", DEFAULT_OTLP_PORT),
159+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
162160
protocol: Protocol::Grpc,
163161
credentials: None,
164162
headers: None,
165163
compression: None,
166164
use_tls: None,
167-
timeout: Duration::from_secs(60),
165+
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
168166
completion_queue_count: 2,
169167
}
170168
}

0 commit comments

Comments
 (0)