Skip to content

Commit 5aed0fc

Browse files
committed
move comment, handle spaces in custom baggage header
1 parent b12bdd3 commit 5aed0fc

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

packages/core/src/fetch.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export function instrumentFetchRequest(
104104
/**
105105
* Adds sentry-trace and baggage headers to the various forms of fetch headers.
106106
* exported only for testing purposes
107+
*
108+
* When we determine if we should add a baggage header, there are 3 cases:
109+
* 1. No previous baggage header -> add baggage
110+
* 2. Previous baggage header has no sentry baggage values -> add our baggage
111+
* 3. Previous baggage header has sentry baggage values -> do nothing (might have been added manually by users)
107112
*/
108113
// eslint-disable-next-line complexity -- yup it's this complicated :(
109114
export function _addTracingHeadersToFetchRequest(
@@ -141,10 +146,6 @@ export function _addTracingHeadersToFetchRequest(
141146
if (baggage) {
142147
const prevBaggageHeader = newHeaders.get('baggage');
143148

144-
// 3 cases:
145-
// 1. No previous baggage header -> add baggage
146-
// 2. Previous baggage header has no sentry baggage values -> add our baggage
147-
// 3. Previous baggage header has sentry baggage values -> do nothing (might have been added manually by users)
148149
if (!prevBaggageHeader) {
149150
newHeaders.set('baggage', baggage);
150151
} else if (!baggageHeaderHasSentryBaggageValues(prevBaggageHeader)) {
@@ -218,9 +219,7 @@ function endSpan(span: Span, handlerData: HandlerDataFetch): void {
218219
}
219220

220221
function baggageHeaderHasSentryBaggageValues(baggageHeader: string): boolean {
221-
return baggageHeader
222-
.split(',')
223-
.some(baggageEntry => baggageEntry.split('=')[0]?.startsWith(SENTRY_BAGGAGE_KEY_PREFIX));
222+
return baggageHeader.split(',').some(baggageEntry => baggageEntry.trim().startsWith(SENTRY_BAGGAGE_KEY_PREFIX));
224223
}
225224

226225
function isHeaders(headers: unknown): headers is Headers {

packages/core/test/lib/fetch.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const { DEFAULT_SENTRY_TRACE, DEFAULT_BAGGAGE } = vi.hoisted(() => ({
77
}));
88

99
const CUSTOM_SENTRY_TRACE = '123-abc-1';
10-
const CUSTOM_BAGGAGE = 'sentry-trace_id=123,sentry-sampled=true';
10+
// adding in random spaces here to ensure they are trimmed and our sentry baggage item detection logic works.
11+
// Spaces between items are allowed by the baggage spec.
12+
const CUSTOM_BAGGAGE = ' sentry-trace_id=123 , sentry-sampled=true';
1113

1214
vi.mock('../../src/utils/traceData', () => {
1315
return {
@@ -156,7 +158,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
156158
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
157159
'custom-header': 'custom-value',
158160
'sentry-trace': CUSTOM_SENTRY_TRACE,
159-
baggage: CUSTOM_BAGGAGE,
161+
baggage: CUSTOM_BAGGAGE.trim(),
160162
});
161163
});
162164

@@ -359,7 +361,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
359361
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
360362
'custom-header': 'custom-value',
361363
'sentry-trace': CUSTOM_SENTRY_TRACE,
362-
baggage: CUSTOM_BAGGAGE,
364+
baggage: CUSTOM_BAGGAGE.trim(),
363365
});
364366
});
365367

@@ -380,7 +382,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
380382
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
381383
'custom-header': 'custom-value',
382384
'sentry-trace': CUSTOM_SENTRY_TRACE,
383-
baggage: CUSTOM_BAGGAGE,
385+
baggage: CUSTOM_BAGGAGE.trim(),
384386
});
385387
});
386388

@@ -401,7 +403,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
401403
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
402404
'custom-header': 'custom-value',
403405
'sentry-trace': CUSTOM_SENTRY_TRACE,
404-
baggage: CUSTOM_BAGGAGE,
406+
baggage: CUSTOM_BAGGAGE.trim(),
405407
});
406408
});
407409
});

0 commit comments

Comments
 (0)