Skip to content

fix(aws-serverless): Only auto-patch handler in CJS when loading awslambda-auto #12392

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2024

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Jun 6, 2024

The awslambda-auto.js file can be used to automatically init the AWS Serverless SDK by only setting environment variables. This file is currently only exposed for CJS output. In a future PR, I'd like to also expose it in ESM so that users don't have to create their own instrumentation.ts file for lambda functions.

To make this file compatible with ESM though, we can only conditionally try to auto patch the lambda function handler as our auto patching method relies on requireing users' lambda function handlers. So this PR guards the tryPatcHandler call by checking for the availability of require (which is undefined in ESM).

fix(aws-serverless): Only auto-patch handler in CJS when loading `awslambda-auto`
Copy link
Contributor

github-actions bot commented Jun 6, 2024

size-limit report 📦

Path Size
@sentry/browser 22 KB (0%)
@sentry/browser (incl. Tracing) 33.05 KB (0%)
@sentry/browser (incl. Tracing, Replay) 68.65 KB (0%)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.94 KB (0%)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.7 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback) 84.82 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 86.66 KB (0%)
@sentry/browser (incl. metrics) 26.18 KB (0%)
@sentry/browser (incl. Feedback) 38.16 KB (0%)
@sentry/browser (incl. sendFeedback) 26.59 KB (0%)
@sentry/browser (incl. FeedbackAsync) 31.14 KB (0%)
@sentry/react 24.77 KB (0%)
@sentry/react (incl. Tracing) 36.08 KB (0%)
@sentry/vue 26 KB (0%)
@sentry/vue (incl. Tracing) 34.87 KB (0%)
@sentry/svelte 22.13 KB (0%)
CDN Bundle 23.35 KB (0%)
CDN Bundle (incl. Tracing) 34.73 KB (0%)
CDN Bundle (incl. Tracing, Replay) 68.67 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) 73.83 KB (0%)
CDN Bundle - uncompressed 68.6 KB (0%)
CDN Bundle (incl. Tracing) - uncompressed 102.8 KB (0%)
CDN Bundle (incl. Tracing, Replay) - uncompressed 212.73 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 225.2 KB (0%)
@sentry/nextjs (client) 35.45 KB (0%)
@sentry/sveltekit (client) 33.68 KB (0%)
@sentry/node 129.96 KB (+12.2% 🔺)
@sentry/node - without tracing 92.7 KB (-2.33% 🔽)
@sentry/aws-serverless 117.75 KB (+13.1% 🔺)

@@ -21,7 +23,9 @@ if (lambdaTaskRoot) {
),
});

Sentry.tryPatchHandler(lambdaTaskRoot, handlerString);
if (typeof require !== 'undefined') {
Copy link
Member Author

Choose a reason for hiding this comment

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

Fwiw, this is the same check we use in our isCjs utility within the SDK. I just can't import it here because this doesn't play well with the lambda layer being bundled into the index file imported on top.

@Lms24 Lms24 merged commit 1f82e47 into develop Jun 7, 2024
79 checks passed
@Lms24 Lms24 deleted the lms/fix-lambda-layer-tryPatchHandler branch June 7, 2024 08:34
@Lms24 Lms24 self-assigned this Jun 7, 2024
billyvg pushed a commit that referenced this pull request Jun 10, 2024
…lambda-auto` (#12392)

Guards the `tryPatcHandler` call by checking for the availability of
`require` (which is undefined in ESM). In ESM mode, this call fails because
`require` is not available. So let's not call it in this case.
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.

2 participants