Skip to content

feat(nextjs): Add opt-in for orchestrion instrumentation#22043

Draft
chargome wants to merge 8 commits into
developfrom
cg/nextjs-orchestrion-support
Draft

feat(nextjs): Add opt-in for orchestrion instrumentation#22043
chargome wants to merge 8 commits into
developfrom
cg/nextjs-orchestrion-support

Conversation

@chargome

@chargome chargome commented Jul 8, 2026

Copy link
Copy Markdown
Member

Adds an experimental option for opting in to orchestrion instrumentation.

  • Once the opt-in flag is set we:
    • Un-externalize the bundle-safe instrumented packages; bundle-unsafe ones (mysql) stay external and are instrumented by the runtime module hook, which works because we also externalize the @apm-js-collab/* transformer packages.
    • transpilePackages for the ones that Next externalizes by default (e.g. pg) — removing them from serverExternalPackages isn't enough for Next's own defaults.
    • Inject the code-transform loader as a Turbopack rule and as a webpack plugin.
  • The webpack loader gets exported from our server utils package (which can be used for turbopack too)

Verified in e2e, which I will stack on top of this pr

closes #22009

@chargome chargome self-assigned this Jul 8, 2026
chargome and others added 6 commits July 8, 2026 10:06
Two fixes for diagnostics-channel injection under Turbopack:

Externalize @apm-js-collab/tracing-hooks and @apm-js-collab/code-transformer
when the flag is on. Bundled, the code transformer's parser breaks
('a.parse is not a function'), so the runtime module hook silently returned
untransformed sources and externalized packages never produced spans.

Keep mysql in serverExternalPackages instead of force-bundling it. Turbopack
cannot bundle mysql 2.x correctly (the wire-protocol handshake fails with
'Received packet in the wrong sequence' even untransformed). External, it is
now instrumented by the working runtime hook instead of the build-time loader.

Also drop the bundler marker from the orchestrion webpack plugin: it made
registerDiagnosticsChannelInjection() skip the runtime hook, but the hybrid
setup (loader for bundled deps, runtime hook for external ones) needs both.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Instead of un-externalizing every instrumented package and forcing the
Next-default-external ones through transpilePackages, only packages on an
explicit bundle-safe allowlist (currently ioredis) are removed from Sentry's
own serverExternalPackages defaults. Everything else — Next's defaults, the
user's externals, the rest of Sentry's defaults — stays external and is
instrumented by the runtime module hook on require, which works since the
orchestrion machinery is externalized.

This is safer: bundling a server package changes real behavior (mysql 2.x
corrupts its wire protocol when bundled by Turbopack), and new upstream
instrumentations (e.g. hapi) now default to the external/runtime-hook path
instead of silently becoming bundled. It also removes the Next
server-external-packages list parsing and the pg-native webpack workaround,
both only needed to support force-bundling.

Verified in the nextjs-16-orchestrion e2e: ioredis via the build-time loader,
pg and mysql via the runtime hook.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…t@13

Use createRequire(__filename) instead of aliasing the CJS require in the
orchestrion webpack bundler module. Next.js 13 bundles @sentry/nextjs into
the server build, and webpack flags the aliased require with 'Critical
dependency: require function is used in a way in which dependencies cannot
be statically extracted', which the nextjs-app-dir e2e treats as a failure.

Add isDiagnosticsChannelInjectionEnabled to the consistent-exports ignore
list: like its companions experimentalUseDiagnosticsChannelInjection and
diagnosticsChannelInjectionIntegrations, the Node-runtime-only opt-in is not
surfaced through the framework / serverless SDKs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@chargome

chargome commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

bugbot run

Comment thread packages/nextjs/src/config/webpack.ts
The plugin previously ran for all server compilations, including the edge
runtime, which diagnostics-channel injection does not target. Gate it on the
already-derived runtime instead of isServer and add unit tests covering the
server/edge/client/flag-off cases.

Also trim down the orchestrion-related comments.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@chargome

chargome commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a3a3595. Configure here.

'[@sentry/nextjs] `useDiagnosticsChannelInjection` is enabled in `withSentryConfig`, but ' +
'`Sentry.experimentalUseDiagnosticsChannelInjection()` was not called before `Sentry.init()`. ' +
'Server DB spans will not be recorded.',
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Init warning hidden in production

Medium Severity

The new check for a missing experimentalUseDiagnosticsChannelInjection() call uses DEBUG_BUILD && debug.warn, so production builds with only useDiagnosticsChannelInjection in withSentryConfig get no visible warning while server DB spans stay disabled.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit a3a3595. Configure here.

expect(externals).toContain('mysql');
expect(externals).not.toContain('@apm-js-collab/tracing-hooks');
});
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Feat lacks integration or E2E

Medium Severity

This feature PR adds unit and webpack config tests but no integration or E2E test in the diff, though the description references separate e2e verification. Review guidelines expect at least one integration or E2E test for feat changes.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit a3a3595. Configure here.

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.

Support orchestrion auto-instrumentation in turbopack

1 participant