Skip to content

perf(node): Short circuit flushing on Vercel only for Vercel #15734

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
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -9,6 +9,17 @@ import { stealthWrap } from './utils';
type Http = typeof http;
type Https = typeof https;

// The reason this "before OTEL" integration even exists is due to timing reasons. We need to be able to register a
// `res.on('close')` handler **after** OTEL registers its own handler (which it uses to end spans), so that we can do
// something (ie. flush) after OTEL has ended a span for a request. If you think about it like an onion:
//
// (Sentry after OTEL instrumentation
// (OTEL instrumentation
// (Sentry before OTEL instrumentation
// (orig HTTP request handler))))
//
// registering an instrumentation before OTEL allows us to do this for incoming requests.

/**
* A Sentry specific http instrumentation that is applied before the otel instrumentation.
*/
Expand Down
16 changes: 12 additions & 4 deletions packages/node/src/integrations/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) =>
return {
name: INTEGRATION_NAME,
setupOnce() {
instrumentSentryHttpBeforeOtel();
// Below, we instrument the Node.js HTTP API three times. 2 times Sentry-specific, 1 time OTEL specific.
// Due to timing reasons, we sometimes need to apply Sentry instrumentation _before_ we apply the OTEL
// instrumentation (e.g. to flush on serverless platforms), and sometimes we need to apply Sentry instrumentation
// _after_ we apply OTEL instrumentation (e.g. for isolation scope handling and breadcrumbs).

// This is Sentry-specific instrumentation that is applied _before_ any OTEL instrumentation.
if (process.env.VERCEL) {
// Currently this instrumentation only does something when deployed on Vercel, so to save some overhead, we short circuit adding it here only for Vercel.
// If it's functionality is extended in the future, feel free to remove the if statement and this comment.
instrumentSentryHttpBeforeOtel();
}

const instrumentSpans = _shouldInstrumentSpans(options, getClient<NodeClient>()?.getOptions());

Expand All @@ -152,9 +162,7 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) =>
instrumentOtelHttp(instrumentationConfig);
}

// This is the Sentry-specific instrumentation that isolates requests & creates breadcrumbs
// Note that this _has_ to be wrapped after the OTEL instrumentation,
// otherwise the isolation will not work correctly
// This is Sentry-specific instrumentation that is applied _after_ any OTEL instrumentation.
instrumentSentryHttp({
...options,
// If spans are not instrumented, it means the HttpInstrumentation has not been added
Expand Down
Loading