Skip to content

Commit 5c60612

Browse files
authored
fix(node): Don't set extra baggage headers (#8657)
if we see that a `sentry-trace` header was already attached on the `requestOptions`, don't attach sentry trace and baggage headers again.
1 parent 6ffc8a3 commit 5c60612

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

packages/node/src/integrations/http.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,18 @@ function addHeadersToRequestOptions(
314314
sentryTraceHeader: string,
315315
dynamicSamplingContext: Partial<DynamicSamplingContext> | undefined,
316316
): void {
317+
// Don't overwrite sentry-trace and baggage header if it's already set.
318+
const headers = requestOptions.headers || {};
319+
if (headers['sentry-trace']) {
320+
return;
321+
}
322+
317323
__DEBUG_BUILD__ &&
318324
logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${requestUrl}": `);
319325
const sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
320-
const sentryBaggageHeader = normalizeBaggageHeader(requestOptions, sentryBaggage);
326+
const sentryBaggageHeader =
327+
sentryBaggage && sentryBaggage.length > 0 ? normalizeBaggageHeader(requestOptions, sentryBaggage) : undefined;
328+
321329
requestOptions.headers = {
322330
...requestOptions.headers,
323331
'sentry-trace': sentryTraceHeader,
@@ -354,7 +362,6 @@ function normalizeBaggageHeader(
354362
} else if (Array.isArray(requestOptions.headers.baggage)) {
355363
return [...requestOptions.headers.baggage, sentryBaggageHeader];
356364
}
357-
358365
// Type-cast explanation:
359366
// Technically this the following could be of type `(number | string)[]` but for the sake of simplicity
360367
// we say this is undefined behaviour, since it would not be baggage spec conform if the user did this.

packages/node/test/integrations/http.test.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const originalHttpGet = http.get;
1919
const originalHttpRequest = http.request;
2020

2121
describe('tracing', () => {
22+
afterEach(() => {
23+
sentryCore.getCurrentHub().getScope().setSpan(undefined);
24+
});
25+
2226
function createTransactionOnScope(
2327
customOptions: Partial<NodeClientOptions> = {},
2428
customContext?: Partial<TransactionContext>,
@@ -178,21 +182,39 @@ describe('tracing', () => {
178182
]);
179183
});
180184

185+
it("doesn't attach baggage headers if already defined", () => {
186+
nock('http://dogs.are.great').get('/').reply(200);
187+
188+
createTransactionOnScope();
189+
190+
const request = http.get({
191+
host: 'http://dogs.are.great/',
192+
headers: {
193+
'sentry-trace': '12312012123120121231201212312012-1231201212312012-0',
194+
baggage: 'sentry-environment=production,sentry-trace_id=12312012123120121231201212312012',
195+
},
196+
});
197+
const baggage = request.getHeader('baggage');
198+
expect(baggage).toEqual('sentry-environment=production,sentry-trace_id=12312012123120121231201212312012');
199+
});
200+
181201
it('generates and uses propagation context to attach baggage and sentry-trace header', async () => {
182202
nock('http://dogs.are.great').get('/').reply(200);
183203

204+
const { traceId } = sentryCore.getCurrentHub().getScope().getPropagationContext();
205+
184206
const request = http.get('http://dogs.are.great/');
185207
const sentryTraceHeader = request.getHeader('sentry-trace') as string;
186208
const baggageHeader = request.getHeader('baggage') as string;
187209

188210
const parts = sentryTraceHeader.split('-');
189211
expect(parts.length).toEqual(3);
190-
expect(parts[0]).toEqual('12312012123120121231201212312012');
212+
expect(parts[0]).toEqual(traceId);
191213
expect(parts[1]).toEqual(expect.any(String));
192-
expect(parts[2]).toEqual('1');
214+
expect(parts[2]).toEqual('0');
193215

194216
expect(baggageHeader).toEqual(
195-
'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,sentry-sampled=true',
217+
`sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=${traceId}`,
196218
);
197219
});
198220

0 commit comments

Comments
 (0)