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+ } ;
25use crate :: {
36 ExportConfig , Protocol , OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_HEADERS ,
47 OTEL_EXPORTER_OTLP_TIMEOUT ,
@@ -150,7 +153,7 @@ impl HttpExporterBuilder {
150153 signal_timeout_var : & str ,
151154 signal_http_headers_var : & str ,
152155 ) -> Result < OtlpHttpClient , crate :: Error > {
153- let endpoint = resolve_endpoint (
156+ let endpoint = resolve_http_endpoint (
154157 signal_endpoint_var,
155158 signal_endpoint_path,
156159 self . exporter_config . endpoint . as_str ( ) ,
@@ -373,10 +376,10 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, crate::Error> {
373376}
374377
375378// 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 (
377380 signal_endpoint_var : & str ,
378381 signal_endpoint_path : & str ,
379- provided_or_default_endpoint : & str ,
382+ provided_endpoint : & str ,
380383) -> Result < Uri , crate :: Error > {
381384 // per signal env var is not modified
382385 if let Some ( endpoint) = env:: var ( signal_endpoint_var)
@@ -394,8 +397,14 @@ fn resolve_endpoint(
394397 return Ok ( endpoint) ;
395398 }
396399
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+ }
399408}
400409
401410#[ 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
411420#[ cfg( test) ]
412421mod tests {
413422 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+ } ;
415427
416- use super :: build_endpoint_uri;
428+ use super :: { build_endpoint_uri, resolve_http_endpoint } ;
417429
418430 #[ test]
419431 fn test_append_signal_path_to_generic_env ( ) {
420432 run_env_test (
421433 vec ! [ ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ] ,
422434 || {
423- let endpoint = super :: resolve_endpoint (
435+ let endpoint = resolve_http_endpoint (
424436 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
425437 "/v1/traces" ,
426438 "http://localhost:4317" ,
@@ -436,7 +448,7 @@ mod tests {
436448 run_env_test (
437449 vec ! [ ( OTEL_EXPORTER_OTLP_TRACES_ENDPOINT , "http://example.com" ) ] ,
438450 || {
439- let endpoint = super :: resolve_endpoint (
451+ let endpoint = super :: resolve_http_endpoint (
440452 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
441453 "/v1/traces" ,
442454 "http://localhost:4317" ,
@@ -455,7 +467,7 @@ mod tests {
455467 ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://wrong.com" ) ,
456468 ] ,
457469 || {
458- let endpoint = super :: resolve_endpoint (
470+ let endpoint = super :: resolve_http_endpoint (
459471 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
460472 "/v1/traces" ,
461473 "http://localhost:4317" ,
@@ -469,10 +481,13 @@ mod tests {
469481 #[ test]
470482 fn test_use_provided_or_default_when_others_missing ( ) {
471483 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/" ) ;
476491 } ) ;
477492 }
478493
@@ -501,7 +516,7 @@ mod tests {
501516 ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ,
502517 ] ,
503518 || {
504- let endpoint = super :: resolve_endpoint (
519+ let endpoint = super :: resolve_http_endpoint (
505520 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
506521 "/v1/traces" ,
507522 "http://localhost:4317" ,
@@ -515,7 +530,7 @@ mod tests {
515530 #[ test]
516531 fn test_all_invalid_urls_falls_back_to_error ( ) {
517532 run_env_test ( vec ! [ ] , || {
518- let result = super :: resolve_endpoint (
533+ let result = super :: resolve_http_endpoint (
519534 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ,
520535 "/v1/traces" ,
521536 "-*/*-/*-//-/-/yet-another-invalid-uri" ,
@@ -610,4 +625,37 @@ mod tests {
610625 }
611626 }
612627 }
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+ }
613661}
0 commit comments