-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
98 lines (93 loc) · 3.89 KB
/
Copy pathtest.ts
File metadata and controls
98 lines (93 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { afterAll, describe, expect } from 'vitest';
import { conditionalTest } from '../../../utils';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';
describe('fastify auto-instrumentation', () => {
afterAll(() => {
cleanupChildProcesses();
});
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));
});
});
});