Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(node): Deduplicate sentry-trace and baggage headers on outgoing requests#19960
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
12c3f5163b76668e7b3e832d3822a0b37a4e7eb9337fb7814429dad19b0b7f9784e9df978b88da9ff1bc933a42fe300e334caafd8ff6b436e3e008ec9627af6b77c61File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { afterAll, expect, test } from 'vitest'; | ||
| import { cleanupChildProcesses, createRunner } from '../../../../utils/runner'; | ||
| import type { TestAPIResponse } from '../server'; | ||
| import { extractTraceparentData } from '@sentry/core'; | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| @@ -33,7 +34,6 @@ test('should ignore sentry-values in `baggage` header of a third party vendor an | ||
| 'sentry-environment=myEnv', | ||
| 'sentry-release=2.1.0', | ||
| expect.stringMatching(/sentry-sample_rand=\d+/), | ||
| 'sentry-sample_rate=0.54', | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was a wrong assertion in the test | ||
| 'third=party', | ||
| ]); | ||
| }); | ||
| @@ -46,6 +46,11 @@ test('should ignore sentry-values in `baggage` header of a third party vendor an | ||
| expect(response).toBeDefined(); | ||
| const baggage = response?.test_data.baggage?.split(',').sort(); | ||
| const sentryTraceHeader = response?.test_data['sentry-trace']; | ||
| const sentryTrace = extractTraceparentData(sentryTraceHeader); | ||
| expect(sentryTrace?.traceId).toMatch(/^[0-9a-f]{32}$/); | ||
| expect(response).toMatchObject({ | ||
| test_data: { | ||
| @@ -63,7 +68,7 @@ test('should ignore sentry-values in `baggage` header of a third party vendor an | ||
| expect.stringMatching(/sentry-sample_rand=\d+/), | ||
| 'sentry-sample_rate=1', | ||
| 'sentry-sampled=true', | ||
| expect.stringMatching(/sentry-trace_id=[\da-f]{32}/), | ||
| `sentry-trace_id=${sentryTrace?.traceId}`, | ||
| 'sentry-transaction=GET%20%2Ftest%2Fexpress', | ||
| 'third=party', | ||
| ]); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { expect } from 'vitest'; | ||
| import { extractTraceparentData, parseBaggageHeader, TRACEPARENT_REGEXP } from '@sentry/core'; | ||
| export function expectNoDuplicateSentryBaggageKeys(baggage: string | string[] | undefined): void { | ||
| expect(baggage).toBeDefined(); | ||
| const baggageStr = Array.isArray(baggage) ? baggage.join(',') : (baggage as string); | ||
| const sentryEntries = baggageStr.split(',').filter(entry => entry.trim().startsWith('sentry-')); | ||
| const sentryKeyNames = sentryEntries.map(entry => entry.trim().split('=')[0]); | ||
| const uniqueKeyNames = [...new Set(sentryKeyNames)]; | ||
| expect(sentryKeyNames).toEqual(uniqueKeyNames); | ||
| } | ||
| export function expectConsistentTraceId(headers: Record<string, string | string[] | undefined>): void { | ||
| const sentryTrace = headers['sentry-trace']; | ||
| expect(sentryTrace).toMatch(TRACEPARENT_REGEXP); | ||
| const sentryTraceData = extractTraceparentData(sentryTrace as string)!; | ||
| expect(sentryTraceData.traceId).toMatch(/^[a-f\d]{32}$/); | ||
| const baggage = parseBaggageHeader(headers['baggage']); | ||
| const baggageTraceId = baggage!['sentry-trace_id']; | ||
| expect(baggageTraceId).toBeDefined(); | ||
| expect(baggageTraceId).toMatch(/^[a-f\d]{32}$/); | ||
| expect(sentryTraceData.traceId).toEqual(baggageTraceId); | ||
| } | ||
| export function expectUserSetTraceId(headers: Record<string, string | string[] | undefined>): void { | ||
| const xSentryTrace = extractTraceparentData(headers['x-tracedata-sentry-trace'] as string); | ||
| const sentryTrace = extractTraceparentData(headers['sentry-trace'] as string); | ||
| expect(xSentryTrace?.traceId).toBe(sentryTrace?.traceId); | ||
| const xBaggage = parseBaggageHeader(headers['x-tracedata-baggage']); | ||
| const baggage = parseBaggageHeader(headers['baggage']); | ||
| expect(xBaggage).toEqual(baggage); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| // explicitly not setting tracesSampleRate, | ||
| transport: loggingTransport, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import http from 'http'; | ||
| async function run() { | ||
| const traceData = Sentry.getTraceData(); | ||
| // fetch with manual getTraceData() headers - the core reproduction case from #19158 | ||
| await fetch(`${process.env.SERVER_URL}/api/fetch-custom-headers`, { | ||
| headers: { | ||
| ...traceData, | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }).then(res => res.text()); | ||
| // fetch without manual headers (baseline - auto-instrumentation only) | ||
| await fetch(`${process.env.SERVER_URL}/api/fetch`).then(res => res.text()); | ||
| // http.request with manual getTraceData() headers | ||
| await new Promise((resolve, reject) => { | ||
| const url = new URL(`${process.env.SERVER_URL}/api/http-custom-headers`); | ||
| const req = http.request( | ||
| { | ||
| hostname: url.hostname, | ||
| port: url.port, | ||
| path: url.pathname, | ||
| method: 'GET', | ||
| headers: { | ||
| ...traceData, | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }, | ||
| res => { | ||
| res.on('data', () => {}); | ||
| res.on('end', () => resolve()); | ||
| }, | ||
| ); | ||
| req.on('error', reject); | ||
| req.end(); | ||
| }); | ||
| Sentry.captureException(new Error('done')); | ||
| } | ||
| run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { createTestServer } from '@sentry-internal/test-utils'; | ||
| import { describe, expect } from 'vitest'; | ||
| import { createEsmAndCjsTests } from '../../../../utils/runner'; | ||
| import { expectConsistentTraceId, expectNoDuplicateSentryBaggageKeys, expectUserSetTraceId } from '../expects'; | ||
| describe('double baggage prevention', () => { | ||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('fetch with manual getTraceData() does not duplicate sentry baggage entries', async () => { | ||
| const [SERVER_URL, closeTestServer] = await createTestServer() | ||
| .get('/api/fetch-custom-headers', headers => { | ||
| // fetch with manual getTraceData() headers | ||
| expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^[a-f\d]{32}-[a-f\d]{16}$/)); | ||
| expectNoDuplicateSentryBaggageKeys(headers['baggage']); | ||
| expectConsistentTraceId(headers); | ||
| expectUserSetTraceId(headers); | ||
| }) | ||
| .get('/api/fetch', headers => { | ||
| // fetch without manual headers (baseline) | ||
| expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^[a-f\d]{32}-[a-f\d]{16}$/)); | ||
| expectNoDuplicateSentryBaggageKeys(headers['baggage']); | ||
| expectConsistentTraceId(headers); | ||
| }) | ||
| .get('/api/http-custom-headers', headers => { | ||
| // http.request with manual getTraceData() headers | ||
| expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^[a-f\d]{32}-[a-f\d]{16}$/)); | ||
| expectNoDuplicateSentryBaggageKeys(headers['baggage']); | ||
| expectConsistentTraceId(headers); | ||
| expectUserSetTraceId(headers); | ||
| }) | ||
| .start(); | ||
| await createRunner() | ||
| .withEnv({ SERVER_URL }) | ||
| .ignore('transaction') | ||
| .expect({ | ||
| event: { | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'done' }], | ||
| }, | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| closeTestServer(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import http from 'http'; | ||
| async function run() { | ||
| const traceData = Sentry.getTraceData(); | ||
| // fetch with manual getTraceData() headers - the core reproduction case from #19158 | ||
| await fetch(`${process.env.SERVER_URL}/api/fetch-custom-headers`, { | ||
| headers: { | ||
| ...traceData, | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }).then(res => res.text()); | ||
| // fetch without manual headers (baseline - auto-instrumentation only) | ||
| await fetch(`${process.env.SERVER_URL}/api/fetch`, {}).then(res => res.text()); | ||
| // http.request with manual getTraceData() headers | ||
| await new Promise((resolve, reject) => { | ||
| const url = new URL(`${process.env.SERVER_URL}/api/http-custom-headers`); | ||
| const req = http.request( | ||
| { | ||
| hostname: url.hostname, | ||
| port: url.port, | ||
| path: url.pathname, | ||
| method: 'GET', | ||
| headers: { | ||
| ...traceData, | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }, | ||
| res => { | ||
| res.on('data', () => {}); | ||
| res.on('end', () => resolve()); | ||
| }, | ||
| ); | ||
| req.on('error', reject); | ||
| req.end(); | ||
| }); | ||
| Sentry.captureException(new Error('done')); | ||
| } | ||
| run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { createTestServer } from '@sentry-internal/test-utils'; | ||
| import { describe, expect } from 'vitest'; | ||
| import { createEsmAndCjsTests } from '../../../../utils/runner'; | ||
| import { expectConsistentTraceId, expectNoDuplicateSentryBaggageKeys, expectUserSetTraceId } from '../expects'; | ||
| describe('double baggage prevention', () => { | ||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('fetch with manual getTraceData() does not duplicate sentry baggage entries', async () => { | ||
| const [SERVER_URL, closeTestServer] = await createTestServer() | ||
| .get('/api/fetch-custom-headers', headers => { | ||
| // fetch with manual getTraceData() headers | ||
| expect(headers['sentry-trace']).not.toContain(','); | ||
| expectNoDuplicateSentryBaggageKeys(headers['baggage']); | ||
| expectConsistentTraceId(headers); | ||
| expectUserSetTraceId(headers); | ||
| }) | ||
| .get('/api/fetch', headers => { | ||
| // fetch without manual headers (baseline) | ||
| expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^[a-f\d]{32}-[a-f\d]{16}(-[01])?$/)); | ||
| expectNoDuplicateSentryBaggageKeys(headers['baggage']); | ||
| expectConsistentTraceId(headers); | ||
| }) | ||
| .get('/api/http-custom-headers', headers => { | ||
| // http.request with manual getTraceData() headers | ||
| expect(headers['sentry-trace']).not.toContain(','); | ||
| expectNoDuplicateSentryBaggageKeys(headers['baggage']); | ||
| expectConsistentTraceId(headers); | ||
| expectUserSetTraceId(headers); | ||
| }) | ||
| .start(); | ||
| await createRunner() | ||
| .withEnv({ SERVER_URL }) | ||
| .expect({ | ||
| event: { | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'done' }], | ||
| }, | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| closeTestServer(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import http from 'http'; | ||
| async function run() { | ||
| const traceData = Sentry.getTraceData(); | ||
| // fetch with manual getTraceData() headers - the core reproduction case from #19158 | ||
| await fetch(`${process.env.SERVER_URL}/api/fetch-custom-headers`, { | ||
| headers: { | ||
| ...traceData, | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }).then(res => res.text()); | ||
| // fetch without manual headers (baseline - auto-instrumentation only) | ||
| await fetch(`${process.env.SERVER_URL}/api/fetch`, { | ||
| headers: { | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }).then(res => res.text()); | ||
| // http.request with manual getTraceData() headers | ||
| await new Promise((resolve, reject) => { | ||
| const url = new URL(`${process.env.SERVER_URL}/api/http-custom-headers`); | ||
| const req = http.request( | ||
| { | ||
| hostname: url.hostname, | ||
| port: url.port, | ||
| path: url.pathname, | ||
| method: 'GET', | ||
| headers: { | ||
| ...traceData, | ||
| 'x-tracedata-sentry-trace': traceData['sentry-trace'], | ||
| 'x-tracedata-baggage': traceData.baggage, | ||
| }, | ||
| }, | ||
| res => { | ||
| res.on('data', () => {}); | ||
| res.on('end', () => resolve()); | ||
| }, | ||
| ); | ||
| req.on('error', reject); | ||
| req.end(); | ||
| }); | ||
| Sentry.captureException(new Error('done')); | ||
| } | ||
| Sentry.startSpan({ name: 'parent_span' }, () => run()); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the browser SDK already guarded against multiple sentry headers, I just strengthened the test assertions a bit.