Skip to content

fix: use default endpoint for endpoint when provided empty string #3000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ fn resolve_http_endpoint(
provided_endpoint: Option<&str>,
) -> Result<Uri, ExporterBuildError> {
// programmatic configuration overrides any value set via environment variables
if let Some(provider_endpoint) = provided_endpoint {
if let Some(provider_endpoint) = provided_endpoint.filter(|s| !s.is_empty()) {
provider_endpoint
.parse()
.map_err(|er: http::uri::InvalidUri| {
Expand Down Expand Up @@ -528,6 +528,15 @@ mod tests {
);
}

#[test]
fn test_use_default_when_empty_string_for_option() {
run_env_test(vec![], || {
let endpoint =
super::resolve_http_endpoint("non_existent_var", "/v1/traces", Some("")).unwrap();
assert_eq!(endpoint, "http://localhost:4318/v1/traces");
});
}

#[test]
fn test_use_default_when_others_missing() {
run_env_test(vec![], || {
Expand Down
13 changes: 12 additions & 1 deletion opentelemetry-otlp/src/exporter/tonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl TonicExporterBuilder {
// If users for some reason want to use a custom path, they can use env var or builder to pass it
//
// programmatic configuration overrides any value set via environment variables
if let Some(endpoint) = provided_endpoint {
if let Some(endpoint) = provided_endpoint.filter(|s| !s.is_empty()) {
endpoint
} else if let Ok(endpoint) = env::var(default_endpoint_var) {
endpoint
Expand Down Expand Up @@ -666,4 +666,15 @@ mod tests {
assert_eq!(url, "http://localhost:4317");
});
}

#[test]
fn test_use_default_when_empty_string_for_option() {
run_env_test(vec![], || {
let url = TonicExporterBuilder::resolve_endpoint(
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
Some(String::new()),
);
assert_eq!(url, "http://localhost:4317");
});
}
}