Skip to content

Commit de53bb3

Browse files
committed
[OTLP Exporter] Use env for endpoint, timeout
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. Add basic tests for the new method.
1 parent 332bd18 commit de53bb3

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

opentelemetry-otlp/src/lib.rs

Lines changed: 94 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 {
@@ -264,6 +279,27 @@ impl OtlpPipelineBuilder {
264279
self
265280
}
266281

282+
/// Set the trace provider configuration from the given environment variables.
283+
///
284+
/// If the value in environment variables is illegal, will fall back to use default value.
285+
pub fn with_env(mut self) -> Self {
286+
let endpoint = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
287+
Ok(val) => val,
288+
Err(_) => std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT)
289+
.unwrap_or_else(|_| OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string()),
290+
};
291+
self.exporter_config.endpoint = endpoint;
292+
293+
let timeout = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT) {
294+
Ok(val) => u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
295+
Err(_) => std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT)
296+
.map(|val| u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT))
297+
.unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
298+
};
299+
self.exporter_config.timeout = Duration::from_secs(timeout);
300+
self
301+
}
302+
267303
/// Install the OTLP exporter pipeline with the recommended defaults.
268304
#[cfg(feature = "tonic")]
269305
pub fn install(mut self) -> Result<(sdk::trace::Tracer, Uninstall), TraceError> {
@@ -341,3 +377,61 @@ pub enum Protocol {
341377
// HttpJson,
342378
// HttpProto,
343379
}
380+
381+
#[cfg(test)]
382+
mod tests {
383+
use crate::{
384+
new_pipeline, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TIMEOUT,
385+
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
386+
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
387+
};
388+
389+
#[test]
390+
fn test_pipeline_builder_from_otlp_env() {
391+
std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, "https://otlp_endpoint:4317");
392+
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "bad_timeout");
393+
394+
let mut pipeline_builder = new_pipeline().with_env();
395+
assert_eq!(
396+
pipeline_builder.exporter_config.timeout,
397+
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
398+
);
399+
400+
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "60");
401+
402+
pipeline_builder = new_pipeline().with_env();
403+
assert_eq!(
404+
pipeline_builder.exporter_config.timeout,
405+
std::time::Duration::from_secs(60)
406+
);
407+
408+
std::env::remove_var(OTEL_EXPORTER_OTLP_ENDPOINT);
409+
std::env::remove_var(OTEL_EXPORTER_OTLP_TIMEOUT);
410+
}
411+
412+
#[test]
413+
fn test_pipeline_builder_from_otlp_traces_env() {
414+
std::env::set_var(
415+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
416+
"https://otlp_traces_endpoint:4317",
417+
);
418+
std::env::set_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, "bad_timeout");
419+
420+
let mut pipeline_builder = new_pipeline().with_env();
421+
assert_eq!(
422+
pipeline_builder.exporter_config.timeout,
423+
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
424+
);
425+
426+
std::env::set_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, "60");
427+
428+
pipeline_builder = new_pipeline().with_env();
429+
assert_eq!(
430+
pipeline_builder.exporter_config.timeout,
431+
std::time::Duration::from_secs(60)
432+
);
433+
434+
std::env::remove_var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
435+
std::env::remove_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT);
436+
}
437+
}

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

@@ -134,18 +134,16 @@ impl Into<grpcio::CompressionAlgorithms> for Compression {
134134
}
135135
}
136136

137-
const DEFAULT_OTLP_PORT: u16 = 4317;
138-
139137
impl Default for ExporterConfig {
140138
#[cfg(feature = "tonic")]
141139
fn default() -> Self {
142140
ExporterConfig {
143-
endpoint: format!("http://localhost:{}", DEFAULT_OTLP_PORT),
141+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
144142
protocol: Protocol::Grpc,
145143
#[cfg(all(feature = "tonic", feature = "tls"))]
146144
tls_config: None,
147145
metadata: None,
148-
timeout: Duration::from_secs(60),
146+
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
149147
#[cfg(not(feature = "async"))]
150148
runtime: None,
151149
}
@@ -154,12 +152,12 @@ impl Default for ExporterConfig {
154152
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
155153
fn default() -> Self {
156154
ExporterConfig {
157-
endpoint: format!("localhost:{}", DEFAULT_OTLP_PORT),
155+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
158156
protocol: Protocol::Grpc,
159157
credentials: None,
160158
headers: None,
161159
compression: None,
162-
timeout: Duration::from_secs(60),
160+
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
163161
completion_queue_count: 2,
164162
}
165163
}

0 commit comments

Comments
 (0)