-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(fastify): Update scope transactionName
when handling request
#11447
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify'; | ||
import { captureException, defineIntegration } from '@sentry/core'; | ||
import { captureException, defineIntegration, getIsolationScope } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
|
||
import { addOriginToSpan } from '../../utils/addOriginToSpan'; | ||
|
@@ -35,6 +35,19 @@ interface Fastify { | |
addHook: (hook: string, handler: (request: unknown, reply: unknown, error: Error) => void) => void; | ||
} | ||
|
||
/** | ||
* Minimal request type containing properties around route information. | ||
* Works for Fastify 3, 4 and presumably 5. | ||
*/ | ||
interface FastifyRequestRouteInfo { | ||
// since fastify@4.10.0 | ||
routeOptions?: { | ||
url?: string; | ||
method?: string; | ||
}; | ||
routerPath?: string; | ||
} | ||
|
||
/** | ||
* Setup an error handler for Fastify. | ||
*/ | ||
|
@@ -45,6 +58,20 @@ export function setupFastifyErrorHandler(fastify: Fastify): void { | |
captureException(error); | ||
}); | ||
|
||
// registering `onRequest` hook here instead of using Otel `onRequest` callback b/c `onRequest` hook | ||
// is ironically called in the fastify `preHandler` hook which is called later in the lifecycle: | ||
// https://fastify.dev/docs/latest/Reference/Lifecycle/ | ||
fastify.addHook('onRequest', async (request, _reply) => { | ||
const reqWithRouteInfo = request as FastifyRequestRouteInfo; | ||
|
||
// Taken from Otel Fastify instrumentation: | ||
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts#L94-L96 | ||
const routeName = reqWithRouteInfo.routeOptions?.url || reqWithRouteInfo.routerPath; | ||
const method = reqWithRouteInfo.routeOptions?.method || 'GET'; | ||
|
||
getIsolationScope().setTransactionName(`${method} ${routeName}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am honestly wondering if we are grabbing the right isolation scope inside these hooks. Gut feeling wise it could I'd say no 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should verify this for sure - there may be a race condition if this hook is executed before the http.server span is created, that is correct 😬 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this locally and the isolation scope already seems to be the forked one (i.e. not the default isolation scope). Added a check with a debug log nevertheless to ensure we don't pollute the default isolation scope. |
||
}); | ||
|
||
done(); | ||
}, | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The additional span here is a consequence of us adding another handler. I think this is fine unless someone has concerns.