Skip to content

Commit

Permalink
feat(aws-serverless): Fix tree-shaking for aws-serverless package (#1…
Browse files Browse the repository at this point in the history
…2017)

The prior fix was incomplete, because we were still using
`getDefaultIntegrations()` inside of node's `init`, so the deps where
pulled in anyhow.

Furthermore, it seems that `preserveModules: false` for `@sentry/node`
also prevented this from working as expected.

So this PR does two things:

1. Set `preserveModules: true` so that tree-shaking can work as expected
(😿 )
2. Expose a new `initWithoutDefaultIntegrations` method from
`@sentry/node` which AWS uses, which avoids including any integrations
by default. You have to pass your own `defaultIntegrations` to it.

This is not the prettiest solution, but I couldn't think of anything
much better 😬 I also added a size-limit entry to keep track of this.
  • Loading branch information
mydea committed May 14, 2024
1 parent c0753f0 commit 43d4d33
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 17 deletions.
27 changes: 27 additions & 0 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,33 @@ module.exports = [
gzip: true,
limit: '180 KB',
},
// AWS SDK (ESM)
{
name: '@sentry/aws-serverless',
path: 'packages/aws-serverless/build/npm/esm/index.js',
import: createImport('init'),
ignore: [
'node:http',
'node:https',
'node:diagnostics_channel',
'async_hooks',
'child_process',
'perf_hooks',
'fs',
'os',
'path',
'inspector',
'worker_threads',
'http',
'stream',
'zlib',
'net',
'tls',
'module',
],
gzip: true,
limit: '140 KB',
},
];

function createImport(...args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const NODE_EXPORTS_IGNORE = [
// Only required from the Node package
'setNodeAsyncContextStrategy',
'getDefaultIntegrationsWithoutPerformance',
'initWithoutDefaultIntegrations',
];

const nodeExports = Object.keys(SentryNode).filter(e => !NODE_EXPORTS_IGNORE.includes(e));
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-serverless/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
flush,
getCurrentScope,
getDefaultIntegrationsWithoutPerformance,
init as initNode,
initWithoutDefaultIntegrations,
startSpanManual,
withScope,
} from '@sentry/node';
Expand Down Expand Up @@ -93,7 +93,7 @@ export function init(options: NodeOptions = {}): void {
version: SDK_VERSION,
};

initNode(opts);
initWithoutDefaultIntegrations(opts);
}

/** */
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-serverless/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jest.mock('@sentry/node', () => {
const original = jest.requireActual('@sentry/node');
return {
...original,
init: (options: unknown) => {
initWithoutDefaultIntegrations: (options: unknown) => {
mockInit(options);
},
startInactiveSpan: (...args: unknown[]) => {
Expand Down
6 changes: 1 addition & 5 deletions packages/node/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ export default [
output: {
// set exports to 'named' or 'auto' so that rollup doesn't warn
exports: 'named',
// set preserveModules to false because we want to bundle everything into one file.
preserveModules:
process.env.SENTRY_BUILD_PRESERVE_MODULES === undefined
? false
: Boolean(process.env.SENTRY_BUILD_PRESERVE_MODULES),
preserveModules: true,
},
plugins: [
replace({
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {
init,
getDefaultIntegrations,
getDefaultIntegrationsWithoutPerformance,
initWithoutDefaultIntegrations,
} from './sdk/init';
export { initOpenTelemetry } from './sdk/initOtel';
export { getAutoPerformanceIntegrations } from './integrations/tracing';
Expand Down
39 changes: 30 additions & 9 deletions packages/node/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,24 @@ declare const __IMPORT_META_URL_REPLACEMENT__: string;
* Initialize Sentry for Node.
*/
export function init(options: NodeOptions | undefined = {}): void {
const clientOptions = getClientOptions(options);
return _init(options, getDefaultIntegrations);
}

/**
* Initialize Sentry for Node, without any integrations added by default.
*/
export function initWithoutDefaultIntegrations(options: NodeOptions | undefined = {}): void {
return _init(options, () => []);
}

/**
* Initialize Sentry for Node, without performance instrumentation.
*/
function _init(
options: NodeOptions | undefined = {},
getDefaultIntegrationsImpl: (options: Options) => Integration[],
): void {
const clientOptions = getClientOptions(options, getDefaultIntegrationsImpl);

if (clientOptions.debug === true) {
if (DEBUG_BUILD) {
Expand Down Expand Up @@ -191,7 +208,10 @@ function validateOpenTelemetrySetup(): void {
}
}

function getClientOptions(options: NodeOptions): NodeClientOptions {
function getClientOptions(
options: NodeOptions,
getDefaultIntegrationsImpl: (options: Options) => Integration[],
): NodeClientOptions {
const release = getRelease(options.release);

const autoSessionTracking =
Expand All @@ -215,17 +235,18 @@ function getClientOptions(options: NodeOptions): NodeClientOptions {
tracesSampleRate,
});

const mergedOptions = {
...baseOptions,
...options,
...overwriteOptions,
};

if (options.defaultIntegrations === undefined) {
options.defaultIntegrations = getDefaultIntegrations({
...options,
...overwriteOptions,
});
options.defaultIntegrations = getDefaultIntegrationsImpl(mergedOptions);
}

const clientOptions: NodeClientOptions = {
...baseOptions,
...options,
...overwriteOptions,
...mergedOptions,
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
integrations: getIntegrationsToSetup({
defaultIntegrations: options.defaultIntegrations,
Expand Down

0 comments on commit 43d4d33

Please sign in to comment.