getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.75k stars 1.51k forks source link

Sentry import failing on AWS λ for v7 & v8.7.0 #12322

Open SidPrak7 opened 3 weeks ago

SidPrak7 commented 3 weeks ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/aws-serverless

SDK Version

8.7.0

Framework Version

@sentry/aws-serverless

Link to Sentry event

No response

SDK Setup

import * as Sentry from '@sentry/aws-serverless';

Sentry.init({
  dsn: "SENTRY_DSN_HERE",

  // Add Performance Monitoring by setting tracesSampleRate and adding integration
  // Set tracesSampleRate to 1.0 to capture 100% of transactions
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
});

Steps to Reproduce

We tried the full combination set of these steps.

  1. Set up the λ layers We tried these two ARNs
    • arn:aws:lambda:us-west-2:943013980633:layer:SentryNodeServerlessSDK:247
    • arn:aws:lambda:us-west-2:943013980633:layer:SentryNodeServerlessSDKv7:1

We also manually built a layer and pushed it to the λ

  1. Set the env variables We tried these env vars SENTRY_DSN='SENTRY_DSN' SENTRY_TRACES_SAMPLE_RATE=1

These four variations of NODE_OPTIONS: NODE_OPTIONS='-r @sentry/serverless/dist/awslambda-auto' NODE_OPTIONS='-r @sentry/serverless/awslambda-auto' NODE_OPTIONS='-r @sentry/aws-serverless/build/npm/cjs/awslambda-auto' not setting NODE_OPTIONS

It might be relevant that our file is .mjs file.

We are only noting the relevant errors in the Actual Result section.

Expected Result

Sentry logs something.

Actual Result

For the v7 arn & node options with dist in it:

Cannot find package '@sentry/aws-serverless' imported from /var/task/index.mjs

For the v7 arn & node options without dist in it:

Error: Cannot find module '@sentry/serverless/awslambda-auto'

For the arn we got from the docs & node options with dist in it:

Error: Cannot find module '@sentry/serverless/dist/awslambda-auto'

For the arn we got from the docs & node options without dist in it:

Error: Cannot find module '@sentry/serverless/awslambda-auto'

For the arn we got from the docs & node options with cjs in it:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './build/npm/cjs/awslambda-auto' is not defined by "exports" in /opt/nodejs/node_modules/@sentry/aws-serverless/package.json

Also, since the official docs for loading the layer didn't specify setting NODE_OPTIONS, we also tried that:

Cannot find package '@sentry/aws-serverless' imported from /var/task/index.mjs\nDid you mean to import \"@sentry/aws-serverless/build/npm/cjs/index.js\"
Lms24 commented 3 weeks ago

Hi @SidPrak7 thanks for writing in!

Our layer auto init script is currently only exported for CJS which is why you're running into these errors with your function which is running in ESM. I'm gonna take a look at adding an ESM entry point for this. Can't promise that this will work easily because ESM is a mess but I'll leave an update or PR later.

sajaghori commented 3 weeks ago

try: import Sentry from '@sentry/aws-serverless';

Lms24 commented 3 weeks ago

So, I looked into emitting an ESM version of the lambda layer a bit today and as expected it wasn't as straight forward as I hoped 🥲

Some challenges we overcame or still need to figure out:

I'll work on this a bit more tomorrow. For now, to unblock you, I can't give you a better workaround yet than to run your lambdas in CJS ("require") mode. If you're building/transpiling your functions (e.g. typescript) this shouldn't be too hard; otherwise rewriting everything to require instead of import is obviously not ideal. I'll keep you posted on updates.

EDIT: Actually, one thing I haven't tried so far is to directly import and use the @sentry/aws-serverless package in your functions, as shown here, without using our layer at all. This should work and I know it does fine in CJS. However, I haven't tested this yet in ESM.

tgdn commented 2 weeks ago

Hi, I have the same issue, tried multiple options as described. Using NODE_OPTIONS="-r @sentry/aws-serverless/awslambda-auto" seems to make AWS happy, but nothing is logged on Sentry's side and importing the actual layer within ESM code does not work.

Lms24 commented 2 weeks ago

nothing is logged on Sentry's side

@tgdn I think this is a different problem than the one described in this issue (which is about running the lambda layer in ESM mode). if you run your lambda function as CJS (i.e. require syntax) and you're experiencing problems, would you mind opening a new issue? Thanks :)

tgdn commented 2 weeks ago

@Lms24 I didn't mention it but I'm also running the lambda layer in ESM mode :)

The only alternative seems to be importing Sentry and using Sentry.wrapHandler()