1
1
use super :: {
2
- default_headers, default_protocol, parse_header_string, ExporterBuildError ,
2
+ default_headers, default_protocol, parse_header_string, resolve_timeout , ExporterBuildError ,
3
3
OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
4
4
} ;
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 } ;
9
6
use http:: { HeaderName , HeaderValue , Uri } ;
10
7
use opentelemetry_http:: HttpClient ;
11
8
use opentelemetry_proto:: transform:: common:: tonic:: ResourceAttributesWithSchema ;
@@ -112,19 +109,10 @@ impl HttpExporterBuilder {
112
109
let endpoint = resolve_http_endpoint (
113
110
signal_endpoint_var,
114
111
signal_endpoint_path,
115
- self . exporter_config . endpoint . clone ( ) ,
112
+ self . exporter_config . endpoint . as_deref ( ) ,
116
113
) ?;
117
114
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 ( ) ) ;
128
116
129
117
#[ allow( unused_mut) ] // TODO - clippy thinks mut is not needed, but it is
130
118
let mut http_client = self . http_config . client . take ( ) ;
@@ -371,30 +359,27 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, ExporterBuildEr
371
359
fn resolve_http_endpoint (
372
360
signal_endpoint_var : & str ,
373
361
signal_endpoint_path : & str ,
374
- provided_endpoint : Option < String > ,
362
+ provided_endpoint : Option < & str > ,
375
363
) -> 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)
378
372
. ok ( )
379
373
. and_then ( |s| s. parse ( ) . ok ( ) )
380
374
{
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 )
386
378
. ok ( )
387
379
. and_then ( |s| build_endpoint_uri ( & s, signal_endpoint_path) . ok ( ) )
388
380
{
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)
398
383
} else {
399
384
build_endpoint_uri (
400
385
OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
@@ -481,12 +466,9 @@ mod tests {
481
466
run_env_test (
482
467
vec ! [ ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ] ,
483
468
|| {
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 ( ) ;
490
472
assert_eq ! ( endpoint, "http://example.com/v1/traces" ) ;
491
473
} ,
492
474
)
@@ -496,20 +478,36 @@ mod tests {
496
478
fn test_not_append_signal_path_to_signal_env ( ) {
497
479
run_env_test (
498
480
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
+ ] ,
499
497
|| {
500
498
let endpoint = super :: resolve_http_endpoint (
501
499
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
502
500
"/v1/traces" ,
503
- Some ( "http://localhost:4317" . to_string ( ) ) ,
501
+ None ,
504
502
)
505
503
. unwrap ( ) ;
506
504
assert_eq ! ( endpoint, "http://example.com" ) ;
507
505
} ,
508
- )
506
+ ) ;
509
507
}
510
508
511
509
#[ test]
512
- fn test_priority_of_signal_env_over_generic_env ( ) {
510
+ fn test_priority_of_code_based_config_over_envs ( ) {
513
511
run_env_test (
514
512
vec ! [
515
513
( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "http://example.com" ) ,
@@ -519,24 +517,20 @@ mod tests {
519
517
let endpoint = super :: resolve_http_endpoint (
520
518
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
521
519
"/v1/traces" ,
522
- Some ( "http://localhost:4317" . to_string ( ) ) ,
520
+ Some ( "http://localhost:4317" ) ,
523
521
)
524
522
. unwrap ( ) ;
525
- assert_eq ! ( endpoint, "http://example.com " ) ;
523
+ assert_eq ! ( endpoint, "http://localhost:4317 " ) ;
526
524
} ,
527
525
) ;
528
526
}
529
527
530
528
#[ test]
531
- fn test_use_provided_or_default_when_others_missing ( ) {
529
+ fn test_use_default_when_others_missing ( ) {
532
530
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" ) ;
540
534
} ) ;
541
535
}
542
536
@@ -568,7 +562,7 @@ mod tests {
568
562
let endpoint = super :: resolve_http_endpoint (
569
563
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
570
564
"/v1/traces" ,
571
- Some ( "http://localhost:4317" . to_string ( ) ) ,
565
+ None ,
572
566
)
573
567
. unwrap ( ) ;
574
568
assert_eq ! ( endpoint, "http://example.com/v1/traces" ) ;
@@ -582,7 +576,7 @@ mod tests {
582
576
let result = super :: resolve_http_endpoint (
583
577
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
584
578
"/v1/traces" ,
585
- Some ( "-*/*-/*-//-/-/yet-another-invalid-uri" . to_string ( ) ) ,
579
+ Some ( "-*/*-/*-//-/-/yet-another-invalid-uri" ) ,
586
580
) ;
587
581
assert ! ( result. is_err( ) ) ;
588
582
// You may also want to assert on the specific error type if applicable
@@ -722,7 +716,7 @@ mod tests {
722
716
let url = resolve_http_endpoint (
723
717
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
724
718
"/v1/traces" ,
725
- exporter. exporter_config . endpoint ,
719
+ exporter. exporter_config . endpoint . as_deref ( ) ,
726
720
)
727
721
. unwrap ( ) ;
728
722
@@ -737,7 +731,7 @@ mod tests {
737
731
let url = resolve_http_endpoint (
738
732
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
739
733
"/v1/traces" ,
740
- exporter. exporter_config . endpoint ,
734
+ exporter. exporter_config . endpoint . as_deref ( ) ,
741
735
)
742
736
. unwrap ( ) ;
743
737
0 commit comments