Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .oxlintrc.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@
"**/integrations/tracing/amqplib/vendored/**/*.ts",
"**/integrations/tracing/prisma/vendored/**/*.ts",
"**/integrations/tracing/postgres/vendored/**/*.ts",
"**/integrations/tracing/fastify/vendored/**/*.ts",
"**/integrations/tracing/redis/vendored/**/*.ts"
],
"rules": {
Expand Down
1 change: 1 addition & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"cron": "^3.1.6",
"dataloader": "2.2.2",
"express": "^4.21.2",
"fastify": "^5.7.0",
"generic-pool": "^3.9.0",
"graphql": "^16.11.0",
"graphql-tag": "^2.12.6",
Expand Down
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}`);
Comment thread
mydea marked this conversation as resolved.
Dismissed
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,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));
});
});
});

This file was deleted.

Loading
Loading