Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): Don't set extra baggage headers #8657

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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