Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
move comment, handle spaces in custom baggage header
  • Loading branch information
Lms24 committed May 5, 2025
commit 5aed0fc12da8fdb9c6eeee071eaf8419d7e1106e
13 changes: 6 additions & 7 deletions packages/core/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export function instrumentFetchRequest(
/**
* Adds sentry-trace and baggage headers to the various forms of fetch headers.
* exported only for testing purposes
*
* When we determine if we should add a baggage header, there are 3 cases:
* 1. No previous baggage header -> add baggage
* 2. Previous baggage header has no sentry baggage values -> add our baggage
* 3. Previous baggage header has sentry baggage values -> do nothing (might have been added manually by users)
*/
// eslint-disable-next-line complexity -- yup it's this complicated :(
export function _addTracingHeadersToFetchRequest(
Expand Down Expand Up @@ -141,10 +146,6 @@ export function _addTracingHeadersToFetchRequest(
if (baggage) {
const prevBaggageHeader = newHeaders.get('baggage');

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

function baggageHeaderHasSentryBaggageValues(baggageHeader: string): boolean {
return baggageHeader
.split(',')
.some(baggageEntry => baggageEntry.split('=')[0]?.startsWith(SENTRY_BAGGAGE_KEY_PREFIX));
return baggageHeader.split(',').some(baggageEntry => baggageEntry.trim().startsWith(SENTRY_BAGGAGE_KEY_PREFIX));
}

function isHeaders(headers: unknown): headers is Headers {
Expand Down
12 changes: 7 additions & 5 deletions packages/core/test/lib/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const { DEFAULT_SENTRY_TRACE, DEFAULT_BAGGAGE } = vi.hoisted(() => ({
}));

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

vi.mock('../../src/utils/traceData', () => {
return {
Expand Down Expand Up @@ -156,7 +158,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
'custom-header': 'custom-value',
'sentry-trace': CUSTOM_SENTRY_TRACE,
baggage: CUSTOM_BAGGAGE,
baggage: CUSTOM_BAGGAGE.trim(),
});
});

Expand Down Expand Up @@ -359,7 +361,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
'custom-header': 'custom-value',
'sentry-trace': CUSTOM_SENTRY_TRACE,
baggage: CUSTOM_BAGGAGE,
baggage: CUSTOM_BAGGAGE.trim(),
});
});

Expand All @@ -380,7 +382,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
'custom-header': 'custom-value',
'sentry-trace': CUSTOM_SENTRY_TRACE,
baggage: CUSTOM_BAGGAGE,
baggage: CUSTOM_BAGGAGE.trim(),
});
});

Expand All @@ -401,7 +403,7 @@ describe('_addTracingHeadersToFetchRequest', () => {
expect(Object.fromEntries(returnedHeaders!.entries())).toEqual({
'custom-header': 'custom-value',
'sentry-trace': CUSTOM_SENTRY_TRACE,
baggage: CUSTOM_BAGGAGE,
baggage: CUSTOM_BAGGAGE.trim(),
});
});
});
Expand Down
Loading