-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
12c3f51
wip
Lms24 63b7666
use dedupe approach
Lms24 8e7b3e8
fix bug in baggage merging
Lms24 32d3822
fix(node): Avoid duplicated `sentry-trace` and `baggage` headers on f…
Lms24 a0b37a4
lint
Lms24 e7eb933
harden tests, fix http integration and propagator
Lms24 7fb7814
remove console logs
Lms24 429dad1
adjust propagator tests
Lms24 9b0b7f9
convert undici node 18 string headers to array to dedupe and then bac…
Lms24 784e9df
always convert string headers to array headers (and back)
Lms24 978b88d
harden browser integration test
Lms24 a9ff1bc
cleanup, fix test, fix colon bug in string->array header conversion
Lms24 933a42f
more cleanup
Lms24 e300e33
even more cleanup
Lms24 4caafd8
more cleanup, fix edge case with odd headers, dedupe traceparent
Lms24 ff6b436
last cleanup, one more test
Lms24 e3e008e
improve mergeBaggageHeaders performance
Lms24 c9627af
remove unused imports
Lms24 6b77c61
log when failing to convert string to array header
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
|
Member
Author
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', | ||
| ]); | ||
|
|
||
37 changes: 37 additions & 0 deletions
37
dev-packages/node-integration-tests/suites/tracing/double-baggage/expects.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/double-baggage/no-spans/instrument.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }); |
45 changes: 45 additions & 0 deletions
45
dev-packages/node-integration-tests/suites/tracing/double-baggage/no-spans/scenario.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
47 changes: 47 additions & 0 deletions
47
dev-packages/node-integration-tests/suites/tracing/double-baggage/no-spans/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
...kages/node-integration-tests/suites/tracing/double-baggage/spans-no-parent/instrument.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }); |
45 changes: 45 additions & 0 deletions
45
...ackages/node-integration-tests/suites/tracing/double-baggage/spans-no-parent/scenario.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
46 changes: 46 additions & 0 deletions
46
dev-packages/node-integration-tests/suites/tracing/double-baggage/spans-no-parent/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
...packages/node-integration-tests/suites/tracing/double-baggage/spans-parent/instrument.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }); |
50 changes: 50 additions & 0 deletions
50
dev-packages/node-integration-tests/suites/tracing/double-baggage/spans-parent/scenario.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.