Skip to content

Commit a2f198f

Browse files
committed
fix lints
1 parent c45c840 commit a2f198f

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

opentelemetry-jaeger-propagator/src/propagator.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use opentelemetry::propagation::PropagationError;
12
use opentelemetry::{
23
global::{self, Error},
34
propagation::{text_map_propagator::FieldIter, Extractor, Injector, TextMapPropagator},
@@ -6,7 +7,6 @@ use opentelemetry::{
67
};
78
use std::borrow::Cow;
89
use std::str::FromStr;
9-
use opentelemetry::propagation::PropagationError;
1010

1111
const JAEGER_HEADER: &str = "uber-trace-id";
1212
const JAEGER_BAGGAGE_PREFIX: &str = "uberctx-";
@@ -86,14 +86,24 @@ impl Propagator {
8686
return None;
8787
}
8888

89-
// extract trace id
90-
let trace_id = self.extract_trace_id(parts[0])?;
91-
let span_id = self.extract_span_id(parts[1])?;
92-
// Ignore parent span id since it's deprecated.
93-
let flags = self.extract_trace_flags(parts[3])?;
94-
let state = self.extract_trace_state(extractor)?;
95-
96-
Some(SpanContext::new(trace_id, span_id, flags, true, state))
89+
match (
90+
self.extract_trace_id(parts[0]),
91+
self.extract_span_id(parts[1]),
92+
// Ignore parent span id since it's deprecated.
93+
self.extract_trace_flags(parts[3]),
94+
self.extract_trace_state(extractor),
95+
) {
96+
(Ok(trace_id), Ok(span_id), Ok(flags), Ok(state)) => {
97+
Some(SpanContext::new(trace_id, span_id, flags, true, state))
98+
}
99+
_ => {
100+
global::handle_error(Error::Propagation(PropagationError::extract(
101+
"invalid jaeger header format",
102+
"JaegerPropagator",
103+
)));
104+
None
105+
}
106+
}
97107
}
98108

99109
/// Extract trace id from the header.
@@ -193,7 +203,7 @@ impl TextMapPropagator for Propagator {
193203
fn extract_with_context(&self, cx: &Context, extractor: &dyn Extractor) -> Context {
194204
self.extract_span_context(extractor)
195205
.map(|sc| cx.with_remote_span_context(sc))
196-
.unwrap_or_else(|_| cx.clone())
206+
.unwrap_or_else(|| cx.clone())
197207
}
198208

199209
fn fields(&self) -> FieldIter<'_> {
@@ -435,7 +445,7 @@ mod tests {
435445
);
436446
assert_eq!(
437447
propagator_with_custom_header.extract_span_context(&map),
438-
Ok(SpanContext::new(
448+
Some(SpanContext::new(
439449
TraceId::from_hex("12345").unwrap(),
440450
SpanId::from_hex("54321").unwrap(),
441451
TRACE_FLAG_DEBUG | TraceFlags::SAMPLED,
@@ -452,7 +462,7 @@ mod tests {
452462
);
453463
assert_eq!(
454464
propagator_with_custom_header.extract_span_context(&map),
455-
Ok(SpanContext::new(
465+
Some(SpanContext::new(
456466
TraceId::from_hex("12345").unwrap(),
457467
SpanId::from_hex("54321").unwrap(),
458468
TRACE_FLAG_DEBUG | TraceFlags::SAMPLED,
@@ -468,7 +478,7 @@ mod tests {
468478
);
469479
assert_eq!(
470480
propagator_with_custom_header.extract_span_context(&map),
471-
Err(())
481+
None,
472482
);
473483

474484
map.clear();
@@ -478,7 +488,7 @@ mod tests {
478488
);
479489
assert_eq!(
480490
propagator_with_custom_header.extract_span_context(&map),
481-
Err(())
491+
None,
482492
);
483493

484494
map.clear();
@@ -488,7 +498,7 @@ mod tests {
488498
);
489499
assert_eq!(
490500
propagator_with_custom_header.extract_span_context(&map),
491-
Err(())
501+
None,
492502
);
493503

494504
map.clear();
@@ -498,7 +508,7 @@ mod tests {
498508
);
499509
assert_eq!(
500510
propagator_with_custom_header.extract_span_context(&map),
501-
Err(())
511+
None,
502512
);
503513

504514
map.clear();
@@ -511,7 +521,7 @@ mod tests {
511521
map.set(&too_long_baggage_key, "baggage_value".to_owned());
512522
assert_eq!(
513523
propagator_with_custom_header.extract_span_context(&map),
514-
Err(())
524+
None,
515525
);
516526
}
517527

opentelemetry-sdk/src/propagation/baggage.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,31 @@ impl TextMapPropagator for BaggagePropagator {
101101
{
102102
let mut iter = name_and_value.split('=');
103103
if let (Some(name), Some(value)) = (iter.next(), iter.next()) {
104-
let name = percent_decode_str(name).decode_utf8().map_err(|_| ())?;
105-
let value = percent_decode_str(value).decode_utf8().map_err(|_| ())?;
104+
let decode_name = percent_decode_str(name).decode_utf8();
105+
let decode_value = percent_decode_str(value).decode_utf8();
106106

107-
// Here we don't store the first ; into baggage since it should be treated
108-
// as separator rather part of metadata
109-
let decoded_props = props
110-
.iter()
111-
.flat_map(|prop| percent_decode_str(prop).decode_utf8())
112-
.map(|prop| prop.trim().to_string())
113-
.collect::<Vec<String>>()
114-
.join(";"); // join with ; because we deleted all ; when calling split above
107+
if let (Ok(name), Ok(value)) = (decode_name, decode_value) {
108+
// Here we don't store the first ; into baggage since it should be treated
109+
// as separator rather part of metadata
110+
let decoded_props = props
111+
.iter()
112+
.flat_map(|prop| percent_decode_str(prop).decode_utf8())
113+
.map(|prop| prop.trim().to_string())
114+
.collect::<Vec<String>>()
115+
.join(";"); // join with ; because we deleted all ; when calling split above
115116

116-
Some(KeyValueMetadata::new(
117-
name.trim().to_owned(),
118-
value.trim().to_string(),
119-
decoded_props.as_str(),
120-
))
117+
Some(KeyValueMetadata::new(
118+
name.trim().to_owned(),
119+
value.trim().to_string(),
120+
decoded_props.as_str(),
121+
))
122+
} else {
123+
global::handle_error(PropagationError::extract(
124+
"invalid UTF8 string in key values",
125+
"BaggagePropagator",
126+
));
127+
None
128+
}
121129
} else {
122130
global::handle_error(PropagationError::extract(
123131
"invalid baggage key-value format",

opentelemetry-sdk/src/propagation/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ mod baggage;
33
mod trace_context;
44

55
pub use baggage::BaggagePropagator;
6-
use std::fmt::Display;
76
pub use trace_context::TraceContextPropagator;
8-
9-

opentelemetry/src/global/error_handler.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::sync::RwLock;
55
use crate::logs::LogError;
66
#[cfg(feature = "metrics")]
77
use crate::metrics::MetricsError;
8+
use crate::propagation::PropagationError;
89
#[cfg(feature = "trace")]
910
use crate::trace::TraceError;
1011
use once_cell::sync::Lazy;
11-
use crate::propagation::PropagationError;
1212

1313
static GLOBAL_ERROR_HANDLER: Lazy<RwLock<Option<ErrorHandler>>> = Lazy::new(|| RwLock::new(None));
1414

@@ -32,8 +32,9 @@ pub enum Error {
3232
#[error(transparent)]
3333
/// Failed to export logs.
3434
Log(#[from] LogError),
35-
35+
3636
#[error(transparent)]
37+
/// Error happens when injecting and extracting information using propagators.
3738
Propagation(#[from] PropagationError),
3839

3940
#[error("{0}")]
@@ -65,7 +66,9 @@ pub fn handle_error<T: Into<Error>>(err: T) {
6566
#[cfg(feature = "logs")]
6667
#[cfg_attr(docsrs, doc(cfg(feature = "logs")))]
6768
Error::Log(err) => eprintln!("OpenTelemetry log error occurred. {}", err),
68-
Error::Propagation(err) => eprintln!("OpenTelemetry propagation error occurred. {}", err),
69+
Error::Propagation(err) => {
70+
eprintln!("OpenTelemetry propagation error occurred. {}", err)
71+
}
6972
Error::Other(err_msg) => eprintln!("OpenTelemetry error occurred. {}", err_msg),
7073
},
7174
}

opentelemetry/src/propagation/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
//! [`Context`]: crate::Context
2121
2222
use std::collections::HashMap;
23-
use std::fmt::Display;
2423
use thiserror::Error;
2524

2625
pub mod composite;
@@ -74,7 +73,8 @@ pub struct PropagationError {
7473
ops: &'static str,
7574
}
7675

77-
impl PropagationError{
76+
impl PropagationError {
77+
/// Error happens when extracting information
7878
pub fn extract(message: &'static str, propagator_name: &'static str) -> Self {
7979
PropagationError {
8080
message,
@@ -83,11 +83,12 @@ impl PropagationError{
8383
}
8484
}
8585

86+
/// Error happens when extracting information
8687
pub fn inject(message: &'static str, propagator_name: &'static str) -> Self {
8788
PropagationError {
8889
message,
8990
propagator_name,
90-
ops: "inject"
91+
ops: "inject",
9192
}
9293
}
9394
}

0 commit comments

Comments
 (0)