Skip to content

Commit cab5565

Browse files
joonascijothomas
andauthored
fix: use default endpoint for endpoint when provided empty string (#3000)
Signed-off-by: Joonas Bergius <[email protected]> Co-authored-by: Cijo Thomas <[email protected]>
1 parent 6279060 commit cab5565

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ fn resolve_http_endpoint(
365365
provided_endpoint: Option<&str>,
366366
) -> Result<Uri, ExporterBuildError> {
367367
// programmatic configuration overrides any value set via environment variables
368-
if let Some(provider_endpoint) = provided_endpoint {
368+
if let Some(provider_endpoint) = provided_endpoint.filter(|s| !s.is_empty()) {
369369
provider_endpoint
370370
.parse()
371371
.map_err(|er: http::uri::InvalidUri| {
@@ -528,6 +528,15 @@ mod tests {
528528
);
529529
}
530530

531+
#[test]
532+
fn test_use_default_when_empty_string_for_option() {
533+
run_env_test(vec![], || {
534+
let endpoint =
535+
super::resolve_http_endpoint("non_existent_var", "/v1/traces", Some("")).unwrap();
536+
assert_eq!(endpoint, "http://localhost:4318/v1/traces");
537+
});
538+
}
539+
531540
#[test]
532541
fn test_use_default_when_others_missing() {
533542
run_env_test(vec![], || {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl TonicExporterBuilder {
225225
// If users for some reason want to use a custom path, they can use env var or builder to pass it
226226
//
227227
// programmatic configuration overrides any value set via environment variables
228-
if let Some(endpoint) = provided_endpoint {
228+
if let Some(endpoint) = provided_endpoint.filter(|s| !s.is_empty()) {
229229
endpoint
230230
} else if let Ok(endpoint) = env::var(default_endpoint_var) {
231231
endpoint
@@ -666,4 +666,15 @@ mod tests {
666666
assert_eq!(url, "http://localhost:4317");
667667
});
668668
}
669+
670+
#[test]
671+
fn test_use_default_when_empty_string_for_option() {
672+
run_env_test(vec![], || {
673+
let url = TonicExporterBuilder::resolve_endpoint(
674+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
675+
Some(String::new()),
676+
);
677+
assert_eq!(url, "http://localhost:4317");
678+
});
679+
}
669680
}

0 commit comments

Comments
 (0)