Skip to content

feat(node): Migrate to @fastify/otel #15542

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

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7905b7c
feat(node): Migrate to `@fastify/otel`
onurtemizkan Feb 28, 2025
e1b4e69
Inline fastify types
onurtemizkan Feb 28, 2025
47836c3
Use default `FastifyOtelInstrumentation` export
onurtemizkan Feb 28, 2025
9c65fcf
Update types.
onurtemizkan Feb 28, 2025
a77ffa0
Auto-register @fastify/otel plugin when instrumented.
onurtemizkan Feb 28, 2025
21a22c3
Remove version check.
onurtemizkan Feb 28, 2025
612a8f1
Switch using `diagnostics_channel`
onurtemizkan Mar 1, 2025
601c5fe
Fix formatting.
onurtemizkan Mar 5, 2025
98e6331
Bump `@fastify/otel` to `0.5.0`
onurtemizkan Mar 20, 2025
4922148
Add vendor permalinks
onurtemizkan Mar 25, 2025
07faa5a
Move fastify into its own folder.
onurtemizkan Mar 25, 2025
179037a
Update e2e tests
onurtemizkan Mar 25, 2025
08cf29b
Fix formatting
onurtemizkan Mar 25, 2025
583c9d8
Deduplicate deps
onurtemizkan Mar 26, 2025
66c5e16
Reset lockfile
onurtemizkan Mar 26, 2025
641629c
Use `skipLibCheck` in e2e tests
onurtemizkan Mar 26, 2025
125e675
Address review comments
onurtemizkan Apr 3, 2025
097060c
Bump `@fastify/otel` to `0.5.2`
onurtemizkan Apr 14, 2025
275a14d
Dedupe deps
onurtemizkan Apr 14, 2025
964cf0c
Reset lockfile
onurtemizkan Apr 14, 2025
8ff5f09
Add middie spans back to e2e tests
onurtemizkan Apr 15, 2025
d429cdd
Realign spans between versions
onurtemizkan Apr 18, 2025
12cfd91
Bump Fastify 5 versions to `5.3.1`
onurtemizkan Apr 18, 2025
84e8591
Set `fastify` as external
onurtemizkan Apr 18, 2025
bf9af14
Dedupe deps
onurtemizkan Apr 18, 2025
c7782e5
Try when `fastify` is imported as a `type` on `@fastify/otel`
onurtemizkan Apr 18, 2025
c3c8dda
Merge branch 'develop' into onur/fastify-otel-migration
lforst Apr 22, 2025
1d0732d
Bump `@fastify/otel` to `0.6.0`
onurtemizkan Apr 22, 2025
5d66586
Bump fastify of E2E test
onurtemizkan Apr 22, 2025
b5a489f
Merge branch 'develop' into onur/fastify-otel-migration
onurtemizkan Apr 22, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,39 @@ test('Sends an API route transaction', async ({ baseURL }) => {
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
data: {
'sentry.origin': 'manual',
'fastify.type': 'middleware',
'plugin.name': 'fastify -> @fastify/middie',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have no more plugin/middleware spans like this anymore? Is this "OK"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked again after the updates. We still have the middie spans, I added them back to the tests. But the problem is now that we don't have access to the requestHook anymore, the spans are not formatted correctly in NestJS applications (when setupFastifyErrorHandler is not used). We attempt using addFastifySpanAttributes inside setupFastifyErrorHandler.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now trying to use diagnosticsChannel for it. Maybe we can reach the context from it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try to PR something like the requestHook to the otel instrumentation, maybe? Or is this something they do not want?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mydea, they released a way to reach the spans: https://github.com/fastify/otel/releases/tag/v0.6.0

The problem is that with this release, they removed the vendored types we added previously. Adding fastify here as a devDependency works for TypeScript 5, but not in previous versions. Should we make this an exception for TS-3.8 support/tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they removed the vendored types

what do you mean, can we not replicate this anymore? 😢

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, yes: https://github.com/fastify/otel/pull/46/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8

Had to remove the previously vendored types due to conflicts with interface augmentation typical in fastify plugins.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mydea - Looks like they have overridden the types of fastify. So, it works now without adding fastify as a dependency. Please bear with me when I generate sample events and post links.

'hook.name': 'onRequest',
'sentry.origin': 'auto.http.otel.fastify',
'sentry.op': 'hook.fastify',
'service.name': 'fastify',
'hook.name': 'fastify -> @fastify/otel -> @fastify/middie - onRequest',
'fastify.type': 'hook',
'hook.callback.name': 'runMiddie',
},
description: 'middleware - runMiddie',
description: '@fastify/middie - onRequest',
op: 'hook.fastify',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
status: 'ok',
origin: 'manual',
origin: 'auto.http.otel.fastify',
},
{
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
data: {
'sentry.origin': 'auto.http.otel.fastify',
'sentry.op': 'request_handler.fastify',
'plugin.name': 'fastify -> @fastify/middie',
'fastify.type': 'request_handler',
'sentry.op': 'request-handler.fastify',
'service.name': 'fastify',
'hook.name': 'fastify -> @fastify/otel -> @fastify/middie - route-handler',
'fastify.type': 'request-handler',
'http.route': '/test-transaction',
'hook.callback.name': 'anonymous',
},
description: '@fastify/middie',
description: '@fastify/middie - route-handler',
op: 'request-handler.fastify',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
status: 'ok',
op: 'request_handler.fastify',
origin: 'auto.http.otel.fastify',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"esModuleInterop": true,
"lib": ["dom", "dom.iterable", "esnext"],
"strict": true,
"noEmit": true
"noEmit": true,
"skipLibCheck": true
},
"include": ["src/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"esModuleInterop": true,
"lib": ["es2018"],
"strict": true,
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"esModuleInterop": true,
"lib": ["es2018"],
"strict": true,
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"esModuleInterop": true,
"lib": ["es2020"],
"strict": true,
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "node-fastify",
"name": "node-fastify-3",
"version": "1.0.0",
"private": true,
"scripts": {
Expand All @@ -15,7 +15,7 @@
"@sentry/core": "latest || *",
"@sentry/opentelemetry": "latest || *",
"@types/node": "^18.19.1",
"fastify": "4.23.2",
"fastify": "3.29.5",
"typescript": "~5.0.0",
"ts-node": "10.9.1"
},
Expand Down
153 changes: 153 additions & 0 deletions dev-packages/e2e-tests/test-applications/node-fastify-3/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import type * as S from '@sentry/node';
const Sentry = require('@sentry/node') as typeof S;

// We wrap console.warn to find out if a warning is incorrectly logged
console.warn = new Proxy(console.warn, {
apply: function (target, thisArg, argumentsList) {
const msg = argumentsList[0];
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) {
console.error(`Sentry warning was triggered: ${msg}`);
process.exit(1);
}

return target.apply(thisArg, argumentsList);
},
});

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});

import type * as H from 'http';
import type * as F from 'fastify';

// Make sure fastify is imported after Sentry is initialized
const { fastify } = require('fastify') as typeof F;
const http = require('http') as typeof H;

const app = fastify();
const port = 3030;
const port2 = 3040;

Sentry.setupFastifyErrorHandler(app);

app.get('/test-success', function (_req, res) {
res.send({ version: 'v1' });
});

app.get<{ Params: { param: string } }>('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});

app.get<{ Params: { id: string } }>('/test-inbound-headers/:id', function (req, res) {
const headers = req.headers;

res.send({ headers, id: req.params.id });
});

app.get<{ Params: { id: string } }>('/test-outgoing-http/:id', async function (req, res) {
const id = req.params.id;
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`);

res.send(data);
});

app.get<{ Params: { id: string } }>('/test-outgoing-fetch/:id', async function (req, res) {
const id = req.params.id;
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`);
const data = await response.json();

res.send(data);
});

app.get('/test-transaction', async function (req, res) {
Sentry.startSpan({ name: 'test-span' }, () => {
Sentry.startSpan({ name: 'child-span' }, () => {});
});

res.send({});
});

app.get('/test-error', async function (req, res) {
const exceptionId = Sentry.captureException(new Error('This is an error'));

await Sentry.flush(2000);

res.send({ exceptionId });
});

app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) {
throw new Error(`This is an exception with id ${req.params.id}`);
});

app.get('/test-outgoing-fetch-external-allowed', async function (req, res) {
const fetchResponse = await fetch(`http://localhost:${port2}/external-allowed`);
const data = await fetchResponse.json();

res.send(data);
});

app.get('/test-outgoing-fetch-external-disallowed', async function (req, res) {
const fetchResponse = await fetch(`http://localhost:${port2}/external-disallowed`);
const data = await fetchResponse.json();

res.send(data);
});

app.get('/test-outgoing-http-external-allowed', async function (req, res) {
const data = await makeHttpRequest(`http://localhost:${port2}/external-allowed`);
res.send(data);
});

app.get('/test-outgoing-http-external-disallowed', async function (req, res) {
const data = await makeHttpRequest(`http://localhost:${port2}/external-disallowed`);
res.send(data);
});

app.listen({ port: port });

// A second app so we can test header propagation between external URLs
const app2 = fastify();
app2.get('/external-allowed', function (req, res) {
const headers = req.headers;

res.send({ headers, route: '/external-allowed' });
});

app2.get('/external-disallowed', function (req, res) {
const headers = req.headers;

res.send({ headers, route: '/external-disallowed' });
});

app2.listen({ port: port2 });

function makeHttpRequest(url: string) {
return new Promise(resolve => {
const data: any[] = [];

http
.request(url, httpRes => {
httpRes.on('data', chunk => {
data.push(chunk);
});
httpRes.on('error', error => {
resolve({ error: error.message, url });
});
httpRes.on('end', () => {
try {
const json = JSON.parse(Buffer.concat(data).toString());
resolve(json);
} catch {
resolve({ data: Buffer.concat(data).toString(), url });
}
});
})
.end();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { startEventProxyServer } from '@sentry-internal/test-utils';

startEventProxyServer({
port: 3031,
proxyServerName: 'node-fastify',
proxyServerName: 'node-fastify-3',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/test-utils';

test('Sends correct error event', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-fastify-3', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123';
});

await fetch(`${baseURL}/test-exception/123`);

const errorEvent = await errorEventPromise;

expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123');

expect(errorEvent.request).toEqual({
method: 'GET',
cookies: {},
headers: expect.any(Object),
url: 'http://localhost:3030/test-exception/123',
});

expect(errorEvent.transaction).toEqual('GET /test-exception/:id');

expect(errorEvent.contexts?.trace).toEqual({
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}/),
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { SpanJSON } from '@sentry/core';
test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
const id = crypto.randomUUID();

const inboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const inboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-inbound-headers/${id}`
);
});

const outboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const outboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-outgoing-http/${id}`
Expand Down Expand Up @@ -120,14 +120,14 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
test('Propagates trace for outgoing fetch requests', async ({ baseURL }) => {
const id = crypto.randomUUID();

const inboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const inboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-inbound-headers/${id}`
);
});

const outboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const outboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-outgoing-fetch/${id}`
Expand Down Expand Up @@ -232,7 +232,7 @@ test('Propagates trace for outgoing fetch requests', async ({ baseURL }) => {
});

test('Propagates trace for outgoing external http requests', async ({ baseURL }) => {
const inboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const inboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-outgoing-http-external-allowed`
Expand Down Expand Up @@ -269,7 +269,7 @@ test('Propagates trace for outgoing external http requests', async ({ baseURL })
});

test('Does not propagate outgoing http requests not covered by tracePropagationTargets', async ({ baseURL }) => {
const inboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const inboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-outgoing-http-external-disallowed`
Expand All @@ -293,7 +293,7 @@ test('Does not propagate outgoing http requests not covered by tracePropagationT
});

test('Propagates trace for outgoing external fetch requests', async ({ baseURL }) => {
const inboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const inboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-outgoing-fetch-external-allowed`
Expand Down Expand Up @@ -330,7 +330,7 @@ test('Propagates trace for outgoing external fetch requests', async ({ baseURL }
});

test('Does not propagate outgoing fetch requests not covered by tracePropagationTargets', async ({ baseURL }) => {
const inboundTransactionPromise = waitForTransaction('node-fastify', transactionEvent => {
const inboundTransactionPromise = waitForTransaction('node-fastify-3', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent.contexts?.trace?.data?.['http.target'] === `/test-outgoing-fetch-external-disallowed`
Expand Down
Loading