sentry-javascript
sentry-javascript copied to clipboard
Serverless errors outside of handler function are never reported
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
- Setup Sentry/serverless following the docs
- Ship a lambda function that throws AFTER sentry init but BEFORE wrapHandler fn runs
- 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
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!
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.
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/
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.
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
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.
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/
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 🥀
Any news regarding this? Not sure why it was closed.....
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.
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.
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.
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.
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 🥀