Skip to content

Commit 74013eb

Browse files
committed
feat(fastify): Update scope transactionName when handling request
1 parent 1319ec7 commit 74013eb

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ app.get('/test-error', async function (req, res) {
5555
res.send({ exceptionId });
5656
});
5757

58-
app.get('/test-exception', async function (req, res) {
59-
throw new Error('This is an exception');
58+
app.get('/test-exception/:id', async function (req, res) {
59+
throw new Error(`This is an exception with id ${req.params.id}`);
6060
});
6161

6262
app.get('/test-outgoing-fetch-external-allowed', async function (req, res) {

dev-packages/e2e-tests/test-applications/node-fastify-app/tests/errors.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,28 @@ test('Sends exception to Sentry', async ({ baseURL }) => {
4141

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

4747
try {
48-
await axios.get(`${baseURL}/test-exception`);
48+
await axios.get(`${baseURL}/test-exception/123`);
4949
} catch {
5050
// this results in an error, but we don't care - we want to check the error event
5151
}
5252

5353
const errorEvent = await errorEventPromise;
5454

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

5858
expect(errorEvent.request).toEqual({
5959
method: 'GET',
6060
cookies: {},
6161
headers: expect.any(Object),
62-
url: 'http://localhost:3030/test-exception',
62+
url: 'http://localhost:3030/test-exception/123',
6363
});
6464

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

6767
expect(errorEvent.contexts?.trace).toEqual({
6868
trace_id: expect.any(String),

packages/node/src/integrations/tracing/fastify.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { registerInstrumentations } from '@opentelemetry/instrumentation';
2+
import type { FastifyRequestInfo } from '@opentelemetry/instrumentation-fastify';
23
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
3-
import { captureException, defineIntegration } from '@sentry/core';
4+
import { captureException, defineIntegration, getIsolationScope, spanToJSON } from '@sentry/core';
45
import type { IntegrationFn } from '@sentry/types';
56

7+
import type { Span } from '@opentelemetry/api';
68
import { addOriginToSpan } from '../../utils/addOriginToSpan';
79

810
const _fastifyIntegration = (() => {
@@ -12,8 +14,9 @@ const _fastifyIntegration = (() => {
1214
registerInstrumentations({
1315
instrumentations: [
1416
new FastifyInstrumentation({
15-
requestHook(span) {
17+
requestHook(span, info) {
1618
addOriginToSpan(span, 'auto.http.otel.fastify');
19+
updateScopeTransactionName(span, info);
1720
},
1821
}),
1922
],
@@ -22,6 +25,22 @@ const _fastifyIntegration = (() => {
2225
};
2326
}) satisfies IntegrationFn;
2427

28+
function updateScopeTransactionName(span: Span, info: FastifyRequestInfo): void {
29+
if (!span.isRecording()) {
30+
return;
31+
}
32+
33+
const attributes = spanToJSON(span).data;
34+
if (!attributes || !attributes['http.route']) {
35+
return;
36+
}
37+
38+
const req = info.request as { method?: string };
39+
const method = req.method?.toUpperCase() || 'GET';
40+
const route = String(attributes['http.route']);
41+
getIsolationScope().setTransactionName(`${method} ${route}`);
42+
}
43+
2544
/**
2645
* Express integration
2746
*

0 commit comments

Comments
 (0)