11use super :: {
2- default_headers, default_protocol, parse_header_string, ExporterBuildError ,
2+ default_headers, default_protocol, parse_header_string, resolve_timeout , ExporterBuildError ,
33 OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
44} ;
5- use crate :: {
6- ExportConfig , Protocol , OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_HEADERS ,
7- OTEL_EXPORTER_OTLP_TIMEOUT ,
8- } ;
5+ use crate :: { ExportConfig , Protocol , OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_HEADERS } ;
96use http:: { HeaderName , HeaderValue , Uri } ;
107use opentelemetry_http:: HttpClient ;
118use opentelemetry_proto:: transform:: common:: tonic:: ResourceAttributesWithSchema ;
@@ -112,19 +109,10 @@ impl HttpExporterBuilder {
112109 let endpoint = resolve_http_endpoint (
113110 signal_endpoint_var,
114111 signal_endpoint_path,
115- self . exporter_config . endpoint . clone ( ) ,
112+ self . exporter_config . endpoint . as_deref ( ) ,
116113 ) ?;
117114
118- let timeout = match env:: var ( signal_timeout_var)
119- . ok ( )
120- . or ( env:: var ( OTEL_EXPORTER_OTLP_TIMEOUT ) . ok ( ) )
121- {
122- Some ( val) => match val. parse ( ) {
123- Ok ( milli_seconds) => Duration :: from_millis ( milli_seconds) ,
124- Err ( _) => self . exporter_config . timeout ,
125- } ,
126- None => self . exporter_config . timeout ,
127- } ;
115+ let timeout = resolve_timeout ( signal_timeout_var, self . exporter_config . timeout . as_ref ( ) ) ;
128116
129117 #[ allow( unused_mut) ] // TODO - clippy thinks mut is not needed, but it is
130118 let mut http_client = self . http_config . client . take ( ) ;
@@ -371,30 +359,27 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, ExporterBuildEr
371359fn resolve_http_endpoint (
372360 signal_endpoint_var : & str ,
373361 signal_endpoint_path : & str ,
374- provided_endpoint : Option < String > ,
362+ provided_endpoint : Option < & str > ,
375363) -> Result < Uri , ExporterBuildError > {
376- // per signal env var is not modified
377- if let Some ( endpoint) = env:: var ( signal_endpoint_var)
364+ // programmatic configuration overrides any value set via environment variables
365+ if let Some ( provider_endpoint) = provided_endpoint {
366+ provider_endpoint
367+ . parse ( )
368+ . map_err ( |er : http:: uri:: InvalidUri | {
369+ ExporterBuildError :: InvalidUri ( provider_endpoint. to_string ( ) , er. to_string ( ) )
370+ } )
371+ } else if let Some ( endpoint) = env:: var ( signal_endpoint_var)
378372 . ok ( )
379373 . and_then ( |s| s. parse ( ) . ok ( ) )
380374 {
381- return Ok ( endpoint) ;
382- }
383-
384- // if signal env var is not set, then we check if the OTEL_EXPORTER_OTLP_ENDPOINT is set
385- if let Some ( endpoint) = env:: var ( OTEL_EXPORTER_OTLP_ENDPOINT )
375+ // per signal env var is not modified
376+ Ok ( endpoint)
377+ } else if let Some ( endpoint) = env:: var ( OTEL_EXPORTER_OTLP_ENDPOINT )
386378 . ok ( )
387379 . and_then ( |s| build_endpoint_uri ( & s, signal_endpoint_path) . ok ( ) )
388380 {
389- return Ok ( endpoint) ;
390- }
391-
392- if let Some ( provider_endpoint) = provided_endpoint {
393- provider_endpoint
394- . parse ( )
395- . map_err ( |er : http:: uri:: InvalidUri | {
396- ExporterBuildError :: InvalidUri ( provider_endpoint, er. to_string ( ) )
397- } )
381+ // if signal env var is not set, then we check if the OTEL_EXPORTER_OTLP_ENDPOINT env var is set
382+ Ok ( endpoint)
398383 } else {
399384 build_endpoint_uri (
400385 OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
@@ -481,12 +466,9 @@ mod tests {
481466 run_env_test (
482467 vec ! [ ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ] ,
483468 || {
484- let endpoint = resolve_http_endpoint (
485- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
486- "/v1/traces" ,
487- Some ( "http://localhost:4317" . to_string ( ) ) ,
488- )
489- . unwrap ( ) ;
469+ let endpoint =
470+ resolve_http_endpoint ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "/v1/traces" , None )
471+ . unwrap ( ) ;
490472 assert_eq ! ( endpoint, "http://example.com/v1/traces" ) ;
491473 } ,
492474 )
@@ -496,20 +478,36 @@ mod tests {
496478 fn test_not_append_signal_path_to_signal_env ( ) {
497479 run_env_test (
498480 vec ! [ ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "http://example.com" ) ] ,
481+ || {
482+ let endpoint =
483+ resolve_http_endpoint ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "/v1/traces" , None )
484+ . unwrap ( ) ;
485+ assert_eq ! ( endpoint, "http://example.com" ) ;
486+ } ,
487+ )
488+ }
489+
490+ #[ test]
491+ fn test_priority_of_signal_env_over_generic_env ( ) {
492+ run_env_test (
493+ vec ! [
494+ ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "http://example.com" ) ,
495+ ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://wrong.com" ) ,
496+ ] ,
499497 || {
500498 let endpoint = super :: resolve_http_endpoint (
501499 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
502500 "/v1/traces" ,
503- Some ( "http://localhost:4317" . to_string ( ) ) ,
501+ None ,
504502 )
505503 . unwrap ( ) ;
506504 assert_eq ! ( endpoint, "http://example.com" ) ;
507505 } ,
508- )
506+ ) ;
509507 }
510508
511509 #[ test]
512- fn test_priority_of_signal_env_over_generic_env ( ) {
510+ fn test_priority_of_code_based_config_over_envs ( ) {
513511 run_env_test (
514512 vec ! [
515513 ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "http://example.com" ) ,
@@ -519,24 +517,20 @@ mod tests {
519517 let endpoint = super :: resolve_http_endpoint (
520518 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
521519 "/v1/traces" ,
522- Some ( "http://localhost:4317" . to_string ( ) ) ,
520+ Some ( "http://localhost:4317" ) ,
523521 )
524522 . unwrap ( ) ;
525- assert_eq ! ( endpoint, "http://example.com " ) ;
523+ assert_eq ! ( endpoint, "http://localhost:4317 " ) ;
526524 } ,
527525 ) ;
528526 }
529527
530528 #[ test]
531- fn test_use_provided_or_default_when_others_missing ( ) {
529+ fn test_use_default_when_others_missing ( ) {
532530 run_env_test ( vec ! [ ] , || {
533- let endpoint = super :: resolve_http_endpoint (
534- "NON_EXISTENT_VAR" ,
535- "/v1/traces" ,
536- Some ( "http://localhost:4317" . to_string ( ) ) ,
537- )
538- . unwrap ( ) ;
539- assert_eq ! ( endpoint, "http://localhost:4317/" ) ;
531+ let endpoint =
532+ super :: resolve_http_endpoint ( "NON_EXISTENT_VAR" , "/v1/traces" , None ) . unwrap ( ) ;
533+ assert_eq ! ( endpoint, "http://localhost:4318/v1/traces" ) ;
540534 } ) ;
541535 }
542536
@@ -568,7 +562,7 @@ mod tests {
568562 let endpoint = super :: resolve_http_endpoint (
569563 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
570564 "/v1/traces" ,
571- Some ( "http://localhost:4317" . to_string ( ) ) ,
565+ None ,
572566 )
573567 . unwrap ( ) ;
574568 assert_eq ! ( endpoint, "http://example.com/v1/traces" ) ;
@@ -582,7 +576,7 @@ mod tests {
582576 let result = super :: resolve_http_endpoint (
583577 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
584578 "/v1/traces" ,
585- Some ( "-*/*-/*-//-/-/yet-another-invalid-uri" . to_string ( ) ) ,
579+ Some ( "-*/*-/*-//-/-/yet-another-invalid-uri" ) ,
586580 ) ;
587581 assert ! ( result. is_err( ) ) ;
588582 // You may also want to assert on the specific error type if applicable
@@ -722,7 +716,7 @@ mod tests {
722716 let url = resolve_http_endpoint (
723717 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
724718 "/v1/traces" ,
725- exporter. exporter_config . endpoint ,
719+ exporter. exporter_config . endpoint . as_deref ( ) ,
726720 )
727721 . unwrap ( ) ;
728722
@@ -737,7 +731,7 @@ mod tests {
737731 let url = resolve_http_endpoint (
738732 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
739733 "/v1/traces" ,
740- exporter. exporter_config . endpoint ,
734+ exporter. exporter_config . endpoint . as_deref ( ) ,
741735 )
742736 . unwrap ( ) ;
743737
0 commit comments