1313//!
1414//! If `inject_encoding` is set to `B3Encoding::SingleHeader` then `b3` header is used to inject
1515//! and extract. Otherwise, separate headers are used to inject and extract.
16- use crate :: { api, api:: TraceContextExt } ;
17- use crate :: api:: TRACE_FLAG_DEFERRED ;
1816use crate :: api:: trace:: b3_propagator:: B3Encoding :: MultipleHeader ;
17+ use crate :: api:: TRACE_FLAG_DEFERRED ;
18+ use crate :: { api, api:: TraceContextExt } ;
1919
2020static B3_SINGLE_HEADER : & str = "b3" ;
2121/// As per spec, the multiple header should be case sensitive. But different protocol will use
@@ -82,8 +82,7 @@ impl B3Propagator {
8282 /// Extract trace id from hex encoded &str value.
8383 fn extract_trace_id ( & self , trace_id : & str ) -> Result < api:: TraceId , ( ) > {
8484 // Only allow lower case hex string
85- if trace_id. to_lowercase ( ) != trace_id ||
86- ( trace_id. len ( ) != 16 && trace_id. len ( ) != 32 ) {
85+ if trace_id. to_lowercase ( ) != trace_id || ( trace_id. len ( ) != 16 && trace_id. len ( ) != 32 ) {
8786 Err ( ( ) )
8887 } else {
8988 u128:: from_str_radix ( trace_id, 16 )
@@ -223,8 +222,9 @@ impl api::HttpTextFormat for B3Propagator {
223222
224223 carrier. set ( B3_SINGLE_HEADER , value) ;
225224 }
226- if self . inject_encoding . support ( & B3Encoding :: MultipleHeader ) ||
227- self . inject_encoding . support ( & B3Encoding :: UnSpecified ) {
225+ if self . inject_encoding . support ( & B3Encoding :: MultipleHeader )
226+ || self . inject_encoding . support ( & B3Encoding :: UnSpecified )
227+ {
228228 // if inject_encoding is Unspecified, default to use MultipleHeader
229229 carrier. set (
230230 B3_TRACE_ID_HEADER ,
@@ -247,8 +247,9 @@ impl api::HttpTextFormat for B3Propagator {
247247 if self . inject_encoding . support ( & B3Encoding :: SingleHeader ) {
248248 carrier. set ( B3_SINGLE_HEADER , flag. to_string ( ) )
249249 }
250- if self . inject_encoding . support ( & B3Encoding :: MultipleHeader ) ||
251- self . inject_encoding . support ( & B3Encoding :: UnSpecified ) {
250+ if self . inject_encoding . support ( & B3Encoding :: MultipleHeader )
251+ || self . inject_encoding . support ( & B3Encoding :: UnSpecified )
252+ {
252253 carrier. set ( B3_SAMPLED_HEADER , flag. to_string ( ) )
253254 }
254255 }
@@ -259,8 +260,7 @@ impl api::HttpTextFormat for B3Propagator {
259260 /// `Context` is returned.
260261 fn extract_with_context ( & self , cx : & api:: Context , carrier : & dyn api:: Carrier ) -> api:: Context {
261262 let span_context = if self . inject_encoding . support ( & B3Encoding :: SingleHeader ) {
262- self . extract_single_header ( carrier)
263- . unwrap_or_else ( |_|
263+ self . extract_single_header ( carrier) . unwrap_or_else ( |_|
264264 // if invalid single header should fallback to multiple
265265 self . extract_multi_header ( carrier)
266266 . unwrap_or_else ( |_| api:: SpanContext :: empty_context ( ) ) )
@@ -285,7 +285,6 @@ mod tests {
285285 const TRACE_ID_HEX : u128 = 0x4bf9_2f35_77b3_4da6_a3ce_929d_0e0e_4736 ;
286286 const SPAN_ID_HEX : u64 = 0x00f0_67aa_0ba9_02b7 ;
287287
288-
289288 #[ rustfmt:: skip]
290289 fn single_header_extract_data ( ) -> Vec < ( & ' static str , api:: SpanContext ) > {
291290 vec ! [
@@ -407,7 +406,13 @@ mod tests {
407406 ]
408407 }
409408
410- fn extract_carrier_from_test_data ( trace : Option < & ' static str > , span : Option < & ' static str > , sampled : Option < & ' static str > , debug : Option < & ' static str > , parent : Option < & ' static str > ) -> HashMap < String , String > {
409+ fn extract_carrier_from_test_data (
410+ trace : Option < & ' static str > ,
411+ span : Option < & ' static str > ,
412+ sampled : Option < & ' static str > ,
413+ debug : Option < & ' static str > ,
414+ parent : Option < & ' static str > ,
415+ ) -> HashMap < String , String > {
411416 let mut carrier = HashMap :: new ( ) ;
412417 if let Some ( trace_id) = trace {
413418 carrier. insert ( B3_TRACE_ID_HEADER . to_string ( ) , trace_id. to_owned ( ) ) ;
@@ -462,7 +467,9 @@ mod tests {
462467 )
463468 }
464469
465- for ( ( trace, span, sampled, debug, parent) , single_header, expected_context) in single_multi_header_extract_data ( ) {
470+ for ( ( trace, span, sampled, debug, parent) , single_header, expected_context) in
471+ single_multi_header_extract_data ( )
472+ {
466473 let mut carrier = extract_carrier_from_test_data ( trace, span, sampled, debug, parent) ;
467474 carrier. insert ( B3_SINGLE_HEADER . to_string ( ) , single_header. to_owned ( ) ) ;
468475 assert_eq ! (
@@ -481,7 +488,10 @@ mod tests {
481488
482489 for invalid_single_header in single_header_extrace_invalid_data ( ) {
483490 let mut carrier = HashMap :: new ( ) ;
484- carrier. insert ( B3_SINGLE_HEADER . to_string ( ) , invalid_single_header. to_string ( ) ) ;
491+ carrier. insert (
492+ B3_SINGLE_HEADER . to_string ( ) ,
493+ invalid_single_header. to_string ( ) ,
494+ ) ;
485495 assert_eq ! (
486496 single_header_propagator
487497 . extract( & carrier)
@@ -511,7 +521,8 @@ mod tests {
511521 _name : String ,
512522 _timestamp : std:: time:: SystemTime ,
513523 _attributes : Vec < api:: KeyValue > ,
514- ) { }
524+ ) {
525+ }
515526 fn span_context ( & self ) -> api:: SpanContext {
516527 self . 0 . clone ( )
517528 }
@@ -528,7 +539,8 @@ mod tests {
528539 fn inject_b3 ( ) {
529540 let single_header_propagator = B3Propagator :: with_encoding ( B3Encoding :: SingleHeader ) ;
530541 let multi_header_propagator = B3Propagator :: with_encoding ( B3Encoding :: MultipleHeader ) ;
531- let single_multi_header_propagator = B3Propagator :: with_encoding ( B3Encoding :: SingleAndMultiHeader ) ;
542+ let single_multi_header_propagator =
543+ B3Propagator :: with_encoding ( B3Encoding :: SingleAndMultiHeader ) ;
532544 let unspecified_header_propagator = B3Propagator :: with_encoding ( B3Encoding :: UnSpecified ) ;
533545
534546 for ( expected_header, context) in single_header_inject_data ( ) {
@@ -558,16 +570,28 @@ mod tests {
558570
559571 assert_eq ! ( carrier_multi_header, carrier_unspecific) ;
560572
561- assert_eq ! ( carrier_multi_header. get( B3_TRACE_ID_HEADER ) . map( |s| s. to_owned( ) ) ,
562- trace_id. map( |s| s. to_string( ) ) ) ;
563- assert_eq ! ( carrier_multi_header. get( B3_SPAN_ID_HEADER ) . map( |s| s. to_owned( ) ) ,
564- span_id. map( |s| s. to_string( ) ) ) ;
565573 assert_eq ! (
566- carrier_multi_header. get( B3_SAMPLED_HEADER ) . map( |s| s. to_owned( ) ) ,
574+ carrier_multi_header
575+ . get( B3_TRACE_ID_HEADER )
576+ . map( |s| s. to_owned( ) ) ,
577+ trace_id. map( |s| s. to_string( ) )
578+ ) ;
579+ assert_eq ! (
580+ carrier_multi_header
581+ . get( B3_SPAN_ID_HEADER )
582+ . map( |s| s. to_owned( ) ) ,
583+ span_id. map( |s| s. to_string( ) )
584+ ) ;
585+ assert_eq ! (
586+ carrier_multi_header
587+ . get( B3_SAMPLED_HEADER )
588+ . map( |s| s. to_owned( ) ) ,
567589 sampled. map( |s| s. to_string( ) )
568590 ) ;
569591 assert_eq ! (
570- carrier_multi_header. get( B3_DEBUG_FLAG_HEADER ) . map( |s| s. to_owned( ) ) ,
592+ carrier_multi_header
593+ . get( B3_DEBUG_FLAG_HEADER )
594+ . map( |s| s. to_owned( ) ) ,
571595 flag. map( |s| s. to_string( ) )
572596 ) ;
573597 assert_eq ! ( carrier_multi_header. get( B3_PARENT_SPAN_ID_HEADER ) , None ) ;
@@ -580,10 +604,14 @@ mod tests {
580604 & mut carrier,
581605 ) ;
582606
583- assert_eq ! ( carrier. get( B3_TRACE_ID_HEADER ) . map( |s| s. to_owned( ) ) ,
584- trace_id. map( |s| s. to_string( ) ) ) ;
585- assert_eq ! ( carrier. get( B3_SPAN_ID_HEADER ) . map( |s| s. to_owned( ) ) ,
586- span_id. map( |s| s. to_string( ) ) ) ;
607+ assert_eq ! (
608+ carrier. get( B3_TRACE_ID_HEADER ) . map( |s| s. to_owned( ) ) ,
609+ trace_id. map( |s| s. to_string( ) )
610+ ) ;
611+ assert_eq ! (
612+ carrier. get( B3_SPAN_ID_HEADER ) . map( |s| s. to_owned( ) ) ,
613+ span_id. map( |s| s. to_string( ) )
614+ ) ;
587615 assert_eq ! (
588616 carrier. get( B3_SAMPLED_HEADER ) . map( |s| s. to_owned( ) ) ,
589617 sampled. map( |s| s. to_string( ) )
0 commit comments