1
1
import { Baggage , BaggageObj , TraceparentData } from '@sentry/types' ;
2
+ import { HttpHeaderValue } from '@sentry/types' ;
2
3
4
+ import { isString } from './is' ;
3
5
import { logger } from './logger' ;
4
6
5
7
export const BAGGAGE_HEADER_NAME = 'baggage' ;
@@ -89,9 +91,28 @@ export function serializeBaggage(baggage: Baggage): string {
89
91
} , baggage [ 1 ] ) ;
90
92
}
91
93
92
- /** Parse a baggage header from a string and return a Baggage object */
93
- export function parseBaggageString ( inputBaggageString : string ) : Baggage {
94
- return inputBaggageString . split ( ',' ) . reduce (
94
+ /** Parse a baggage header from a string or a string array and return a Baggage object */
95
+ export function parseBaggageHeader ( inputBaggageValue : HttpHeaderValue ) : Baggage {
96
+ // Adding this check here because we got reports of this function failing due to the input value
97
+ // not being a string. This debug log might help us determine what's going on here.
98
+ if ( ( ! Array . isArray ( inputBaggageValue ) && ! isString ( inputBaggageValue ) ) || typeof inputBaggageValue === 'number' ) {
99
+ __DEBUG_BUILD__ &&
100
+ logger . warn (
101
+ '[parseBaggageHeader] Received input value of incompatible type: ' ,
102
+ typeof inputBaggageValue ,
103
+ inputBaggageValue ,
104
+ ) ;
105
+
106
+ // Gonna early-return an empty baggage object so that we don't fail later on
107
+ return createBaggage ( { } , '' ) ;
108
+ }
109
+
110
+ const baggageEntries = ( isString ( inputBaggageValue ) ? inputBaggageValue : inputBaggageValue . join ( ',' ) )
111
+ . split ( ',' )
112
+ . map ( entry => entry . trim ( ) )
113
+ . filter ( entry => entry !== '' ) ;
114
+
115
+ return baggageEntries . reduce (
95
116
( [ baggageObj , baggageString ] , curr ) => {
96
117
const [ key , val ] = curr . split ( '=' ) ;
97
118
if ( SENTRY_BAGGAGE_KEY_PREFIX_REGEX . test ( key ) ) {
@@ -122,16 +143,17 @@ export function parseBaggageString(inputBaggageString: string): Baggage {
122
143
* it would only affect parts of the sentry baggage (@see Baggage interface).
123
144
*
124
145
* @param incomingBaggage the baggage header of the incoming request that might contain sentry entries
125
- * @param headerBaggageString possibly existing baggage header string added from a third party to request headers
146
+ * @param thirdPartyBaggageHeader possibly existing baggage header string or string[] added from a third
147
+ * party to the request headers
126
148
*
127
149
* @return a merged and serialized baggage string to be propagated with the outgoing request
128
150
*/
129
- export function mergeAndSerializeBaggage ( incomingBaggage ?: Baggage , headerBaggageString ?: string ) : string {
130
- if ( ! incomingBaggage && ! headerBaggageString ) {
151
+ export function mergeAndSerializeBaggage ( incomingBaggage ?: Baggage , thirdPartyBaggageHeader ?: HttpHeaderValue ) : string {
152
+ if ( ! incomingBaggage && ! thirdPartyBaggageHeader ) {
131
153
return '' ;
132
154
}
133
155
134
- const headerBaggage = ( headerBaggageString && parseBaggageString ( headerBaggageString ) ) || undefined ;
156
+ const headerBaggage = ( thirdPartyBaggageHeader && parseBaggageHeader ( thirdPartyBaggageHeader ) ) || undefined ;
135
157
const thirdPartyHeaderBaggage = headerBaggage && getThirdPartyBaggage ( headerBaggage ) ;
136
158
137
159
const finalBaggage = createBaggage (
@@ -150,14 +172,14 @@ export function mergeAndSerializeBaggage(incomingBaggage?: Baggage, headerBaggag
150
172
*
151
173
* Extracted this logic to a function because it's duplicated in a lot of places.
152
174
*
153
- * @param rawBaggageString
175
+ * @param rawBaggageValue
154
176
* @param sentryTraceHeader
155
177
*/
156
178
export function parseBaggageSetMutability (
157
- rawBaggageString : string | false | undefined | null ,
179
+ rawBaggageValue : HttpHeaderValue | false | undefined ,
158
180
sentryTraceHeader : TraceparentData | string | false | undefined | null ,
159
181
) : Baggage {
160
- const baggage = parseBaggageString ( rawBaggageString || '' ) ;
182
+ const baggage = parseBaggageHeader ( rawBaggageValue || '' ) ;
161
183
if ( ! isSentryBaggageEmpty ( baggage ) || ( sentryTraceHeader && isSentryBaggageEmpty ( baggage ) ) ) {
162
184
setBaggageImmutable ( baggage ) ;
163
185
}
0 commit comments