Skip to content

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

Merged
merged 3 commits into from
Apr 9, 2024

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Apr 5, 2024

This PR updates the isolation scope's transactionName whenever the requestHook Otel instrumentation hook is invoked and we have a recording span (non-recording spans don't have a attributes :( )

Update: Changed the transactionName updating mechanism to adding another hook callback in our setupFastifyErrorHandler fastify plugin. This is better than accessing the otel span for two reasons:

  1. We can always update the transactionName, not just when a span is sampled and recording
  2. The onRequest hook is executed earlier in the lifecycle than the preHandler hook that Otel uses to trigger the requestHook callback.

Also, we now access the same fields to read the route name as Otel.

adjusted existing e2e slightly to cover a paramterized route and to account for the additional span caused by our new onRequest handler.

ref #10846

@Lms24 Lms24 changed the base branch from develop to lms/feat-node-update-scope-transactionName April 5, 2024 10:44
@Lms24 Lms24 self-assigned this Apr 5, 2024
@@ -56,6 +56,23 @@ test('Sends an API route transaction', async ({ baseURL }) => {
expect(transactionEvent).toEqual(
expect.objectContaining({
spans: [
{
Copy link
Member Author

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.

Copy link
Contributor

github-actions bot commented Apr 5, 2024

size-limit report 📦

Path Size
@sentry/browser 22.1 KB (+0.11% 🔺)
@sentry/browser (incl. Tracing) 31.75 KB (+0.21% 🔺)
@sentry/browser (incl. Tracing, Replay) 66.87 KB (-0.05% 🔽)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 60.44 KB (-0.09% 🔽)
@sentry/browser (incl. Tracing, Replay with Canvas) 70.73 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) 75.65 KB (+0.01% 🔺)
@sentry/browser (incl. Feedback) 0 B (removed)
@sentry/browser (incl. Feedback, Feedback Modal) 0 B (removed)
@sentry/browser (incl. Feedback, Feedback Modal, Feedback Screenshot) 0 B (removed)
@sentry/browser (incl. sendFeedback) 26.89 KB (+0.1% 🔺)
@sentry/react 22.12 KB (-10.64% 🔽)
@sentry/react (incl. Tracing) 0 B (removed)
@sentry/vue 0 B (removed)
@sentry/vue (incl. Tracing) 0 B (removed)
@sentry/svelte 0 B (removed)
CDN Bundle 24.13 KB (+0.21% 🔺)
CDN Bundle (incl. Tracing) 32.7 KB (+0.22% 🔺)
CDN Bundle (incl. Tracing, Replay) 66.38 KB (+0.06% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) 71.63 KB (+0.05% 🔺)
CDN Bundle - uncompressed 71.37 KB (-0.36% 🔽)
CDN Bundle (incl. Tracing) - uncompressed 97.59 KB (-0.08% 🔽)
CDN Bundle (incl. Tracing, Replay) - uncompressed 206.99 KB (-0.04% 🔽)
@sentry/nextjs (client) 0 B (removed)
@sentry/sveltekit (client) 0 B (removed)
@sentry/node 0 B (removed)
@sentry/browser (incl. browserTracingIntegration) 31.75 KB (added)
@sentry/browser (incl. feedbackIntegration) 30.88 KB (added)
@sentry/browser (incl. feedbackModalIntegration) 30.9 KB (added)
@sentry/browser (incl. feedbackScreenshotIntegration) 30.9 KB (added)
@sentry/react (incl. Tracing, Replay) 66.99 KB (added)

Base automatically changed from lms/feat-node-update-scope-transactionName to develop April 8, 2024 11:11
@Lms24 Lms24 force-pushed the lms/feat-fastify-update-scope-transactionName branch from 0f8abff to 2d7ee55 Compare April 8, 2024 11:13
@Lms24 Lms24 requested review from mydea and lforst April 8, 2024 11:13
@Lms24 Lms24 marked this pull request as ready for review April 8, 2024 11:14
const routeName = reqWithRouteInfo.routeOptions?.url || reqWithRouteInfo.routerPath;
const method = reqWithRouteInfo.routeOptions?.method || 'GET';

getIsolationScope().setTransactionName(`${method} ${routeName}`);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 🤔

Copy link
Member

Choose a reason for hiding this comment

The 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 😬

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

@Lms24 Lms24 merged commit 28b7d75 into develop Apr 9, 2024
@Lms24 Lms24 deleted the lms/feat-fastify-update-scope-transactionName branch April 9, 2024 15:33
cadesalaberry pushed a commit to cadesalaberry/sentry-javascript that referenced this pull request Apr 19, 2024
…etsentry#11447)

This PR updates the isolation scope's `transactionName` by adding another
hook callback in our `setupFastifyErrorHandler` fastify plugin. This is
better than accessing the otel span for two reasons:

1. We can always update the transactionName, not just when a span is
sampled and recording
2. The `onRequest` hook is executed earlier in the lifecycle than the
`preHandler` hook that Otel uses to trigger the `requestHook` callback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants