1
+ use opentelemetry:: propagation:: PropagationError ;
1
2
use opentelemetry:: {
2
3
global:: { self , Error } ,
3
4
propagation:: { text_map_propagator:: FieldIter , Extractor , Injector , TextMapPropagator } ,
@@ -69,7 +70,7 @@ impl Propagator {
69
70
}
70
71
71
72
/// 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 > {
73
74
let mut header_value = Cow :: from ( extractor. get ( self . header_name ) . unwrap_or ( "" ) ) ;
74
75
// if there is no :, it means header_value could be encoded as url, try decode first
75
76
if !header_value. contains ( ':' ) {
@@ -78,17 +79,31 @@ impl Propagator {
78
79
79
80
let parts = header_value. split_terminator ( ':' ) . collect :: < Vec < & str > > ( ) ;
80
81
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 ;
82
87
}
83
88
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
+ }
92
107
}
93
108
94
109
/// Extract trace id from the header.
@@ -188,7 +203,7 @@ impl TextMapPropagator for Propagator {
188
203
fn extract_with_context ( & self , cx : & Context , extractor : & dyn Extractor ) -> Context {
189
204
self . extract_span_context ( extractor)
190
205
. map ( |sc| cx. with_remote_span_context ( sc) )
191
- . unwrap_or_else ( |_ | cx. clone ( ) )
206
+ . unwrap_or_else ( || cx. clone ( ) )
192
207
}
193
208
194
209
fn fields ( & self ) -> FieldIter < ' _ > {
@@ -430,7 +445,7 @@ mod tests {
430
445
) ;
431
446
assert_eq ! (
432
447
propagator_with_custom_header. extract_span_context( & map) ,
433
- Ok ( SpanContext :: new(
448
+ Some ( SpanContext :: new(
434
449
TraceId :: from_hex( "12345" ) . unwrap( ) ,
435
450
SpanId :: from_hex( "54321" ) . unwrap( ) ,
436
451
TRACE_FLAG_DEBUG | TraceFlags :: SAMPLED ,
@@ -447,7 +462,7 @@ mod tests {
447
462
) ;
448
463
assert_eq ! (
449
464
propagator_with_custom_header. extract_span_context( & map) ,
450
- Ok ( SpanContext :: new(
465
+ Some ( SpanContext :: new(
451
466
TraceId :: from_hex( "12345" ) . unwrap( ) ,
452
467
SpanId :: from_hex( "54321" ) . unwrap( ) ,
453
468
TRACE_FLAG_DEBUG | TraceFlags :: SAMPLED ,
@@ -463,7 +478,7 @@ mod tests {
463
478
) ;
464
479
assert_eq ! (
465
480
propagator_with_custom_header. extract_span_context( & map) ,
466
- Err ( ( ) )
481
+ None ,
467
482
) ;
468
483
469
484
map. clear ( ) ;
@@ -473,7 +488,7 @@ mod tests {
473
488
) ;
474
489
assert_eq ! (
475
490
propagator_with_custom_header. extract_span_context( & map) ,
476
- Err ( ( ) )
491
+ None ,
477
492
) ;
478
493
479
494
map. clear ( ) ;
@@ -483,7 +498,7 @@ mod tests {
483
498
) ;
484
499
assert_eq ! (
485
500
propagator_with_custom_header. extract_span_context( & map) ,
486
- Err ( ( ) )
501
+ None ,
487
502
) ;
488
503
489
504
map. clear ( ) ;
@@ -493,7 +508,7 @@ mod tests {
493
508
) ;
494
509
assert_eq ! (
495
510
propagator_with_custom_header. extract_span_context( & map) ,
496
- Err ( ( ) )
511
+ None ,
497
512
) ;
498
513
499
514
map. clear ( ) ;
@@ -506,7 +521,7 @@ mod tests {
506
521
map. set ( & too_long_baggage_key, "baggage_value" . to_owned ( ) ) ;
507
522
assert_eq ! (
508
523
propagator_with_custom_header. extract_span_context( & map) ,
509
- Err ( ( ) )
524
+ None ,
510
525
) ;
511
526
}
512
527
0 commit comments