sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Serverless errors outside of handler function are never reported

Open enapupe opened this issue 2 years ago • 14 comments

Is there an existing issue for this?

  • [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [X] I have reviewed the documentation https://docs.sentry.io/
  • [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Self-hosted/on-premise

Which package are you using?

@sentry/serverless

SDK Version

7.1.1

Steps to Reproduce

  1. Setup Sentry/serverless following the docs
  2. Ship a lambda function that throws AFTER sentry init but BEFORE wrapHandler fn runs
  3. error never gets to Sentry

Expected Result

I'd expect

Sentry.AWSLambda.init({
    dsn: SENTRY_DSN,
    release: VERSION,
  })

to take care of any exception that would happen after it

Actual Result

no errors reported

enapupe avatar Jun 08 '22 12:06 enapupe

Hi @enapupe , thanks for writing in. We could not reproduce this. Would you be able to share relevant parts of your code or even a reproduction setup so we can investigate this further? Thanks!

lforst avatar Jun 09 '22 13:06 lforst

Hey, I'm running it with AWS Lambda, the code can be reduced to just:

Runtime: Node.js 16.x

"use strict";
const Sentry = require("@sentry/serverless");
Sentry.AWSLambda.init({
    dsn: "<DSN>",
});


throw new Error('error')

const some = async () => {
    return 'OK';
};
exports.handler = some;

Running the above code locally, with node v16.10.0 (not serverless), it reports the error just fine.

enapupe avatar Jun 09 '22 14:06 enapupe

Hey @enapupe ! One question: You followed the manual guide [1] and you did NOT install Sentry's Lambda Layer [2], right?

1: https://docs.sentry.io/platforms/node/guides/aws-lambda/ 2: https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/

antonpirker avatar Jun 09 '22 15:06 antonpirker

1: I suppose the reduced case above should answer that, or else I'm not 100% sure what you are asking. 2: Correct! I have my own application layer that includes sentry.

enapupe avatar Jun 09 '22 15:06 enapupe

One thing, code that is outside the main handler function is run in Lambdas INIT phase. According to this blog post [1] the Node version used by AWS Lambda does not support the top-level await feature and maybe this prevents Sentry from sending Errors outside of the main handler. @lforst can you check how the data is sent to Sentry? If it is done asynchronously, then we know that it does not work outside the handler in AWS

1: https://dev.to/aws-builders/getting-the-most-of-aws-lambda-free-compute-wrapper-scripts-3h4b#the-nodejs-problem

antonpirker avatar Jun 09 '22 15:06 antonpirker

From reading that blog I don't think you can't have asynchronous code outside of the main handler. Node is non-blocking by nature so that would be crazy. One thing I can imagine happening, is that when the lambda errors-out, it just freezes causing an event not to be sent to Sentry. Within the wrapped handler we specifically wait for all events to be sent before allowing the lambda to close because if return it just freezes. This is something we would have to investigate.

lforst avatar Jun 09 '22 15:06 lforst

This could be relevant to the subject https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/

enapupe avatar Jun 09 '22 17:06 enapupe

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Jul 02 '22 00:07 github-actions[bot]

Any news regarding this? Not sure why it was closed.....

enapupe avatar Sep 26 '22 12:09 enapupe

Did you wrap your handler in Sentry.AWSLambda.wrapHandler? That wrapper blocks the termination of the lambda until the SDK has flushed all built up events.

lforst avatar Sep 26 '22 12:09 lforst

I'm definitely wrapping my handler and that is working fine, that's not the question though.

The point is "errors during setup/init", say a db connection or anything else that happens (synchronously) before my handler is invoked.

enapupe avatar Sep 26 '22 12:09 enapupe

Ah right sorry - lost context of this issue. I need some time to think about this. Essentially we need to stop the lambda from terminating when something bubbles up globally so we can flush out events. Let me know if you already have suggestions on how to tackle this.

lforst avatar Sep 26 '22 13:09 lforst

Np. Aside from the links shared above (https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/), I don't have anything extra to share. I believe this should be doable and would greatly increase Sentry's scope if accomplished.

enapupe avatar Sep 26 '22 13:09 enapupe

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

getsantry[bot] avatar May 02 '24 07:05 getsantry[bot]