1
- use super :: { default_headers, default_protocol, parse_header_string} ;
1
+ use super :: {
2
+ default_headers, default_protocol, parse_header_string,
3
+ OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
4
+ } ;
2
5
use crate :: {
3
6
ExportConfig , Protocol , OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_HEADERS ,
4
7
OTEL_EXPORTER_OTLP_TIMEOUT ,
@@ -150,7 +153,7 @@ impl HttpExporterBuilder {
150
153
signal_timeout_var : & str ,
151
154
signal_http_headers_var : & str ,
152
155
) -> Result < OtlpHttpClient , crate :: Error > {
153
- let endpoint = resolve_endpoint (
156
+ let endpoint = resolve_http_endpoint (
154
157
signal_endpoint_var,
155
158
signal_endpoint_path,
156
159
self . exporter_config . endpoint . as_str ( ) ,
@@ -373,10 +376,10 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, crate::Error> {
373
376
}
374
377
375
378
// see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp
376
- fn resolve_endpoint (
379
+ fn resolve_http_endpoint (
377
380
signal_endpoint_var : & str ,
378
381
signal_endpoint_path : & str ,
379
- provided_or_default_endpoint : & str ,
382
+ provided_endpoint : & str ,
380
383
) -> Result < Uri , crate :: Error > {
381
384
// per signal env var is not modified
382
385
if let Some ( endpoint) = env:: var ( signal_endpoint_var)
@@ -394,8 +397,14 @@ fn resolve_endpoint(
394
397
return Ok ( endpoint) ;
395
398
}
396
399
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)
400
+ if provided_endpoint. is_empty ( ) {
401
+ build_endpoint_uri (
402
+ OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
403
+ signal_endpoint_path,
404
+ )
405
+ } else {
406
+ provided_endpoint. parse ( ) . map_err ( From :: from)
407
+ }
399
408
}
400
409
401
410
#[ allow( clippy:: mutable_key_type) ] // http headers are not mutated
@@ -411,16 +420,19 @@ fn add_header_from_string(input: &str, headers: &mut HashMap<HeaderName, HeaderV
411
420
#[ cfg( test) ]
412
421
mod tests {
413
422
use crate :: exporter:: tests:: run_env_test;
414
- use crate :: { OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_TRACES_ENDPOINT } ;
423
+ use crate :: {
424
+ new_exporter, WithExportConfig , OTEL_EXPORTER_OTLP_ENDPOINT ,
425
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
426
+ } ;
415
427
416
- use super :: build_endpoint_uri;
428
+ use super :: { build_endpoint_uri, resolve_http_endpoint } ;
417
429
418
430
#[ test]
419
431
fn test_append_signal_path_to_generic_env ( ) {
420
432
run_env_test (
421
433
vec ! [ ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ] ,
422
434
|| {
423
- let endpoint = super :: resolve_endpoint (
435
+ let endpoint = resolve_http_endpoint (
424
436
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
425
437
"/v1/traces" ,
426
438
"http://localhost:4317" ,
@@ -436,7 +448,7 @@ mod tests {
436
448
run_env_test (
437
449
vec ! [ ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "http://example.com" ) ] ,
438
450
|| {
439
- let endpoint = super :: resolve_endpoint (
451
+ let endpoint = super :: resolve_http_endpoint (
440
452
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
441
453
"/v1/traces" ,
442
454
"http://localhost:4317" ,
@@ -455,7 +467,7 @@ mod tests {
455
467
( OTEL_EXPORTER_OTLP_ENDPOINT , "http://wrong.com" ) ,
456
468
] ,
457
469
|| {
458
- let endpoint = super :: resolve_endpoint (
470
+ let endpoint = super :: resolve_http_endpoint (
459
471
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
460
472
"/v1/traces" ,
461
473
"http://localhost:4317" ,
@@ -469,10 +481,13 @@ mod tests {
469
481
#[ test]
470
482
fn test_use_provided_or_default_when_others_missing ( ) {
471
483
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" ) ;
484
+ let endpoint = super :: resolve_http_endpoint (
485
+ "NON_EXISTENT_VAR" ,
486
+ "/v1/traces" ,
487
+ "http://localhost:4317" ,
488
+ )
489
+ . unwrap ( ) ;
490
+ assert_eq ! ( endpoint, "http://localhost:4317/" ) ;
476
491
} ) ;
477
492
}
478
493
@@ -501,7 +516,7 @@ mod tests {
501
516
( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ,
502
517
] ,
503
518
|| {
504
- let endpoint = super :: resolve_endpoint (
519
+ let endpoint = super :: resolve_http_endpoint (
505
520
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
506
521
"/v1/traces" ,
507
522
"http://localhost:4317" ,
@@ -515,7 +530,7 @@ mod tests {
515
530
#[ test]
516
531
fn test_all_invalid_urls_falls_back_to_error ( ) {
517
532
run_env_test ( vec ! [ ] , || {
518
- let result = super :: resolve_endpoint (
533
+ let result = super :: resolve_http_endpoint (
519
534
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
520
535
"/v1/traces" ,
521
536
"-*/*-/*-//-/-/yet-another-invalid-uri" ,
@@ -610,4 +625,37 @@ mod tests {
610
625
}
611
626
}
612
627
}
628
+
629
+ #[ test]
630
+ fn test_http_exporter_endpoint ( ) {
631
+ // default endpoint should add signal path
632
+ run_env_test ( vec ! [ ] , || {
633
+ let exporter = new_exporter ( ) . http ( ) ;
634
+
635
+ let url = resolve_http_endpoint (
636
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
637
+ "/v1/traces" ,
638
+ exporter. exporter_config . endpoint . as_str ( ) ,
639
+ )
640
+ . unwrap ( ) ;
641
+
642
+ assert_eq ! ( url, "http://localhost:4318/v1/traces" ) ;
643
+ } ) ;
644
+
645
+ // if builder endpoint is set, it should not add signal path
646
+ run_env_test ( vec ! [ ] , || {
647
+ let exporter = new_exporter ( )
648
+ . http ( )
649
+ . with_endpoint ( "http://localhost:4318/v1/tracesbutnotreally" ) ;
650
+
651
+ let url = resolve_http_endpoint (
652
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
653
+ "/v1/traces" ,
654
+ exporter. exporter_config . endpoint . as_str ( ) ,
655
+ )
656
+ . unwrap ( ) ;
657
+
658
+ assert_eq ! ( url, "http://localhost:4318/v1/tracesbutnotreally" ) ;
659
+ } ) ;
660
+ }
613
661
}
0 commit comments