Skip to content

fix: add panic error when multiple HTTP features are enabled #3003

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 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ impl HttpExporterBuilder {
.unwrap(), // TODO: Return ExporterBuildError::ThreadSpawnFailed
) as Arc<dyn HttpClient>);
}
#[cfg(any(
all(feature = "hyper-client", feature = "reqwest-client"),
all(feature = "hyper-client", feature = "reqwest-blocking-client"),
all(feature = "reqwest-client", feature = "reqwest-blocking-client")
))]
{
return Err(ExporterBuildError::InternalFailure("Can't enable more than one HTTP client features simultaneously. Please choose only one: hyper-client, reqwest-client, or reqwest-blocking-client (default feature)".to_string()));
}
Comment on lines +164 to +171
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is a good idea.
Because of feature unification, if you depend transitively on a crate, it might enable features which you do not explicitely have

A consequence of this is that features should be additive. That is, enabling a feature should not disable functionality, and it should usually be safe to enable any combination of features. A feature should not introduce a SemVer-incompatible change

https://doc.rust-lang.org/cargo/reference/features.html#feature-unification

I think a cleaner idea would be to define an order of precedence between clients (for instance reqwest-client > hyper-client > reqwest-blocking-client) and use the client with the higher precendence if multiple features are enabled

}

let http_client = http_client.ok_or(ExporterBuildError::NoHttpClient)?;
Expand Down Expand Up @@ -741,4 +749,25 @@ mod tests {
assert_eq!(url, "http://localhost:4318/v1/tracesbutnotreally");
});
}

#[cfg(any(
all(feature = "hyper-client", feature = "reqwest-client"),
all(feature = "hyper-client", feature = "reqwest-blocking-client"),
all(feature = "reqwest-client", feature = "reqwest-blocking-client")
))]
#[test]
fn test_http_exporter_builder_returns_error_with_multiple_http_features() {
let mut builder = HttpExporterBuilder {
http_config: HttpConfig::default(),
exporter_config: crate::ExportConfig::default(),
};

let client = builder.build_client("ENV_VAR_1", "PATH", "ENV_VAR_2", "ENV_VAR_3");

assert!(client.is_err());
assert!(client
.unwrap_err()
.to_string()
.contains("Can't enable more than one HTTP client features simultaneously."))
}
}