Repository navigation
feat(node): Instrument HTTP/2 servers that use the compat API #25992
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
+309
−14
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebbe33a
feat(node): Instrument HTTP/2 servers that use the compat API
timfish cdb9d35
Merge branch 'develop' into feat/node-http2-server-channels
timfish 4bff77e
test(node): Fix HTTP/2 server test ordering and Node version gate
timfish 72d161d
test(node): Gate the HTTP/2 server suite with supports({ nodeMin })
timfish 56330d7
Merge remote-tracking branch 'origin/develop' into feat/node-http2-se…
timfish f58ee45
test(node): Gate HTTP/2 server suite on Node 22
timfish 8f40407
Merge branch 'develop' into feat/node-http2-server-channels
timfish 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/http2-server/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, | ||
| }); |
26 changes: 26 additions & 0 deletions
26
dev-packages/node-integration-tests/suites/tracing/http2-server/scenario-fastify.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,26 @@ | ||
| import Fastify from 'fastify'; | ||
| import http2 from 'node:http2'; | ||
|
|
||
| async function run() { | ||
| const app = Fastify({ http2: true }); | ||
|
|
||
| app.get('/users/:id', async () => { | ||
| return { ok: true }; | ||
| }); | ||
|
|
||
| await app.listen({ port: 0 }); | ||
|
|
||
| const client = http2.connect(`http://localhost:${app.server.address().port}`); | ||
| await new Promise((resolve, reject) => { | ||
| const stream = client.request({ ':path': '/users/42' }); | ||
| stream.on('error', reject); | ||
| stream.on('data', () => {}); | ||
| stream.on('end', resolve); | ||
| stream.end(); | ||
| }); | ||
|
|
||
| client.close(); | ||
| await app.close(); | ||
| } | ||
|
|
||
| run(); |
41 changes: 41 additions & 0 deletions
41
dev-packages/node-integration-tests/suites/tracing/http2-server/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,41 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import http2 from 'node:http2'; | ||
|
|
||
| const server = http2.createServer((request, response) => { | ||
| if (request.url === '/error') { | ||
| Sentry.captureException(new Error('error in http2 handler')); | ||
| response.statusCode = 500; | ||
| response.end(); | ||
| return; | ||
| } | ||
|
|
||
| Sentry.startSpan({ name: 'handler-work' }, () => {}); | ||
| response.end('ok'); | ||
| }); | ||
|
|
||
| function request(client, headers) { | ||
| return new Promise((resolve, reject) => { | ||
| const stream = client.request(headers); | ||
| stream.on('error', reject); | ||
| stream.on('data', () => {}); | ||
| stream.on('end', resolve); | ||
| stream.end(); | ||
| }); | ||
| } | ||
|
|
||
| server.listen(0, async () => { | ||
| const client = http2.connect(`http://localhost:${server.address().port}`); | ||
|
|
||
| await request(client, { | ||
| ':path': '/users/42?q=1', | ||
| 'sentry-trace': '12312012123120121231201212312012-1121201211212012-1', | ||
| baggage: 'sentry-release=2.1.0,sentry-environment=myEnv', | ||
| }); | ||
| await request(client, { | ||
| ':path': '/error', | ||
| 'sentry-trace': '45645645645645645645645645645645-4564564564564564-1', | ||
| }); | ||
|
|
||
| client.close(); | ||
| server.close(); | ||
| }); |
103 changes: 103 additions & 0 deletions
103
dev-packages/node-integration-tests/suites/tracing/http2-server/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,103 @@ | ||
| import { | ||
| HTTP_RESPONSE_STATUS_CODE, | ||
| NETWORK_PROTOCOL_VERSION, | ||
| SENTRY_OP, | ||
| SENTRY_ORIGIN, | ||
| SERVER_ADDRESS, | ||
| URL_PATH, | ||
| URL_QUERY, | ||
| } from '@sentry/conventions/attributes'; | ||
| import { HTTP_SERVER } from '@sentry/conventions/op'; | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { supports } from '../../../utils'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| describe.runIf(supports({ min: 22, runtimes: ['node'] }))('http2 server', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('isolates compat API requests and continues their traces', async () => { | ||
| await createRunner() | ||
| .unordered() | ||
| .expect({ | ||
| span: container => { | ||
| const segment = container.items.find(item => item.is_segment); | ||
| expect(segment?.attributes[URL_PATH]?.value).toBe('/users/42'); | ||
|
|
||
| expect(segment).toMatchObject({ | ||
| name: 'GET', | ||
| trace_id: '12312012123120121231201212312012', | ||
| parent_span_id: '1121201211212012', | ||
| status: 'ok', | ||
| }); | ||
| expect(segment?.attributes).toMatchObject({ | ||
| [SENTRY_OP]: { value: HTTP_SERVER }, | ||
| [SENTRY_ORIGIN]: { value: 'auto.http.http_server' }, | ||
| [URL_QUERY]: { value: 'q=1' }, | ||
| [SERVER_ADDRESS]: { value: 'localhost' }, | ||
| [NETWORK_PROTOCOL_VERSION]: { value: '2.0' }, | ||
| [HTTP_RESPONSE_STATUS_CODE]: { value: 200 }, | ||
| }); | ||
| expect(Object.keys(segment?.attributes ?? {}).filter(key => key.includes(':'))).toEqual([]); | ||
|
|
||
| const child = container.items.find(item => item.name === 'handler-work'); | ||
| expect(child?.parent_span_id).toBe(segment?.span_id); | ||
| }, | ||
| }) | ||
| .expect({ | ||
| event: event => { | ||
| expect(event.exception?.values?.[0]?.value).toBe('error in http2 handler'); | ||
| expect(event.request?.url).toMatch(/^http:\/\/localhost:\d+\/error$/); | ||
| expect(event.request?.method).toBe('GET'); | ||
| expect(Object.keys(event.request?.headers ?? {}).filter(key => key.startsWith(':'))).toEqual([]); | ||
| expect(event.transaction).toBe('GET /error'); | ||
| expect(event.contexts?.trace?.trace_id).toBe('45645645645645645645645645645645'); | ||
| }, | ||
| }) | ||
| .expect({ | ||
| span: container => { | ||
| const segment = container.items.find(item => item.is_segment); | ||
| expect(segment?.attributes[URL_PATH]?.value).toBe('/error'); | ||
| expect(segment?.status).toBe('error'); | ||
| expect(segment?.attributes[HTTP_RESPONSE_STATUS_CODE]?.value).toBe(500); | ||
| expect(segment?.trace_id).toBe('45645645645645645645645645645645'); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('does not trigger HTTP/2 compat API warnings', async () => { | ||
| await createRunner().ensureNoErrorOutput().start().completed(); | ||
| }); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario-fastify.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('nests fastify spans under the HTTP/2 server span', async () => { | ||
| await createRunner() | ||
| .expect({ | ||
| span: container => { | ||
| const segment = container.items.find(item => item.is_segment); | ||
| expect(segment?.name).toBe('GET /users/:id'); | ||
| expect(segment?.attributes).toMatchObject({ | ||
| [SENTRY_OP]: { value: HTTP_SERVER }, | ||
| [NETWORK_PROTOCOL_VERSION]: { value: '2.0' }, | ||
| [HTTP_RESPONSE_STATUS_CODE]: { value: 200 }, | ||
| }); | ||
|
|
||
| const fastifySpans = container.items.filter( | ||
| item => item.attributes[SENTRY_ORIGIN]?.value === 'auto.http.fastify', | ||
| ); | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| expect(fastifySpans.length).toBeGreaterThan(0); | ||
| for (const span of fastifySpans) { | ||
| expect(span.trace_id).toBe(segment?.trace_id); | ||
| } | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
| }); | ||
| }); | ||
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,5 +1,6 @@ | ||
| export const LOG_PREFIX = '@sentry/instrumentation-http'; | ||
| export const HTTP_ON_CLIENT_REQUEST = 'http.client.request.created'; | ||
| export const HTTP_ON_SERVER_REQUEST = 'http.server.request.start'; | ||
| export const HTTP2_ON_SERVER_STREAM = 'http2.server.stream.start'; | ||
| export type ClientSubscriptionName = typeof HTTP_ON_CLIENT_REQUEST; | ||
| export type ServerSubscriptionName = typeof HTTP_ON_SERVER_REQUEST; | ||
| export type ServerSubscriptionName = typeof HTTP_ON_SERVER_REQUEST | typeof HTTP2_ON_SERVER_STREAM; |
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
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
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.