Skip to content
Open
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
19 changes: 19 additions & 0 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,25 @@ module.exports = [
return config;
},
},
{
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',
Expand Down
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;
Comment thread
cursor[bot] marked this conversation as resolved.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ah, you are right. this is a bit weird and maybe we can combine these together at some point...


/**
* 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: 15 additions & 0 deletions packages/core/src/build-time-plugins/buildTimeOptionsBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,19 @@ interface BundleSizeOptimizationsOptions {
* @default false
*/
excludeReplayWorker?: 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;
}
19 changes: 14 additions & 5 deletions packages/node/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import { defaultStackParser, getSentryRelease } from './api';
import { NodeClient } from './client';
import { initOpenTelemetry } from './initOtel';

// 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 @@ -155,11 +159,16 @@ 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;
if (useChannelInjection) {
// 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.
if (
(typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) &&
options.enableRuntimeChannelInjection !== false
) {
registerDiagnosticsChannelInjection();
}
registerDiagnosticsChannelInjection();
}
Comment on lines 172 to 173

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: There is a duplicate, unconditional call to registerDiagnosticsChannelInjection() outside the intended if block, which bypasses the opt-out logic.
Severity: CRITICAL

Suggested Fix

Remove the unconditional call to registerDiagnosticsChannelInjection() on line 172 and the extraneous closing brace on line 173. The function should only be called from within the if block on line 170.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/node/src/sdk/index.ts#L172-L173

Potential issue: A refactoring error resulted in a duplicate, unconditional call to
`registerDiagnosticsChannelInjection()` at line 172, placed outside the conditional
guard. This means the function is always called at least once, regardless of the
`options.enableRuntimeChannelInjection` or `__SENTRY_CHANNEL_INJECTION__` flags.
Consequently, the opt-out mechanisms for this feature are non-functional. In the default
case where the feature is enabled, the function is called twice instead of once. This
breaks the intended behavior and would cause existing tests to fail.

Did we get this right? 👍 / 👎 to inform future reviews.


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 but still runs detection 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).toHaveBeenCalledTimes(1);
} finally {
vi.unstubAllGlobals();
}
});
Comment thread
cursor[bot] marked this conversation as resolved.
});
Loading