Skip to content
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

feat(node): Only add span listeners for instrumentation when used #15802

Merged
merged 4 commits into from
Mar 26, 2025

Conversation

mydea
Copy link
Member

@mydea mydea commented Mar 24, 2025

Related to #15725 (comment), this PR is an idea to avoid registering on('spanStart') hooks for all the node libraries that do not have proper hooks, unless they are used.

Today, for OTEL instrumentation that does not provide span hooks, we fall back to client.on('spanStart') to enhance/mutate spans emitted by the instrumentation. This PR improved this by only registering the spanStart client hook if we detect that the OTEL instrumentation is actually wrapping something. This avoids us calling a bunch of span callbacks each time a span is started, when it is not even needed.

For this, a new instrumentWhenWrapped utility is added which can be passed an instrumentation. This works by monkey-patching _wrap on the instrumentation, and ensuring a callback is only called conditionally.

Note: For some (e.g. connect, fastify) we register spanStart in the error handler, which is even better, so there we do not need this logic.

@mydea mydea self-assigned this Mar 24, 2025
@mydea mydea force-pushed the fn/conditional-spanStart branch from 90d77ae to ee30339 Compare March 25, 2025 09:56
Copy link
Contributor

github-actions bot commented Mar 25, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.21 KB - -
@sentry/browser - with treeshaking flags 23.01 KB - -
@sentry/browser (incl. Tracing) 36.62 KB - -
@sentry/browser (incl. Tracing, Replay) 73.79 KB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.11 KB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 78.42 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 91 KB - -
@sentry/browser (incl. Feedback) 40.34 KB - -
@sentry/browser (incl. sendFeedback) 27.84 KB - -
@sentry/browser (incl. FeedbackAsync) 32.63 KB - -
@sentry/react 25 KB - -
@sentry/react (incl. Tracing) 38.51 KB - -
@sentry/vue 27.44 KB - -
@sentry/vue (incl. Tracing) 38.29 KB - -
@sentry/svelte 23.24 KB - -
CDN Bundle 24.43 KB - -
CDN Bundle (incl. Tracing) 36.63 KB - -
CDN Bundle (incl. Tracing, Replay) 71.61 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 76.82 KB - -
CDN Bundle - uncompressed 71.39 KB - -
CDN Bundle (incl. Tracing) - uncompressed 108.58 KB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 219.83 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 232.4 KB - -
@sentry/nextjs (client) 39.81 KB - -
@sentry/sveltekit (client) 37.03 KB - -
@sentry/node 143.03 KB +0.13% +181 B 🔺
@sentry/node - without tracing 96.25 KB +0.01% +3 B 🔺
@sentry/aws-serverless 120.59 KB +0.01% +4 B 🔺

View base workflow run

@mydea mydea changed the title feat(node): WIP Only add span listeners conditionally feat(node): Only add span listeners for instrumentation when used Mar 25, 2025
@mydea mydea marked this pull request as ready for review March 25, 2025 10:23
@@ -119,6 +125,12 @@ export const prismaIntegration = defineIntegration(
instrumentPrisma({ prismaInstrumentation });
},
setup(client) {
// If no tracing helper exists, we skip any work here
Copy link
Member Author

Choose a reason for hiding this comment

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

this is not perfect as possibly prisma could be set up later than setup(client) is being called, but it was the best I could come up with here 🤔 it seems to work in tests at least!

}

return event;
instrumentation = instrumentVercelAi();
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 convert this to use instrumentWhenWrapped.

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 was thinking about this - technically, this implementation is better and safer, as it does not rely on patching the method etc. Since we control the instrumentation class here 🤔

@mydea mydea merged commit 05391d0 into develop Mar 26, 2025
103 of 104 checks passed
@mydea mydea deleted the fn/conditional-spanStart branch March 26, 2025 09:54
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.

2 participants