15
15
*/
16
16
17
17
import {
18
- Baggage ,
19
18
Context ,
20
19
BaggageEntry ,
21
20
getBaggage ,
@@ -24,22 +23,15 @@ import {
24
23
TextMapPropagator ,
25
24
TextMapSetter ,
26
25
createBaggage ,
27
- baggageEntryMetadataFromString ,
28
26
isInstrumentationSuppressed ,
29
27
} from '@opentelemetry/api' ;
30
-
31
- const KEY_PAIR_SEPARATOR = '=' ;
32
- const PROPERTIES_SEPARATOR = ';' ;
33
- const ITEMS_SEPARATOR = ',' ;
34
-
35
- // Name of the http header used to propagate the baggage
36
- export const BAGGAGE_HEADER = 'baggage' ;
37
- // Maximum number of name-value pairs allowed by w3c spec
38
- export const MAX_NAME_VALUE_PAIRS = 180 ;
39
- // Maximum number of bytes per a single name-value pair allowed by w3c spec
40
- export const MAX_PER_NAME_VALUE_PAIRS = 4096 ;
41
- // Maximum total length of all name-value pairs allowed by w3c spec
42
- export const MAX_TOTAL_LENGTH = 8192 ;
28
+ import { getKeyPairs , serializeKeyPairs , parsePairKeyValue } from '../utils' ;
29
+ import {
30
+ BAGGAGE_MAX_NAME_VALUE_PAIRS ,
31
+ BAGGAGE_ITEMS_SEPARATOR ,
32
+ BAGGAGE_HEADER ,
33
+ BAGGAGE_MAX_PER_NAME_VALUE_PAIRS ,
34
+ } from '../constants' ;
43
35
44
36
/**
45
37
* Propagates {@link Baggage} through Context format propagation.
@@ -51,45 +43,27 @@ export class HttpBaggagePropagator implements TextMapPropagator {
51
43
inject ( context : Context , carrier : unknown , setter : TextMapSetter ) {
52
44
const baggage = getBaggage ( context ) ;
53
45
if ( ! baggage || isInstrumentationSuppressed ( context ) ) return ;
54
- const keyPairs = this . _getKeyPairs ( baggage )
46
+ const keyPairs = getKeyPairs ( baggage )
55
47
. filter ( ( pair : string ) => {
56
- return pair . length <= MAX_PER_NAME_VALUE_PAIRS ;
48
+ return pair . length <= BAGGAGE_MAX_PER_NAME_VALUE_PAIRS ;
57
49
} )
58
- . slice ( 0 , MAX_NAME_VALUE_PAIRS ) ;
59
- const headerValue = this . _serializeKeyPairs ( keyPairs ) ;
50
+ . slice ( 0 , BAGGAGE_MAX_NAME_VALUE_PAIRS ) ;
51
+ const headerValue = serializeKeyPairs ( keyPairs ) ;
60
52
if ( headerValue . length > 0 ) {
61
53
setter . set ( carrier , BAGGAGE_HEADER , headerValue ) ;
62
54
}
63
55
}
64
56
65
- private _serializeKeyPairs ( keyPairs : string [ ] ) {
66
- return keyPairs . reduce ( ( hValue : string , current : string ) => {
67
- const value = `${ hValue } ${
68
- hValue !== '' ? ITEMS_SEPARATOR : ''
69
- } ${ current } `;
70
- return value . length > MAX_TOTAL_LENGTH ? hValue : value ;
71
- } , '' ) ;
72
- }
73
-
74
- private _getKeyPairs ( baggage : Baggage ) : string [ ] {
75
- return baggage
76
- . getAllEntries ( )
77
- . map (
78
- ( [ key , value ] ) =>
79
- `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value . value ) } `
80
- ) ;
81
- }
82
-
83
57
extract ( context : Context , carrier : unknown , getter : TextMapGetter ) : Context {
84
58
const headerValue : string = getter . get ( carrier , BAGGAGE_HEADER ) as string ;
85
59
if ( ! headerValue ) return context ;
86
60
const baggage : Record < string , BaggageEntry > = { } ;
87
61
if ( headerValue . length === 0 ) {
88
62
return context ;
89
63
}
90
- const pairs = headerValue . split ( ITEMS_SEPARATOR ) ;
64
+ const pairs = headerValue . split ( BAGGAGE_ITEMS_SEPARATOR ) ;
91
65
pairs . forEach ( entry => {
92
- const keyPair = this . _parsePairKeyValue ( entry ) ;
66
+ const keyPair = parsePairKeyValue ( entry ) ;
93
67
if ( keyPair ) {
94
68
const baggageEntry : BaggageEntry = { value : keyPair . value } ;
95
69
if ( keyPair . metadata ) {
@@ -104,24 +78,6 @@ export class HttpBaggagePropagator implements TextMapPropagator {
104
78
return setBaggage ( context , createBaggage ( baggage ) ) ;
105
79
}
106
80
107
- private _parsePairKeyValue ( entry : string ) {
108
- const valueProps = entry . split ( PROPERTIES_SEPARATOR ) ;
109
- if ( valueProps . length <= 0 ) return ;
110
- const keyPairPart = valueProps . shift ( ) ;
111
- if ( ! keyPairPart ) return ;
112
- const keyPair = keyPairPart . split ( KEY_PAIR_SEPARATOR ) ;
113
- if ( keyPair . length !== 2 ) return ;
114
- const key = decodeURIComponent ( keyPair [ 0 ] . trim ( ) ) ;
115
- const value = decodeURIComponent ( keyPair [ 1 ] . trim ( ) ) ;
116
- let metadata ;
117
- if ( valueProps . length > 0 ) {
118
- metadata = baggageEntryMetadataFromString (
119
- valueProps . join ( PROPERTIES_SEPARATOR )
120
- ) ;
121
- }
122
- return { key, value, metadata } ;
123
- }
124
-
125
81
fields ( ) : string [ ] {
126
82
return [ BAGGAGE_HEADER ] ;
127
83
}
0 commit comments