Skip to content

Commit 99bba7b

Browse files
authored
Merge branch 'main' into bug/1509-defaults-for-grpc-incorrect
2 parents 4b478c8 + 4c22cf6 commit 99bba7b

File tree

1 file changed

+28
-5
lines changed
  • opentelemetry-otlp/src/exporter/http

1 file changed

+28
-5
lines changed

opentelemetry-otlp/src/exporter/http/mod.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ impl OtlpHttpClient {
270270
}
271271
}
272272

273+
fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, crate::Error> {
274+
let path = if endpoint.ends_with('/') && path.starts_with('/') {
275+
path.strip_prefix('/').unwrap()
276+
} else {
277+
path
278+
};
279+
format!("{endpoint}{path}").parse().map_err(From::from)
280+
}
281+
273282
// see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp
274283
fn resolve_endpoint(
275284
signal_endpoint_var: &str,
@@ -287,15 +296,13 @@ fn resolve_endpoint(
287296
// if signal env var is not set, then we check if the OTEL_EXPORTER_OTLP_ENDPOINT is set
288297
if let Some(endpoint) = env::var(OTEL_EXPORTER_OTLP_ENDPOINT)
289298
.ok()
290-
.and_then(|s| format!("{s}{signal_endpoint_path}").parse().ok())
299+
.and_then(|s| build_endpoint_uri(&s, signal_endpoint_path).ok())
291300
{
292301
return Ok(endpoint);
293302
}
294303

295-
// if neither works, we use the one provided in pipeline. If user never provide one, we will use the default one
296-
format!("{provided_or_default_endpoint}{signal_endpoint_path}")
297-
.parse()
298-
.map_err(From::from)
304+
// if neither works, we use the one provided in pipeline. If user never provides one, we will use the default one
305+
build_endpoint_uri(provided_or_default_endpoint, signal_endpoint_path)
299306
}
300307

301308
#[allow(clippy::mutable_key_type)] // http headers are not mutated
@@ -313,6 +320,8 @@ mod tests {
313320
use crate::exporter::tests::run_env_test;
314321
use crate::{OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT};
315322

323+
use super::build_endpoint_uri;
324+
316325
#[test]
317326
fn test_append_signal_path_to_generic_env() {
318327
run_env_test(
@@ -374,6 +383,20 @@ mod tests {
374383
});
375384
}
376385

386+
#[test]
387+
fn test_build_endpoint_uri() {
388+
let uri = build_endpoint_uri("https://example.com", "/v1/traces").unwrap();
389+
assert_eq!(uri, "https://example.com/v1/traces");
390+
391+
// Should be no duplicate slahes:
392+
let uri = build_endpoint_uri("https://example.com/", "/v1/traces").unwrap();
393+
assert_eq!(uri, "https://example.com/v1/traces");
394+
395+
// Append paths properly:
396+
let uri = build_endpoint_uri("https://example.com/additional/path/", "/v1/traces").unwrap();
397+
assert_eq!(uri, "https://example.com/additional/path/v1/traces");
398+
}
399+
377400
#[test]
378401
fn test_invalid_uri_in_signal_env_falls_back_to_generic_env() {
379402
run_env_test(

0 commit comments

Comments
 (0)