@@ -123,6 +123,7 @@ use opentelemetry::{global, sdk, trace::TracerProvider};
123123#[ cfg( all( feature = "grpc-sys" , not( feature = "tonic" ) ) ) ]
124124use std:: collections:: HashMap ;
125125
126+ use std:: str:: FromStr ;
126127use 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+
200215impl 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+ }
0 commit comments