|
1 | 1 | import { afterAll, describe, expect } from 'vitest'; |
| 2 | +import { conditionalTest } from '../../../utils'; |
2 | 3 | import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
3 | 4 |
|
4 | 5 | describe('fastify auto-instrumentation', () => { |
@@ -46,38 +47,44 @@ describe('fastify auto-instrumentation', () => { |
46 | 47 | await runner.completed(); |
47 | 48 | }); |
48 | 49 |
|
49 | | - test('captures errors thrown in route handlers', async () => { |
50 | | - const runner = createRunner() |
51 | | - .ignore('transaction') |
52 | | - .expect({ |
53 | | - event: { |
54 | | - exception: { |
55 | | - values: [ |
56 | | - { |
57 | | - type: 'Error', |
58 | | - value: 'This is an exception with id 123', |
59 | | - mechanism: { |
60 | | - type: 'auto.function.fastify', |
61 | | - handled: false, |
| 50 | + // Fastify v5 only publishes the `tracing:fastify.request.handler:error` diagnostics channel when |
| 51 | + // `tracingChannel(...).hasSubscribers` is truthy. That aggregate getter does not exist on Node 18 |
| 52 | + // (it was added in Node 20), so fastify takes the fast path and never publishes the channel there — |
| 53 | + // making automatic error capture (without `setupFastifyErrorHandler`) impossible on Node 18. |
| 54 | + conditionalTest({ min: 20 })('error capture via diagnostics channel', () => { |
| 55 | + test('captures errors thrown in route handlers', async () => { |
| 56 | + const runner = createRunner() |
| 57 | + .ignore('transaction') |
| 58 | + .expect({ |
| 59 | + event: { |
| 60 | + exception: { |
| 61 | + values: [ |
| 62 | + { |
| 63 | + type: 'Error', |
| 64 | + value: 'This is an exception with id 123', |
| 65 | + mechanism: { |
| 66 | + type: 'auto.function.fastify', |
| 67 | + handled: false, |
| 68 | + }, |
62 | 69 | }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + transaction: 'GET /test-exception/:id', |
| 73 | + // The error must be parented to the fastify request span (not the root `http.server` span), |
| 74 | + // so the trace context carries a `parent_span_id`. |
| 75 | + contexts: { |
| 76 | + trace: { |
| 77 | + trace_id: expect.stringMatching(/[a-f0-9]{32}/), |
| 78 | + span_id: expect.stringMatching(/[a-f0-9]{16}/), |
| 79 | + parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), |
63 | 80 | }, |
64 | | - ], |
65 | | - }, |
66 | | - transaction: 'GET /test-exception/:id', |
67 | | - // The error must be parented to the fastify request span (not the root `http.server` span), |
68 | | - // so the trace context carries a `parent_span_id`. |
69 | | - contexts: { |
70 | | - trace: { |
71 | | - trace_id: expect.stringMatching(/[a-f0-9]{32}/), |
72 | | - span_id: expect.stringMatching(/[a-f0-9]{16}/), |
73 | | - parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), |
74 | 81 | }, |
75 | 82 | }, |
76 | | - }, |
77 | | - }) |
78 | | - .start(); |
79 | | - runner.makeRequest('get', '/test-exception/123', { expectError: true }); |
80 | | - await runner.completed(); |
| 83 | + }) |
| 84 | + .start(); |
| 85 | + runner.makeRequest('get', '/test-exception/123', { expectError: true }); |
| 86 | + await runner.completed(); |
| 87 | + }); |
81 | 88 | }); |
82 | 89 |
|
83 | 90 | test('propagates trace data to outgoing requests within a request handler', async () => { |
|
0 commit comments