1+ use opentelemetry:: propagation:: PropagationError ;
12use opentelemetry:: {
23 global:: { self , Error } ,
34 propagation:: { text_map_propagator:: FieldIter , Extractor , Injector , TextMapPropagator } ,
@@ -69,7 +70,7 @@ impl Propagator {
6970 }
7071
7172 /// Extract span context from header value
72- fn extract_span_context ( & self , extractor : & dyn Extractor ) -> Result < SpanContext , ( ) > {
73+ fn extract_span_context ( & self , extractor : & dyn Extractor ) -> Option < SpanContext > {
7374 let mut header_value = Cow :: from ( extractor. get ( self . header_name ) . unwrap_or ( "" ) ) ;
7475 // if there is no :, it means header_value could be encoded as url, try decode first
7576 if !header_value. contains ( ':' ) {
@@ -78,17 +79,31 @@ impl Propagator {
7879
7980 let parts = header_value. split_terminator ( ':' ) . collect :: < Vec < & str > > ( ) ;
8081 if parts. len ( ) != 4 {
81- return Err ( ( ) ) ;
82+ global:: handle_error ( Error :: Propagation ( PropagationError :: extract (
83+ "invalid jaeger header format" ,
84+ "JaegerPropagator" ,
85+ ) ) ) ;
86+ return None ;
8287 }
8388
84- // extract trace id
85- let trace_id = self . extract_trace_id ( parts[ 0 ] ) ?;
86- let span_id = self . extract_span_id ( parts[ 1 ] ) ?;
87- // Ignore parent span id since it's deprecated.
88- let flags = self . extract_trace_flags ( parts[ 3 ] ) ?;
89- let state = self . extract_trace_state ( extractor) ?;
90-
91- Ok ( 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+ }
92107 }
93108
94109 /// Extract trace id from the header.
@@ -188,7 +203,7 @@ impl TextMapPropagator for Propagator {
188203 fn extract_with_context ( & self , cx : & Context , extractor : & dyn Extractor ) -> Context {
189204 self . extract_span_context ( extractor)
190205 . map ( |sc| cx. with_remote_span_context ( sc) )
191- . unwrap_or_else ( |_ | cx. clone ( ) )
206+ . unwrap_or_else ( || cx. clone ( ) )
192207 }
193208
194209 fn fields ( & self ) -> FieldIter < ' _ > {
@@ -430,7 +445,7 @@ mod tests {
430445 ) ;
431446 assert_eq ! (
432447 propagator_with_custom_header. extract_span_context( & map) ,
433- Ok ( SpanContext :: new(
448+ Some ( SpanContext :: new(
434449 TraceId :: from_hex( "12345" ) . unwrap( ) ,
435450 SpanId :: from_hex( "54321" ) . unwrap( ) ,
436451 TRACE_FLAG_DEBUG | TraceFlags :: SAMPLED ,
@@ -447,7 +462,7 @@ mod tests {
447462 ) ;
448463 assert_eq ! (
449464 propagator_with_custom_header. extract_span_context( & map) ,
450- Ok ( SpanContext :: new(
465+ Some ( SpanContext :: new(
451466 TraceId :: from_hex( "12345" ) . unwrap( ) ,
452467 SpanId :: from_hex( "54321" ) . unwrap( ) ,
453468 TRACE_FLAG_DEBUG | TraceFlags :: SAMPLED ,
@@ -463,7 +478,7 @@ mod tests {
463478 ) ;
464479 assert_eq ! (
465480 propagator_with_custom_header. extract_span_context( & map) ,
466- Err ( ( ) )
481+ None ,
467482 ) ;
468483
469484 map. clear ( ) ;
@@ -473,7 +488,7 @@ mod tests {
473488 ) ;
474489 assert_eq ! (
475490 propagator_with_custom_header. extract_span_context( & map) ,
476- Err ( ( ) )
491+ None ,
477492 ) ;
478493
479494 map. clear ( ) ;
@@ -483,7 +498,7 @@ mod tests {
483498 ) ;
484499 assert_eq ! (
485500 propagator_with_custom_header. extract_span_context( & map) ,
486- Err ( ( ) )
501+ None ,
487502 ) ;
488503
489504 map. clear ( ) ;
@@ -493,7 +508,7 @@ mod tests {
493508 ) ;
494509 assert_eq ! (
495510 propagator_with_custom_header. extract_span_context( & map) ,
496- Err ( ( ) )
511+ None ,
497512 ) ;
498513
499514 map. clear ( ) ;
@@ -506,7 +521,7 @@ mod tests {
506521 map. set ( & too_long_baggage_key, "baggage_value" . to_owned ( ) ) ;
507522 assert_eq ! (
508523 propagator_with_custom_header. extract_span_context( & map) ,
509- Err ( ( ) )
524+ None ,
510525 ) ;
511526 }
512527
0 commit comments