Skip to content

feat(node): Add build-time opt-out for runtime channel injection - #23475

Open
mydea wants to merge 1 commit into
developfrom
fn/channel-injection-build-flag
Open

feat(node): Add build-time opt-out for runtime channel injection#23475
mydea wants to merge 1 commit into
developfrom
fn/channel-injection-build-flag

Conversation

@mydea

@mydea mydea commented Aug 21, 2026

Copy link
Copy Markdown
Member

Stacked on #23473.

Adds a build-time opt-out for the Node SDK's runtime diagnostics-channel injection, complementing the runtime enableRuntimeChannelInjection option from the base PR.

  • New __SENTRY_CHANNEL_INJECTION__ treeshaking flag, mirroring __SENTRY_TRACING__: when a bundler text-replaces it with false, the registerDiagnosticsChannelInjection() / detectOrchestrionSetup() block in init is dropped, and its transitive orchestrion-register code tree-shakes away.
  • Exposed through the bundler plugins' bundleSizeOptimizations.excludeChannelInjection, which maps to __SENTRY_CHANNEL_INJECTION__ = false via the same mechanism as excludeTracing.
  • The gate now reads both the build flag and the runtime option: (typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) && options.enableRuntimeChannelInjection !== false.

🤖 Generated with Claude Code

Comment thread packages/node/test/sdk/diagnosticsChannelInjection.test.ts
@github-actions

github-actions Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 28.57 kB - -
@sentry/browser - with treeshaking flags 26.92 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.82 kB - -
@sentry/browser (incl. Tracing) 48.5 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.52 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.43 kB - -
@sentry/browser (incl. Tracing, Replay) 87.89 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.35 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 92.59 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 105.3 kB - -
@sentry/browser (incl. Feedback) 45.81 kB - -
@sentry/browser (incl. sendFeedback) 33.36 kB - -
@sentry/browser (incl. FeedbackAsync) 38.47 kB - -
@sentry/browser (incl. Metrics) 29.52 kB - -
@sentry/browser (incl. Logs) 29.8 kB - -
@sentry/browser (incl. Metrics & Logs) 30.45 kB - -
@sentry/react 30.33 kB - -
@sentry/react (incl. Tracing) 50.71 kB - -
@sentry/vue 35.64 kB - -
@sentry/vue (incl. Tracing) 50.73 kB - -
@sentry/svelte 28.6 kB - -
CDN Bundle 30.32 kB - -
CDN Bundle (incl. Tracing) 49.02 kB - -
CDN Bundle (incl. Logs, Metrics) 32.54 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 50.89 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 72.91 kB - -
CDN Bundle (incl. Tracing, Replay) 86.47 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 92.22 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.17 kB - -
CDN Bundle - uncompressed 89.94 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.64 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.23 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.33 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.18 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.92 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 271.6 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.62 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.28 kB - -
@sentry/nextjs (client) 53.23 kB - -
@sentry/sveltekit (client) 48.92 kB - -
@sentry/core/server 65.11 kB - -
@sentry/core/browser 52.27 kB - -
@sentry/node 121.6 kB +0.04% +42 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 85.18 kB - -
@sentry/node - without tracing 87.49 kB +0.07% +54 B 🔺
@sentry/aws-serverless 95.87 kB +0.05% +42 B 🔺
@sentry/cloudflare (withSentry) - minified 196.9 kB - -
@sentry/cloudflare (withSentry) 487.47 kB - -

View base workflow run

Comment thread packages/node/src/sdk/index.ts
@mydea
mydea force-pushed the fn/channel-injection-build-flag branch from 1d27eb4 to e670b7c Compare August 24, 2026 07:51
@mydea
mydea force-pushed the fn/channel-injection-build-flag branch from e670b7c to 40ae0c4 Compare August 24, 2026 09:04
@mydea
mydea marked this pull request as ready for review August 24, 2026 09:24
@mydea
mydea requested a review from a team as a code owner August 24, 2026 09:24
@mydea
mydea requested review from msonnb and stephanie-anderson and removed request for a team August 24, 2026 09:24
@mydea
mydea force-pushed the fn/channel-injection-build-flag branch from 40ae0c4 to 8ff1633 Compare August 24, 2026 11:06

@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 1 potential issue.

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 8ff1633. Configure here.

Comment thread packages/bundler-plugins/src/core/types.ts
@mydea
mydea force-pushed the fn/channel-injection-build-flag branch from 8ff1633 to 5a399f9 Compare August 24, 2026 11:24
@mydea
mydea requested review from Lms24 and removed request for stephanie-anderson August 24, 2026 12:52
@mydea
mydea requested a review from a team as a code owner August 25, 2026 08:12
@mydea
mydea requested review from JPeer264 and isaacs and removed request for a team August 25, 2026 08:12
Base automatically changed from fn/default-framework-integrations to develop August 25, 2026 12:00
Introduce a `__SENTRY_CHANNEL_INJECTION__` treeshaking flag (mirroring `__SENTRY_TRACING__`)
that removes the runtime diagnostics-channel injection when text-replaced with `false`, and
expose it through the bundler plugins' `bundleSizeOptimizations.excludeChannelInjection`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mydea
mydea force-pushed the fn/channel-injection-build-flag branch from 5a399f9 to dac4c3a Compare August 25, 2026 13:31

@isaacs isaacs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two small fixes that I think are worth doing before landing, but this looks good otherwise.

*
* @default false
*/
excludeChannelInjection?: boolean;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this also be added to the BundleSizeOptimizationsOptions in packages/core/src/build-time-plugins/buildTimeOptionsBase.ts, so that it can be picked up on the bundleSizeOptimizations field of BuildTimeOptionsBase? Eg, that's what packages/nuxt/src/common/types.ts pulls its options from.

Comment on lines +166 to 169
const useChannelInjection =
(typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) &&
options.enableRuntimeChannelInjection !== false;
if (useChannelInjection) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Making this a const seems to thwart tree shaking on webpack 5 and esbuild, maybe because the options.enableRuntimeChannelInjection is a property access, so could theoretically be a getter?

But doing it this way drops the webpack and esbuild builds by about 20kb. (Rollup seems to be smart enough to know the const is unused outside of these two lines.)

Suggested change
const useChannelInjection =
(typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) &&
options.enableRuntimeChannelInjection !== false;
if (useChannelInjection) {
if (
(typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) &&
options.enableRuntimeChannelInjection !== false
) {
registerDiagnosticsChannelInjection();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Found by adding a size-limt entry for this, could be a good addition to this PR, or a follow-up:

diff --git a/.size-limit.js b/.size-limit.js
index 288258df7c..66e82cbdc5 100644
--- a/.size-limit.js
+++ b/.size-limit.js
@@ -447,6 +447,30 @@ module.exports = [
       return config;
     },
   },
+  {
+    // verify the ~20kb the `bundleSizeOptimizations.excludeChannelInjection`
+    // option removes from the default @sentry/node build.
+    name: '@sentry/node - without channel injection',
+    path: 'packages/node/build/esm/index.js',
+    import: createImport('init'),
+    gzip: true,
+    limit: '104 KB',
+    disablePlugins: ['@size-limit/esbuild'],
+    ignore: [...builtinModules, ...nodePrefixedBuiltinModules],
+    modifyWebpackConfig: function (config) {
+      const webpack = require('webpack');
+
+      config.plugins.push(
+        new webpack.DefinePlugin({
+          __SENTRY_CHANNEL_INJECTION__: false,
+        }),
+      );
+
+      config.optimization.minimize = true;
+
+      return config;
+    },
+  },
   // AWS SDK (ESM)
   {
     name: '@sentry/aws-serverless',

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