Skip to content

Commit 04fa6d5

Browse files
authored
fix: with_collector_endpoint is error prune. (#428)
Now we will store the result of try_from function until we init the uploader. If the result is error, we wrap it into TraceError and return.
1 parent b2ab799 commit 04fa6d5

File tree

1 file changed

+44
-4
lines changed
  • opentelemetry-jaeger/src/exporter

1 file changed

+44
-4
lines changed

opentelemetry-jaeger/src/exporter/mod.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl trace::SpanExporter for Exporter {
129129
pub struct PipelineBuilder {
130130
agent_endpoint: Vec<net::SocketAddr>,
131131
#[cfg(any(feature = "collector_client", feature = "wasm_collector_client"))]
132-
collector_endpoint: Option<http::Uri>,
132+
collector_endpoint: Option<Result<http::Uri, http::uri::InvalidUri>>,
133133
#[cfg(any(feature = "collector_client", feature = "wasm_collector_client"))]
134134
collector_username: Option<String>,
135135
#[cfg(any(feature = "collector_client", feature = "wasm_collector_client"))]
@@ -206,9 +206,12 @@ impl PipelineBuilder {
206206
pub fn with_collector_endpoint<T>(self, collector_endpoint: T) -> Self
207207
where
208208
http::Uri: core::convert::TryFrom<T>,
209+
<http::Uri as core::convert::TryFrom<T>>::Error: Into<http::uri::InvalidUri>,
209210
{
210211
PipelineBuilder {
211-
collector_endpoint: core::convert::TryFrom::try_from(collector_endpoint).ok(),
212+
collector_endpoint: Some(
213+
core::convert::TryFrom::try_from(collector_endpoint).map_err(Into::into),
214+
),
212215
..self
213216
}
214217
}
@@ -314,7 +317,11 @@ impl PipelineBuilder {
314317

315318
#[cfg(feature = "collector_client")]
316319
fn init_uploader(self) -> Result<(Process, uploader::BatchUploader), TraceError> {
317-
if let Some(collector_endpoint) = self.collector_endpoint {
320+
if let Some(collector_endpoint) = self
321+
.collector_endpoint
322+
.transpose()
323+
.map_err::<Error, _>(Into::into)?
324+
{
318325
#[cfg(all(
319326
not(feature = "isahc_collector_client"),
320327
not(feature = "surf_collector_client"),
@@ -402,7 +409,11 @@ impl PipelineBuilder {
402409

403410
#[cfg(all(feature = "wasm_collector_client", not(feature = "collector_client")))]
404411
fn init_uploader(self) -> Result<(Process, uploader::BatchUploader), TraceError> {
405-
if let Some(collector_endpoint) = self.collector_endpoint {
412+
if let Some(collector_endpoint) = self
413+
.collector_endpoint
414+
.transpose()
415+
.map_err::<Error, _>(Into::into)?
416+
{
406417
let collector = CollectorAsyncClientHttp::new(
407418
collector_endpoint,
408419
self.collector_username,
@@ -680,6 +691,11 @@ pub enum Error {
680691
feature = "reqwest_blocking_collector_client"
681692
))]
682693
ReqwestClientError(#[from] reqwest::Error),
694+
695+
/// invalid collector uri is provided.
696+
#[error("collector uri is invalid, {0}")]
697+
#[cfg(any(feature = "collector_client", feature = "wasm_collector_client"))]
698+
InvalidUri(#[from] http::uri::InvalidUri),
683699
}
684700

685701
impl ExportError for Error {
@@ -737,6 +753,30 @@ mod collector_client_tests {
737753

738754
Ok(())
739755
}
756+
757+
#[test]
758+
#[cfg(any(
759+
feature = "isahc_collector_client",
760+
feature = "surf_collector_client",
761+
feature = "reqwest_collector_client",
762+
feature = "reqwest_blocking_collector_client"
763+
))]
764+
fn test_set_collector_endpoint() {
765+
let invalid_uri = new_pipeline()
766+
.with_collector_endpoint("127.0.0.1:14268/api/traces")
767+
.init_uploader();
768+
assert!(invalid_uri.is_err());
769+
assert_eq!(
770+
format!("{:?}", invalid_uri.err().unwrap()),
771+
"ExportFailed(InvalidUri(InvalidUri(InvalidFormat)))"
772+
);
773+
774+
let valid_uri = new_pipeline()
775+
.with_collector_endpoint("http://127.0.0.1:14268/api/traces")
776+
.init_uploader();
777+
778+
assert!(valid_uri.is_ok());
779+
}
740780
}
741781

742782
#[cfg(test)]

0 commit comments

Comments
 (0)