@@ -123,6 +123,7 @@ use opentelemetry::{global, sdk, trace::TracerProvider};
123
123
#[ cfg( all( feature = "grpc-sys" , not( feature = "tonic" ) ) ) ]
124
124
use std:: collections:: HashMap ;
125
125
126
+ use std:: str:: FromStr ;
126
127
use std:: time:: Duration ;
127
128
128
129
#[ cfg( all( feature = "tonic" , not( feature = "integration-testing" ) ) ) ]
@@ -197,6 +198,20 @@ pub struct OtlpPipelineBuilder {
197
198
trace_config : Option < sdk:: trace:: Config > ,
198
199
}
199
200
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
+
200
215
impl OtlpPipelineBuilder {
201
216
/// Set the address of the OTLP collector. If not set, the default address is used.
202
217
pub fn with_endpoint < T : Into < String > > ( mut self , endpoint : T ) -> Self {
@@ -264,6 +279,27 @@ impl OtlpPipelineBuilder {
264
279
self
265
280
}
266
281
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
+
267
303
/// Install the OTLP exporter pipeline with the recommended defaults.
268
304
#[ cfg( feature = "tonic" ) ]
269
305
pub fn install ( mut self ) -> Result < ( sdk:: trace:: Tracer , Uninstall ) , TraceError > {
@@ -341,3 +377,61 @@ pub enum Protocol {
341
377
// HttpJson,
342
378
// HttpProto,
343
379
}
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
+ }
0 commit comments