@@ -28,31 +28,10 @@ export function baggageHeaderToDynamicSamplingContext(
28
28
// Very liberal definition of what any incoming header might look like
29
29
baggageHeader : string | string [ ] | number | null | undefined | boolean ,
30
30
) : Partial < DynamicSamplingContext > | undefined {
31
- if ( ! isString ( baggageHeader ) && ! Array . isArray ( baggageHeader ) ) {
32
- return undefined ;
33
- }
34
-
35
- // Intermediary object to store baggage key value pairs of incoming baggage headers on.
36
- // It is later used to read Sentry-DSC-values from.
37
- let baggageObject : Readonly < Record < string , string > > = { } ;
31
+ const baggageObject = parseBaggageHeader ( baggageHeader ) ;
38
32
39
- if ( Array . isArray ( baggageHeader ) ) {
40
- // Combine all baggage headers into one object containing the baggage values so we can later read the Sentry-DSC-values from it
41
- baggageObject = baggageHeader . reduce < Record < string , string > > ( ( acc , curr ) => {
42
- const currBaggageObject = baggageHeaderToObject ( curr ) ;
43
- for ( const key of Object . keys ( currBaggageObject ) ) {
44
- acc [ key ] = currBaggageObject [ key ] ;
45
- }
46
- return acc ;
47
- } , { } ) ;
48
- } else {
49
- // Return undefined if baggage header is an empty string (technically an empty baggage header is not spec conform but
50
- // this is how we choose to handle it)
51
- if ( ! baggageHeader ) {
52
- return undefined ;
53
- }
54
-
55
- baggageObject = baggageHeaderToObject ( baggageHeader ) ;
33
+ if ( ! baggageObject ) {
34
+ return undefined ;
56
35
}
57
36
58
37
// Read all "sentry-" prefixed values out of the baggage object and put it onto a dynamic sampling context object.
@@ -104,6 +83,30 @@ export function dynamicSamplingContextToSentryBaggageHeader(
104
83
return objectToBaggageHeader ( sentryPrefixedDSC ) ;
105
84
}
106
85
86
+ /**
87
+ * Take a baggage header and parse it into an object.
88
+ */
89
+ export function parseBaggageHeader (
90
+ baggageHeader : string | string [ ] | number | null | undefined | boolean ,
91
+ ) : Record < string , string > | undefined {
92
+ if ( ! baggageHeader || ( ! isString ( baggageHeader ) && ! Array . isArray ( baggageHeader ) ) ) {
93
+ return undefined ;
94
+ }
95
+
96
+ if ( Array . isArray ( baggageHeader ) ) {
97
+ // Combine all baggage headers into one object containing the baggage values so we can later read the Sentry-DSC-values from it
98
+ return baggageHeader . reduce < Record < string , string > > ( ( acc , curr ) => {
99
+ const currBaggageObject = baggageHeaderToObject ( curr ) ;
100
+ for ( const key of Object . keys ( currBaggageObject ) ) {
101
+ acc [ key ] = currBaggageObject [ key ] ;
102
+ }
103
+ return acc ;
104
+ } , { } ) ;
105
+ }
106
+
107
+ return baggageHeaderToObject ( baggageHeader ) ;
108
+ }
109
+
107
110
/**
108
111
* Will parse a baggage header, which is a simple key-value map, into a flat object.
109
112
*
0 commit comments