Skip to content

Commit

Permalink
fix(node): Don't set extra baggage headers (#8657)
Browse files Browse the repository at this point in the history
if we see that a `sentry-trace` header was already attached on the
`requestOptions`, don't attach sentry trace and baggage headers again.
  • Loading branch information
AbhiPrasad authored Jul 27, 2023
1 parent 6ffc8a3 commit 5c60612
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
11 changes: 9 additions & 2 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,18 @@ function addHeadersToRequestOptions(
sentryTraceHeader: string,
dynamicSamplingContext: Partial<DynamicSamplingContext> | undefined,
): void {
// Don't overwrite sentry-trace and baggage header if it's already set.
const headers = requestOptions.headers || {};
if (headers['sentry-trace']) {
return;
}

__DEBUG_BUILD__ &&
logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${requestUrl}": `);
const sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
const sentryBaggageHeader = normalizeBaggageHeader(requestOptions, sentryBaggage);
const sentryBaggageHeader =
sentryBaggage && sentryBaggage.length > 0 ? normalizeBaggageHeader(requestOptions, sentryBaggage) : undefined;

requestOptions.headers = {
...requestOptions.headers,
'sentry-trace': sentryTraceHeader,
Expand Down Expand Up @@ -354,7 +362,6 @@ function normalizeBaggageHeader(
} else if (Array.isArray(requestOptions.headers.baggage)) {
return [...requestOptions.headers.baggage, sentryBaggageHeader];
}

// Type-cast explanation:
// Technically this the following could be of type `(number | string)[]` but for the sake of simplicity
// we say this is undefined behaviour, since it would not be baggage spec conform if the user did this.
Expand Down
28 changes: 25 additions & 3 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const originalHttpGet = http.get;
const originalHttpRequest = http.request;

describe('tracing', () => {
afterEach(() => {
sentryCore.getCurrentHub().getScope().setSpan(undefined);
});

function createTransactionOnScope(
customOptions: Partial<NodeClientOptions> = {},
customContext?: Partial<TransactionContext>,
Expand Down Expand Up @@ -178,21 +182,39 @@ describe('tracing', () => {
]);
});

it("doesn't attach baggage headers if already defined", () => {
nock('http://dogs.are.great').get('/').reply(200);

createTransactionOnScope();

const request = http.get({
host: 'http://dogs.are.great/',
headers: {
'sentry-trace': '12312012123120121231201212312012-1231201212312012-0',
baggage: 'sentry-environment=production,sentry-trace_id=12312012123120121231201212312012',
},
});
const baggage = request.getHeader('baggage');
expect(baggage).toEqual('sentry-environment=production,sentry-trace_id=12312012123120121231201212312012');
});

it('generates and uses propagation context to attach baggage and sentry-trace header', async () => {
nock('http://dogs.are.great').get('/').reply(200);

const { traceId } = sentryCore.getCurrentHub().getScope().getPropagationContext();

const request = http.get('http://dogs.are.great/');
const sentryTraceHeader = request.getHeader('sentry-trace') as string;
const baggageHeader = request.getHeader('baggage') as string;

const parts = sentryTraceHeader.split('-');
expect(parts.length).toEqual(3);
expect(parts[0]).toEqual('12312012123120121231201212312012');
expect(parts[0]).toEqual(traceId);
expect(parts[1]).toEqual(expect.any(String));
expect(parts[2]).toEqual('1');
expect(parts[2]).toEqual('0');

expect(baggageHeader).toEqual(
'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',
`sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=${traceId}`,
);
});

Expand Down

0 comments on commit 5c60612

Please sign in to comment.