@@ -20,6 +20,8 @@ use std::str::FromStr;
20
20
use std:: sync:: { Arc , Mutex } ;
21
21
use std:: time:: Duration ;
22
22
23
+ use super :: { default_headers, parse_header_string, OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT } ;
24
+
23
25
#[ cfg( feature = "metrics" ) ]
24
26
mod metrics;
25
27
@@ -153,7 +155,7 @@ impl HttpExporterBuilder {
153
155
let endpoint = resolve_endpoint (
154
156
signal_endpoint_var,
155
157
signal_endpoint_path,
156
- self . exporter_config . endpoint . as_str ( ) ,
158
+ self . exporter_config . endpoint . as_deref ( ) ,
157
159
) ?;
158
160
159
161
let timeout = match env:: var ( signal_timeout_var)
@@ -376,7 +378,7 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, crate::Error> {
376
378
fn resolve_endpoint (
377
379
signal_endpoint_var : & str ,
378
380
signal_endpoint_path : & str ,
379
- provided_or_default_endpoint : & str ,
381
+ provided_endpoint : Option < & str > ,
380
382
) -> Result < Uri , crate :: Error > {
381
383
// per signal env var is not modified
382
384
if let Some ( endpoint) = env:: var ( signal_endpoint_var)
@@ -394,8 +396,13 @@ fn resolve_endpoint(
394
396
return Ok ( endpoint) ;
395
397
}
396
398
397
- // if neither works, we use the one provided in pipeline. If user never provides one, we will use the default one
398
- build_endpoint_uri ( provided_or_default_endpoint, signal_endpoint_path)
399
+ match provided_endpoint {
400
+ Some ( endpoint) => build_endpoint_uri ( endpoint, "" ) ,
401
+ None => build_endpoint_uri (
402
+ OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
403
+ signal_endpoint_path,
404
+ ) ,
405
+ }
399
406
}
400
407
401
408
#[ allow( clippy:: mutable_key_type) ] // http headers are not mutated
@@ -411,19 +418,22 @@ fn add_header_from_string(input: &str, headers: &mut HashMap<HeaderName, HeaderV
411
418
#[ cfg( test) ]
412
419
mod tests {
413
420
use crate :: exporter:: tests:: run_env_test;
414
- use crate :: { OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_TRACES_ENDPOINT } ;
421
+ use crate :: {
422
+ new_exporter, WithExportConfig , OTEL_EXPORTER_OTLP_ENDPOINT ,
423
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
424
+ } ;
415
425
416
- use super :: build_endpoint_uri;
426
+ use super :: { build_endpoint_uri, resolve_endpoint } ;
417
427
418
428
#[ test]
419
429
fn test_append_signal_path_to_generic_env ( ) {
420
430
run_env_test (
421
431
vec ! [ ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ] ,
422
432
|| {
423
- let endpoint = super :: resolve_endpoint (
433
+ let endpoint = resolve_endpoint (
424
434
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
425
435
"/v1/traces" ,
426
- "http://localhost:4317" ,
436
+ Some ( "http://localhost:4317" ) ,
427
437
)
428
438
. unwrap ( ) ;
429
439
assert_eq ! ( endpoint, "http://example.com/v1/traces" ) ;
@@ -439,7 +449,7 @@ mod tests {
439
449
let endpoint = super :: resolve_endpoint (
440
450
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
441
451
"/v1/traces" ,
442
- "http://localhost:4317" ,
452
+ Some ( "http://localhost:4317" ) ,
443
453
)
444
454
. unwrap ( ) ;
445
455
assert_eq ! ( endpoint, "http://example.com" ) ;
@@ -458,7 +468,7 @@ mod tests {
458
468
let endpoint = super :: resolve_endpoint (
459
469
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
460
470
"/v1/traces" ,
461
- "http://localhost:4317" ,
471
+ Some ( "http://localhost:4317" ) ,
462
472
)
463
473
. unwrap ( ) ;
464
474
assert_eq ! ( endpoint, "http://example.com" ) ;
@@ -469,10 +479,13 @@ mod tests {
469
479
#[ test]
470
480
fn test_use_provided_or_default_when_others_missing ( ) {
471
481
run_env_test ( vec ! [ ] , || {
472
- let endpoint =
473
- super :: resolve_endpoint ( "NON_EXISTENT_VAR" , "/v1/traces" , "http://localhost:4317" )
474
- . unwrap ( ) ;
475
- assert_eq ! ( endpoint, "http://localhost:4317/v1/traces" ) ;
482
+ let endpoint = super :: resolve_endpoint (
483
+ "NON_EXISTENT_VAR" ,
484
+ "/v1/traces" ,
485
+ Some ( "http://localhost:4317" ) ,
486
+ )
487
+ . unwrap ( ) ;
488
+ assert_eq ! ( endpoint, "http://localhost:4317/" ) ;
476
489
} ) ;
477
490
}
478
491
@@ -504,7 +517,7 @@ mod tests {
504
517
let endpoint = super :: resolve_endpoint (
505
518
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
506
519
"/v1/traces" ,
507
- "http://localhost:4317" ,
520
+ Some ( "http://localhost:4317" ) ,
508
521
)
509
522
. unwrap ( ) ;
510
523
assert_eq ! ( endpoint, "http://example.com/v1/traces" ) ;
@@ -518,7 +531,7 @@ mod tests {
518
531
let result = super :: resolve_endpoint (
519
532
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
520
533
"/v1/traces" ,
521
- "-*/*-/*-//-/-/yet-another-invalid-uri" ,
534
+ Some ( "-*/*-/*-//-/-/yet-another-invalid-uri" ) ,
522
535
) ;
523
536
assert ! ( result. is_err( ) ) ;
524
537
// You may also want to assert on the specific error type if applicable
@@ -610,4 +623,37 @@ mod tests {
610
623
}
611
624
}
612
625
}
626
+
627
+ #[ test]
628
+ fn test_http_exporter_endpoint ( ) {
629
+ // default endpoint should add signal path
630
+ run_env_test ( vec ! [ ] , || {
631
+ let exporter = new_exporter ( ) . http ( ) ;
632
+
633
+ let url = resolve_endpoint (
634
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
635
+ "/v1/traces" ,
636
+ exporter. exporter_config . endpoint . as_deref ( ) ,
637
+ )
638
+ . unwrap ( ) ;
639
+
640
+ assert_eq ! ( url, "http://localhost:4318/v1/traces" ) ;
641
+ } ) ;
642
+
643
+ // if builder endpoint is set, it should not add signal path
644
+ run_env_test ( vec ! [ ] , || {
645
+ let exporter = new_exporter ( )
646
+ . http ( )
647
+ . with_endpoint ( "http://localhost:4318/v1/tracesbutnotreally" ) ;
648
+
649
+ let url = resolve_endpoint (
650
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
651
+ "/v1/traces" ,
652
+ exporter. exporter_config . endpoint . as_deref ( ) ,
653
+ )
654
+ . unwrap ( ) ;
655
+
656
+ assert_eq ! ( url, "http://localhost:4318/v1/tracesbutnotreally" ) ;
657
+ } ) ;
658
+ }
613
659
}
0 commit comments