1515 */
1616
1717import {
18+ Baggage ,
1819 Context ,
19- CorrelationContext ,
20+ getBaggage ,
21+ setBaggage ,
2022 TextMapGetter ,
2123 TextMapPropagator ,
2224 TextMapSetter ,
2325} from '@opentelemetry/api' ;
24- import {
25- getCorrelationContext ,
26- setCorrelationContext ,
27- } from '../correlation-context' ;
2826
2927const KEY_PAIR_SEPARATOR = '=' ;
3028const PROPERTIES_SEPARATOR = ';' ;
3129const ITEMS_SEPARATOR = ',' ;
3230
33- // Name of the http header used to propagate the correlation context
34- export const CORRELATION_CONTEXT_HEADER = 'baggage' ;
31+ // Name of the http header used to propagate the baggage
32+ export const BAGGAGE_HEADER = 'baggage' ;
3533// Maximum number of name-value pairs allowed by w3c spec
3634export const MAX_NAME_VALUE_PAIRS = 180 ;
3735// Maximum number of bytes per a single name-value pair allowed by w3c spec
@@ -44,23 +42,23 @@ type KeyPair = {
4442} ;
4543
4644/**
47- * Propagates {@link CorrelationContext } through Context format propagation.
45+ * Propagates {@link Baggage } through Context format propagation.
4846 *
49- * Based on the Correlation Context specification:
50- * https://w3c.github.io/correlation-context /
47+ * Based on the Baggage specification:
48+ * https://w3c.github.io/baggage /
5149 */
52- export class HttpCorrelationContext implements TextMapPropagator {
50+ export class HttpBaggage implements TextMapPropagator {
5351 inject ( context : Context , carrier : unknown , setter : TextMapSetter ) {
54- const correlationContext = getCorrelationContext ( context ) ;
55- if ( ! correlationContext ) return ;
56- const keyPairs = this . _getKeyPairs ( correlationContext )
52+ const baggage = getBaggage ( context ) ;
53+ if ( ! baggage ) return ;
54+ const keyPairs = this . _getKeyPairs ( baggage )
5755 . filter ( ( pair : string ) => {
5856 return pair . length <= MAX_PER_NAME_VALUE_PAIRS ;
5957 } )
6058 . slice ( 0 , MAX_NAME_VALUE_PAIRS ) ;
6159 const headerValue = this . _serializeKeyPairs ( keyPairs ) ;
6260 if ( headerValue . length > 0 ) {
63- setter . set ( carrier , CORRELATION_CONTEXT_HEADER , headerValue ) ;
61+ setter . set ( carrier , BAGGAGE_HEADER , headerValue ) ;
6462 }
6563 }
6664
@@ -71,36 +69,31 @@ export class HttpCorrelationContext implements TextMapPropagator {
7169 } , '' ) ;
7270 }
7371
74- private _getKeyPairs ( correlationContext : CorrelationContext ) : string [ ] {
75- return Object . keys ( correlationContext ) . map (
72+ private _getKeyPairs ( baggage : Baggage ) : string [ ] {
73+ return Object . keys ( baggage ) . map (
7674 ( key : string ) =>
77- `${ encodeURIComponent ( key ) } =${ encodeURIComponent (
78- correlationContext [ key ] . value
79- ) } `
75+ `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( baggage [ key ] . value ) } `
8076 ) ;
8177 }
8278
8379 extract ( context : Context , carrier : unknown , getter : TextMapGetter ) : Context {
84- const headerValue : string = getter . get (
85- carrier ,
86- CORRELATION_CONTEXT_HEADER
87- ) as string ;
80+ const headerValue : string = getter . get ( carrier , BAGGAGE_HEADER ) as string ;
8881 if ( ! headerValue ) return context ;
89- const correlationContext : CorrelationContext = { } ;
82+ const baggage : Baggage = { } ;
9083 if ( headerValue . length == 0 ) {
9184 return context ;
9285 }
9386 const pairs = headerValue . split ( ITEMS_SEPARATOR ) ;
9487 pairs . forEach ( entry => {
9588 const keyPair = this . _parsePairKeyValue ( entry ) ;
9689 if ( keyPair ) {
97- correlationContext [ keyPair . key ] = { value : keyPair . value } ;
90+ baggage [ keyPair . key ] = { value : keyPair . value } ;
9891 }
9992 } ) ;
100- if ( Object . entries ( correlationContext ) . length === 0 ) {
93+ if ( Object . entries ( baggage ) . length === 0 ) {
10194 return context ;
10295 }
103- return setCorrelationContext ( context , correlationContext ) ;
96+ return setBaggage ( context , baggage ) ;
10497 }
10598
10699 private _parsePairKeyValue ( entry : string ) : KeyPair | undefined {
@@ -120,6 +113,6 @@ export class HttpCorrelationContext implements TextMapPropagator {
120113 }
121114
122115 fields ( ) : string [ ] {
123- return [ CORRELATION_CONTEXT_HEADER ] ;
116+ return [ BAGGAGE_HEADER ] ;
124117 }
125118}
0 commit comments