Skip to content
Draft
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
3 changes: 3 additions & 0 deletions packages/bundler-plugins/src/core/build-plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ export function createSentryBuildPluginManager(
if (bundleSizeOptimizations.excludeTracing) {
bundleSizeOptimizationReplacementValues['__SENTRY_TRACING__'] = false;
}
if (bundleSizeOptimizations.excludeChannelInjection) {
bundleSizeOptimizationReplacementValues['__SENTRY_CHANNEL_INJECTION__'] = false;
}
if (bundleSizeOptimizations.excludeReplayCanvas) {
bundleSizeOptimizationReplacementValues['__RRWEB_EXCLUDE_CANVAS__'] = true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-plugins/src/core/options-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type NormalizedOptions = {
| {
excludeDebugStatements?: boolean;
excludeTracing?: boolean;
excludeChannelInjection?: boolean;
excludeReplayCanvas?: boolean;
excludeReplayShadowDom?: boolean;
excludeReplayIframe?: boolean;
Expand Down
16 changes: 16 additions & 0 deletions packages/bundler-plugins/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ export interface Options {
*/
excludeTracing?: boolean;

/**
* Exclude the Node SDK's runtime diagnostics-channel injection from the bundle.
*
* If set to `true`, the plugin will attempt to tree-shake (remove) code that installs the Node SDK's
* runtime module hooks (e.g. for Express instrumentation) at load time. Note that the success of this
* depends on tree-shaking being enabled in your build tooling.
*
* Only enable this when the diagnostics channels are injected at build time (via the bundler plugin) or
* when you otherwise do not rely on the runtime channel injection. This is equivalent to setting
* `enableRuntimeChannelInjection: false` in the SDK's `init` options.
*
* @default false
*/
excludeChannelInjection?: boolean;

/**
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay Canvas recording functionality.
* Note that the success of this depends on tree-shaking being enabled in your build tooling.
Expand Down Expand Up @@ -526,6 +541,7 @@ export type IncludeEntry = {
export interface SentrySDKBuildFlags extends Record<string, boolean | undefined> {
__SENTRY_DEBUG__?: boolean;
__SENTRY_TRACING__?: boolean;
__SENTRY_CHANNEL_INJECTION__?: boolean;
__RRWEB_EXCLUDE_CANVAS__?: boolean;
__RRWEB_EXCLUDE_IFRAME__?: boolean;
__RRWEB_EXCLUDE_SHADOW_DOM__?: boolean;
Expand Down
15 changes: 11 additions & 4 deletions packages/node/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import { NodeClient } from './client';
import { initOpenTelemetry } from './initOtel';
import { fastifyIntegration } from '../integrations/tracing/fastify';

// Treeshakable guard to remove all code related to runtime diagnostics-channel injection. Set to
// `false` at build time by the Sentry bundler plugins' `bundleSizeOptimizations.excludeChannelInjection`.
declare const __SENTRY_CHANNEL_INJECTION__: boolean | undefined;

/**
* Get the base default integrations shared by all Node SDK default-integration sets.
*/
Expand Down Expand Up @@ -166,10 +170,13 @@ function _init(
};

// Install the channel-based (orchestrion diagnostics-channel) instrumentation hooks by default,
// independent of tracing — the channel integrations also capture errors, not just spans. Opt out
// with `enableRuntimeChannelInjection: false`. Install as early as possible, before the app imports
// its instrumented modules.
const useChannelInjection = options.enableRuntimeChannelInjection !== false;
// independent of tracing — the channel integrations also capture errors, not just spans. Opt out at
// runtime with `enableRuntimeChannelInjection: false`, or at build time via the bundler plugins'
// `bundleSizeOptimizations.excludeChannelInjection` (which tree-shakes this whole block away).
// Install as early as possible, before the app imports its instrumented modules.
const useChannelInjection =
(typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) &&
options.enableRuntimeChannelInjection !== false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing integration test for feat

Medium Severity

Flagging per the testing conventions in the PR review guidelines: this feat only adds a unit test that stubs __SENTRY_CHANNEL_INJECTION__. There is no integration or E2E coverage for bundleSizeOptimizations.excludeChannelInjection wiring the define through the bundler plugins. The existing bundle-size-optimizations fixtures under dev-packages/bundler-plugin-integration-tests already exercise the other flags and were not extended for this one.

Additional Locations (1)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 1d27eb4. Configure here.

if (useChannelInjection) {
registerDiagnosticsChannelInjection();
}
Expand Down
14 changes: 14 additions & 0 deletions packages/node/test/sdk/diagnosticsChannelInjection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ describe('diagnostics-channel injection', () => {
expect(registerDiagnosticsChannelInjection).toHaveBeenCalledTimes(1);
expect(detectOrchestrionSetup).toHaveBeenCalledTimes(1);
});

it('does not register the injection hooks when the `__SENTRY_CHANNEL_INJECTION__` build flag is false', () => {
// Simulates the bundler plugins' `bundleSizeOptimizations.excludeChannelInjection` text-replacing the flag.
vi.stubGlobal('__SENTRY_CHANNEL_INJECTION__', false);

try {
init({ dsn: PUBLIC_DSN, tracesSampleRate: 1, enableOpenTelemetrySetup: false });

expect(registerDiagnosticsChannelInjection).not.toHaveBeenCalled();
expect(detectOrchestrionSetup).not.toHaveBeenCalled();
} finally {
vi.unstubAllGlobals();
}
});

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 PR missing integration tests

Medium Severity

This feat PR only adds a unit test for the __SENTRY_CHANNEL_INJECTION__ gate. Per the testing conventions, feat PRs need at least one integration or E2E test. The new bundleSizeOptimizations.excludeChannelInjection path also is not covered by the existing bundler-plugin bundle-size-optimizations fixtures that already assert the other exclude flags.

Additional Locations (2)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 68993db. Configure here.

});