11import { Baggage , BaggageObj , TraceparentData } from '@sentry/types' ;
2+ import { HttpHeaderValue } from '@sentry/types' ;
23
4+ import { isString } from './is' ;
35import { logger } from './logger' ;
46
57export const BAGGAGE_HEADER_NAME = 'baggage' ;
@@ -89,9 +91,28 @@ export function serializeBaggage(baggage: Baggage): string {
8991 } , baggage [ 1 ] ) ;
9092}
9193
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 (
95116 ( [ baggageObj , baggageString ] , curr ) => {
96117 const [ key , val ] = curr . split ( '=' ) ;
97118 if ( SENTRY_BAGGAGE_KEY_PREFIX_REGEX . test ( key ) ) {
@@ -122,16 +143,17 @@ export function parseBaggageString(inputBaggageString: string): Baggage {
122143 * it would only affect parts of the sentry baggage (@see Baggage interface).
123144 *
124145 * @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
126148 *
127149 * @return a merged and serialized baggage string to be propagated with the outgoing request
128150 */
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 ) {
131153 return '' ;
132154 }
133155
134- const headerBaggage = ( headerBaggageString && parseBaggageString ( headerBaggageString ) ) || undefined ;
156+ const headerBaggage = ( thirdPartyBaggageHeader && parseBaggageHeader ( thirdPartyBaggageHeader ) ) || undefined ;
135157 const thirdPartyHeaderBaggage = headerBaggage && getThirdPartyBaggage ( headerBaggage ) ;
136158
137159 const finalBaggage = createBaggage (
@@ -150,14 +172,14 @@ export function mergeAndSerializeBaggage(incomingBaggage?: Baggage, headerBaggag
150172 *
151173 * Extracted this logic to a function because it's duplicated in a lot of places.
152174 *
153- * @param rawBaggageString
175+ * @param rawBaggageValue
154176 * @param sentryTraceHeader
155177 */
156178export function parseBaggageSetMutability (
157- rawBaggageString : string | false | undefined | null ,
179+ rawBaggageValue : HttpHeaderValue | false | undefined ,
158180 sentryTraceHeader : TraceparentData | string | false | undefined | null ,
159181) : Baggage {
160- const baggage = parseBaggageString ( rawBaggageString || '' ) ;
182+ const baggage = parseBaggageHeader ( rawBaggageValue || '' ) ;
161183 if ( ! isSentryBaggageEmpty ( baggage ) || ( sentryTraceHeader && isSentryBaggageEmpty ( baggage ) ) ) {
162184 setBaggageImmutable ( baggage ) ;
163185 }
0 commit comments