-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(node): Ensure traces are propagated without spans in Node 22+ #16221
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
5 commits
Select commit
Hold shift + click to select a range
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
17 changes: 17 additions & 0 deletions
17
...es/node-integration-tests/suites/tracing/requests/http-no-tracing-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,17 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracePropagationTargets: [/\/v0/, 'v1'], | ||
integrations: [Sentry.httpIntegration({ spans: false })], | ||
transport: loggingTransport, | ||
// Ensure this gets a correct hint | ||
beforeBreadcrumb(breadcrumb, hint) { | ||
breadcrumb.data = breadcrumb.data || {}; | ||
const req = hint?.request; | ||
breadcrumb.data.ADDED_PATH = req?.path; | ||
return breadcrumb; | ||
}, | ||
}); |
43 changes: 43 additions & 0 deletions
43
...ages/node-integration-tests/suites/tracing/requests/http-no-tracing-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,43 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import * as http from 'http'; | ||
|
||
async function run() { | ||
Sentry.addBreadcrumb({ message: 'manual breadcrumb' }); | ||
|
||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v0`); | ||
await makeHttpGet(`${process.env.SERVER_URL}/api/v1`); | ||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v2`); | ||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v3`); | ||
|
||
Sentry.captureException(new Error('foo')); | ||
} | ||
|
||
run(); | ||
|
||
function makeHttpRequest(url) { | ||
return new Promise(resolve => { | ||
http | ||
.request(url, httpRes => { | ||
httpRes.on('data', () => { | ||
// we don't care about data | ||
}); | ||
httpRes.on('end', () => { | ||
resolve(); | ||
}); | ||
}) | ||
.end(); | ||
}); | ||
} | ||
|
||
function makeHttpGet(url) { | ||
return new Promise(resolve => { | ||
http.get(url, httpRes => { | ||
httpRes.on('data', () => { | ||
// we don't care about data | ||
}); | ||
httpRes.on('end', () => { | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
} |
204 changes: 204 additions & 0 deletions
204
dev-packages/node-integration-tests/suites/tracing/requests/http-no-tracing-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,204 @@ | ||
import { describe, expect } from 'vitest'; | ||
import { conditionalTest } from '../../../../utils'; | ||
import { createEsmAndCjsTests } from '../../../../utils/runner'; | ||
import { createTestServer } from '../../../../utils/server'; | ||
|
||
describe('outgoing http requests with tracing & spans disabled', () => { | ||
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
conditionalTest({ min: 22 })('node >=22', () => { | ||
test('outgoing http requests are correctly instrumented with tracing & spans disabled', async () => { | ||
expect.assertions(11); | ||
|
||
const [SERVER_URL, closeTestServer] = await createTestServer() | ||
.get('/api/v0', headers => { | ||
expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/)); | ||
expect(headers['sentry-trace']).not.toEqual('00000000000000000000000000000000-0000000000000000'); | ||
expect(headers['baggage']).toEqual(expect.any(String)); | ||
}) | ||
.get('/api/v1', headers => { | ||
expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/)); | ||
expect(headers['sentry-trace']).not.toEqual('00000000000000000000000000000000-0000000000000000'); | ||
expect(headers['baggage']).toEqual(expect.any(String)); | ||
}) | ||
.get('/api/v2', headers => { | ||
expect(headers['baggage']).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
}) | ||
.get('/api/v3', headers => { | ||
expect(headers['baggage']).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
}) | ||
.start(); | ||
|
||
await createRunner() | ||
.withEnv({ SERVER_URL }) | ||
.ensureNoErrorOutput() | ||
.expect({ | ||
event: { | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'foo', | ||
}, | ||
], | ||
}, | ||
breadcrumbs: [ | ||
{ | ||
message: 'manual breadcrumb', | ||
timestamp: expect.any(Number), | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v0`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v0', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v1`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v1', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v2`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v2', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v3`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v3', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
], | ||
}, | ||
}) | ||
.start() | ||
.completed(); | ||
|
||
closeTestServer(); | ||
}); | ||
}); | ||
|
||
// On older node versions, outgoing requests do not get trace-headers injected, sadly | ||
// This is because the necessary diagnostics channel hook is not available yet | ||
conditionalTest({ max: 21 })('node <22', () => { | ||
test('outgoing http requests generate breadcrumbs correctly with tracing & spans disabled', async () => { | ||
expect.assertions(9); | ||
|
||
const [SERVER_URL, closeTestServer] = await createTestServer() | ||
.get('/api/v0', headers => { | ||
// This is not instrumented, sadly | ||
expect(headers['baggage']).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
}) | ||
.get('/api/v1', headers => { | ||
// This is not instrumented, sadly | ||
expect(headers['baggage']).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
}) | ||
.get('/api/v2', headers => { | ||
expect(headers['baggage']).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
}) | ||
.get('/api/v3', headers => { | ||
expect(headers['baggage']).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
}) | ||
.start(); | ||
|
||
await createRunner() | ||
.withEnv({ SERVER_URL }) | ||
.ensureNoErrorOutput() | ||
.expect({ | ||
event: { | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'foo', | ||
}, | ||
], | ||
}, | ||
breadcrumbs: [ | ||
{ | ||
message: 'manual breadcrumb', | ||
timestamp: expect.any(Number), | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v0`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v0', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v1`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v1', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v2`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v2', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v3`, | ||
status_code: 200, | ||
ADDED_PATH: '/api/v3', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
], | ||
}, | ||
}) | ||
.start() | ||
.completed(); | ||
|
||
closeTestServer(); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.