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

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
SidPrak7 opened this issue Jun 2, 2024 · 12 comments
Closed
3 tasks done

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

SidPrak7 opened this issue Jun 2, 2024 · 12 comments
Assignees
Labels
Package: aws-serverless Issues related to the Sentry AWS Serverless SDK Type: Bug

Comments

@SidPrak7
Copy link

SidPrak7 commented Jun 2, 2024

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\"
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 2, 2024
@github-actions github-actions bot added the Package: browser Issues related to the Sentry Browser SDK label Jun 2, 2024
@Lms24 Lms24 self-assigned this Jun 3, 2024
@Lms24
Copy link
Member

Lms24 commented Jun 3, 2024

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
Copy link

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

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 3, 2024
@Lms24
Copy link
Member

Lms24 commented Jun 3, 2024

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:

  •  building bundled Serverless SDK in ESM
  • building awslambda-auto.js in ESM
  • our check for ESM or CJS mode is broken in the bundled SDK code, causing the SDK, as well as some Otel parts to incorrectly assume CJS mode instead of ESM. This is because our rollup config (some plugin probably), for some reason replaces global require references with a minified version of require that just throws an error 🤦 We can fix this. Found an ugly way so far but there's probably a nicer solution.
  • Otel Instrumentation does not seem to get applied despite us coming in early enough with the --import arg set in NODE_OPTIONS. I'm not sure yet what the cause is. So far, my two suspicions are:
    • Our own imports of e.g. http that are evaluated before the init call is made
    • Import-in-the-middle is failing silently, resulting in http et al. not being patched

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
Copy link

tgdn commented Jun 6, 2024

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.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 6, 2024
@Lms24
Copy link
Member

Lms24 commented Jun 6, 2024

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
Copy link

tgdn commented Jun 6, 2024

@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()

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 6, 2024
@Lms24 Lms24 added Package: serverless Issues related to the Sentry Serverless SDK and removed Package: browser Issues related to the Sentry Browser SDK labels Jun 6, 2024
@AbhiPrasad AbhiPrasad removed the Package: serverless Issues related to the Sentry Serverless SDK label Aug 2, 2024
@AbhiPrasad AbhiPrasad added the Package: aws-serverless Issues related to the Sentry AWS Serverless SDK label Aug 2, 2024
@zolbayars
Copy link

Having the same issue on v8.23.0.


INIT_START Runtime Version: nodejs:20.v25 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:66a145a0c659d27a0f5c781ef79f03464c01811b9c2f47d1287ba3f315da8983
--
  | node:internal/modules/esm/resolve:858
  | throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
  | ^
  | Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sentry/aws-serverless' imported from /var/task/
  | at packageResolve (node:internal/modules/esm/resolve:858:9)
  | at moduleResolve (node:internal/modules/esm/resolve:931:18)
  | at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)
  | at defaultResolve (node:internal/modules/esm/resolve:1216:79)
  | at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
  | at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
  | at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)
  | at ModuleLoader.import (node:internal/modules/esm/loader:315:34)
  | at asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:118:36)
  | at runEntryPointWithESMLoader (node:internal/modules/run_main:147:19) {
  | code: 'ERR_MODULE_NOT_FOUND'
  | }

@Lms24
Copy link
Member

Lms24 commented Aug 6, 2024

Hey @zolbayars thanks for writing in!

A lot has changed since this issue was originally reported, including that we finally updated our docs for AWS Lambda.

I might be wrong but from the logs, it looks like you're using the Lambda layer in an ESM lambda function. Is this correct? This unfortunately isn't possible at the moment due to some technical limitations we discovered while working on #12409. I recommend going through the docs and choosing the installation method that suits you best.

If you're already doing that, can you please open a new issue with more details (ESM or CJS, layer or NPM package, how to reproduce)? Thanks!

I'm gonna close this issue for the time being to avoid further mixups.

@zolbayars
Copy link

Thanks for the reply @Lms24,

Somehow I'm not able to create an issue (It says Unable to create issue. whenever I try 🫠). So I'm detailing my issue here.

I'm trying to use Sentry on an AWS Lambda written in Typescript. Here's my tsconfig.json:

{
  "compilerOptions": {
      "esModuleInterop": true,
      "useUnknownInCatchVariables": false,
      ...
  },
  "include": [
     ...
  ]
}

According to this doc:

If you're using TypeScript, your lambda function is likely transpiled to CommonJS before running it. In this case, you should follow the CommonJS instructions. Note that TypeScript can also be configured to output ESM, in which case you should follow the ESM instructions.

So I have followed the CommonJS instructions. It works fine when I only add the @sentry/aws-serverless and have initialization like this:

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 1.0,
});

But when I add profiling, it starts to throw an error:

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  integrations: [nodeProfilingIntegration()],
  tracesSampleRate: 1.0,
  profilesSampleRate: 1.0,
});

The error:

Uncaught Exception 	
{
    "errorType": "TypeError",
    "errorMessage": "The \"path\" argument must be of type string or an instance of URL. Received undefined",
    "code": "ERR_INVALID_ARG_TYPE",
    "stack": [
        "TypeError [ERR_INVALID_ARG_TYPE]: The \"path\" argument must be of type string or an instance of URL. Received undefined",
        "    at Object.fileURLToPath (node:internal/url:1461:11)",
        "    at Object.<anonymous> (/var/task/functions/analytics/refresh/refreshCustomerBreakdowns.js:80979:43)",
        "    at Module._compile (node:internal/modules/cjs/loader:1358:14)",
        "    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)",
        "    at Module.load (node:internal/modules/cjs/loader:1208:32)",
        "    at Module._load (node:internal/modules/cjs/loader:1024:12)",
        "    at Module.require (node:internal/modules/cjs/loader:1233:19)",
        "    at require (node:internal/modules/helpers:179:18)",
        "    at _tryRequireFile (file:///var/runtime/index.mjs:1002:37)",
        "    at _tryRequire (file:///var/runtime/index.mjs:1052:25)"
    ]
}

@Lms24
Copy link
Member

Lms24 commented Aug 7, 2024

@zolbayars before we extract this to a new issue, there was a very similar issue recently for profiling node on AWS lambda which was caused by @sentry/profiling-node being bundled in some way. You might wanna take a look at #12994 (comment) and see if this applies to your situation.

@zolbayars
Copy link

Hey @Lms24, yep, I took a look at the issue. But I'm not sure how I could do these 2 steps on serverless.yml (instead of on template.yaml):

  1. so you'd have to create a lambda layer yourself that has @sentry/profiling-node and all its dependencies inside node_modules

  2. You'll also have to mark @sentry/profiling-node as external in your SAM template.yaml like so:

@Lms24
Copy link
Member

Lms24 commented Aug 7, 2024

  1. As the documentation states, you somehow have to upload your node_modules, including @sentry/profiling-node to AWS lambda. This really depends on how you work with lambdas. It can be as simple as deploying your entire working directory. Creating a layer with node_modules is one option but it's not strictly necessary. Sorry for the confusion

  2. This means that you need to ensure that code from @sentry/profiling-node is not bundled if you potentially bundle your lambda functions. If you only use plain tsc this shouldn't be an issue but in the linked issue, the user was also bundling the transpiled code into one file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: aws-serverless Issues related to the Sentry AWS Serverless SDK Type: Bug
Projects
Archived in project
Development

No branches or pull requests

6 participants