Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-serverless): Fix tree-shaking for aws-serverless package #12017

Merged
merged 5 commits into from
May 14, 2024
Merged
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
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
Loading