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
feat(server-utils): Expose channel-based, streamlined fastifyIntegration#21706
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
fc67e434bc256bd4523d661a67653dd40421b9b80bd411d26320910efe620438de49809eff30f9020bb5File 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 |
|---|---|---|
| @@ -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,44 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { sendPortToRunner } from '@sentry-internal/node-integration-tests'; | ||
| import Fastify from 'fastify'; | ||
| const app = Fastify(); | ||
| let port; | ||
| app.get( | ||
| '/test-transaction', | ||
| { | ||
| preHandler: function routePreHandler(_request, _reply, done) { | ||
| done(); | ||
| }, | ||
| }, | ||
| async () => { | ||
| Sentry.startSpan({ name: 'test-span' }, () => { | ||
| Sentry.startSpan({ name: 'child-span' }, () => {}); | ||
| }); | ||
| return {}; | ||
| }, | ||
| ); | ||
| app.get('/test-exception/:id', async request => { | ||
| throw new Error(`This is an exception with id ${request.params.id}`); | ||
| }); | ||
| app.get('/test-inbound-headers/:id', async request => { | ||
| return { headers: request.headers, id: request.params.id }; | ||
| }); | ||
| app.get('/test-outgoing-fetch/:id', async request => { | ||
| const response = await fetch(`http://localhost:${port}/test-inbound-headers/${request.params.id}`); | ||
| return response.json(); | ||
| }); | ||
| const run = async () => { | ||
| await app.listen({ port: 0, host: 'localhost' }); | ||
| port = app.server.address().port; | ||
| sendPortToRunner(port); | ||
| }; | ||
| run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { conditionalTest } from '../../../utils'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
| describe('fastify auto-instrumentation', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
| // Fastify v5 does not support Node 18 | ||
| conditionalTest({ min: 20 })('fastify v5', () => { | ||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { | ||
| test('creates transaction with fastify hook, request-handler and manual spans', async () => { | ||
| const runner = createRunner() | ||
| .expect({ | ||
| transaction: { | ||
| transaction: 'GET /test-transaction', | ||
| spans: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| op: 'hook.fastify', | ||
| origin: 'auto.http.otel.fastify', | ||
| data: expect.objectContaining({ | ||
| 'fastify.type': 'hook', | ||
| 'sentry.op': 'hook.fastify', | ||
| 'sentry.origin': 'auto.http.otel.fastify', | ||
| }), | ||
| }), | ||
| expect.objectContaining({ | ||
| op: 'request_handler.fastify', | ||
| origin: 'auto.http.otel.fastify', | ||
| data: expect.objectContaining({ | ||
| 'sentry.op': 'request_handler.fastify', | ||
| 'sentry.origin': 'auto.http.otel.fastify', | ||
| }), | ||
| }), | ||
| expect.objectContaining({ | ||
| description: 'test-span', | ||
| origin: 'manual', | ||
| }), | ||
| expect.objectContaining({ | ||
| description: 'child-span', | ||
| origin: 'manual', | ||
| }), | ||
| ]), | ||
| }, | ||
| }) | ||
| .start(); | ||
| runner.makeRequest('get', '/test-transaction'); | ||
| await runner.completed(); | ||
| }); | ||
| // Fastify v5 only publishes the `tracing:fastify.request.handler:error` diagnostics channel when | ||
| // `tracingChannel(...).hasSubscribers` is truthy. That aggregate getter does not exist on Node 18 | ||
| // (it was added in Node 20), so fastify takes the fast path and never publishes the channel there — | ||
| // making automatic error capture (without `setupFastifyErrorHandler`) impossible on Node 18. | ||
| conditionalTest({ min: 20 })('error capture via diagnostics channel', () => { | ||
| test('captures errors thrown in route handlers', async () => { | ||
| const runner = createRunner() | ||
| .ignore('transaction') | ||
| .expect({ | ||
| event: { | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'This is an exception with id 123', | ||
| mechanism: { | ||
| type: 'auto.function.fastify', | ||
| handled: false, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| transaction: 'GET /test-exception/:id', | ||
| // The error must be parented to the fastify request span (not the root `http.server` span), | ||
| // so the trace context carries a `parent_span_id`. | ||
| contexts: { | ||
| trace: { | ||
| trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| .start(); | ||
| runner.makeRequest('get', '/test-exception/123', { expectError: true }); | ||
| await runner.completed(); | ||
| }); | ||
| }); | ||
| test('propagates trace data to outgoing requests within a request handler', async () => { | ||
| const runner = createRunner().start(); | ||
| const response = await runner.makeRequest<{ headers: Record<string, string> }>( | ||
| 'get', | ||
| '/test-outgoing-fetch/123', | ||
| ); | ||
| expect(response?.headers?.['sentry-trace']).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-1$/); | ||
| expect(response?.headers?.['baggage']).toEqual(expect.any(String)); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.