You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[@sentry/node] AWS Lambda and other Serverless solutions support #1449
There's no official way yet, as we're still trying things out in beta, but it's doable with this code:
import{init,getDefaultHub}from'@sentry/node';init({dsn: 'https://my-dsn.com/1337'});exports.myHandler=asyncfunction(event,context){// your codeawaitgetDefaultHub().getClient().captureException(error,getDefaultHub().getScope());context.fail();}
changed the title [-][raven-node] Callback after capturing exception (or event, message)[/-][+][@sentry/node] Callback after capturing exception (or event, message)[/+]on Aug 7, 2018
@vietbui
In 4.0.0-rc.1 we introduced a function on the client called close, you call it like this:
import{getCurrentHub}from'@sentry/node';getCurrentHub().getClient().close(2000).then(result=>{if(!result){console.log('We reached the timeout for emptying the request buffer, still exiting now!');}global.process.exit(1);})
close will wait until all requests are sent, it will always resolve (result = false timeout was reached), up until the timeout is reached (in this example 2000ms).
This is our official API.
While the prev approach will still work, the close method works for all cases.
This solution does not work for me for some reason. It only works the first time in production when there is a cold start time and does not work after that. here is example code
After shutdown the client cannot be used any more so make sure to only do that right before you shut down the application.
So I don't know how we can handle this in lambda where we don't know when the application will be killed. Best would be to drain sentry per request like we could with the old API?
I have what I believe is a similar issue as @enapupe. If you call await client.flush(2000); in parallel only the first promise is resolved. This can happen in AWS lambda contexts where the client is reused among multiple concurrent calls to the handler.
I am using code like this:
letclient=Sentry.getCurrentHub().getClient();if(client){// flush the sentry client if it has any events to sendlog('begin flushing sentry client');try{awaitclient.flush(2000);}catch(err){console.error('sentry client flush error:',err);}log('end flushing sentry client');}
But when I make two calls to my lambda function in rapid succession, I get:
app begin flushing sentry client +2ms
app begin flushing sentry client +0ms
app end flushing sentry client +2ms
You can see that the second promise is never resolved.
@SarasArya@HazAT
First of all... Thanks for sharing your solution! :)
There is one callback of configureScope method that I guess it is supposed to be called before captureException but it is not being done in the same "thread".
Couldn't this lead to the appearance of race conditions?
@cibergarri I don't think so, looks synchronous to me, in case you have an async method in there, then there would be race conditions.
Consider is like .map of array's that the same thing happening here. In case you have issues wrapping your head around it. I hope that helps.
It does not work on lambda.
In my testing env it was working but in prod where there is a lot a concurrent executions and the containers are reuse it is logging just about 10% of the total amount.
Please tell how did your measure that it loses the exception events? Do you have a code sample of how such lost exception was thrown? Need more context.
What is happening?
If the function is invoked several times with short interval between invocations the event is only logged once.
If the time interval between invocations is larger all events are logged.
I assume that the problem is when the invocation is over a reused container.
I have tried also await Sentry.captureException(error);
and: await Sentry.flush();
and without flushing
same result
@armando25723 Looks like the server is responding with 429 (too many exceptions) while sending these events. We throw that in case of over quota/rate limiting scenarios. Do you know if you are sequentially sending errors or over quota? We can debug further if you think these are real error events getting dropped and you are under our 5k limit for the free tier.
@ajjindal all other projects are working fine with sentry. The organization slug is "alegra", project name is mail-dispatch-serverless under #mail-micros. We have been using sentry for a long time, but first time with serverless. This is not free tier, I can't tell you exactly which plan we are using but it is a paid one.
It will be nice if you could help me to debug further.
Thanks for reply : )
I'm having the same issue here, doing await Sentry.flush(1000); after captureException is not sending the errors to sentry. Filed a new issue here if anyone has any advice: #4155
Activity
[-][raven-node] Callback after capture exception (or event, message)[/-][+][raven-node] Callback after capturing exception (or event, message)[/+]kamilogorek commentedon Aug 2, 2018
This may help I guess https://blog.sentry.io/2018/06/20/how-droplr-uses-sentry-to-debug-serverless (it's using old raven version, which had a callback, but I'm mostly pointing to a
callbackWaitsForEmptyEventLoopflag.There's no official way yet, as we're still trying things out in beta, but it's doable with this code:
vietbui commentedon Aug 6, 2018
@kamilogorek Thank you for the pointer. I'll give it a try and play back the learnings.
vietbui commentedon Aug 7, 2018
@kamilogorek You suggestion works. I'm looking forward to a more official way.
[-][raven-node] Callback after capturing exception (or event, message)[/-][+][@sentry/node] Callback after capturing exception (or event, message)[/+]HazAT commentedon Sep 7, 2018
@vietbui
In
4.0.0-rc.1we introduced a function on the client calledclose, you call it like this:closewill wait until all requests are sent, it will always resolve (result = false timeout was reached), up until the timeout is reached (in this example 2000ms).This is our official API.
While the prev approach will still work, the
closemethod works for all cases.vietbui commentedon Sep 13, 2018
@HazAT Nice one. Thanks for all the hard work.
vietbui commentedon Sep 21, 2018
In 4.0.3 I call it like this in my lambda function:
getDefaultHub() is no longer available.
kamilogorek commentedon Sep 24, 2018
@vietbui it's called
getCurrentHubnow, as we had to unify our API with other languages SDKs.vietbui commentedon Sep 26, 2018
@kamilogorek Thanks for the clarification. There is a problem with
getCurrentHubapproach as somehow the scope I set up did not end up in Sentry.In the end I took a different approach as suggested by @HazAT to capture exception in my lambda functions:
And it works perfectly.
albinekb commentedon Oct 3, 2018
Is this the recommended way to wait/force sentry to send events?
kamilogorek commentedon Oct 3, 2018
@albinekb yes – https://docs.sentry.io/learn/draining/?platform=browser
Andriy-Kulak commentedon Nov 11, 2018
This solution does not work for me for some reason. It only works the first time in production when there is a cold start time and does not work after that. here is example code
albinekb commentedon Nov 12, 2018
@Andriy-Kulak Thats also stated in the docs:
After shutdown the client cannot be used any more so make sure to only do that right before you shut down the application.So I don't know how we can handle this in lambda where we don't know when the application will be killed. Best would be to drain sentry per request like we could with the old API?
57 remaining items
esetnik commentedon Jul 1, 2019
I have what I believe is a similar issue as @enapupe. If you call
await client.flush(2000);in parallel only the first promise is resolved. This can happen in AWS lambda contexts where the client is reused among multiple concurrent calls to the handler.I am using code like this:
But when I make two calls to my lambda function in rapid succession, I get:
You can see that the second promise is never resolved.
enapupe commentedon Jul 1, 2019
@esetnik I've filed an issue on that: #2131
My currrent workaround is a wrapper flush fn that always resolves (based on a timeout):
esetnik commentedon Jul 1, 2019
@enapupe I added a note about your workaround in #2131. I believe it will cause a performance regression on concurrent flush.
SarasArya commentedon Aug 10, 2019
In case anybody is having any issues.
This works beautifully
cibergarri commentedon Dec 10, 2019
@SarasArya @HazAT
First of all... Thanks for sharing your solution! :)
There is one callback of configureScope method that I guess it is supposed to be called before captureException but it is not being done in the same "thread".
Couldn't this lead to the appearance of race conditions?
SarasArya commentedon Dec 11, 2019
@cibergarri I don't think so, looks synchronous to me, in case you have an async method in there, then there would be race conditions.
Consider is like .map of array's that the same thing happening here. In case you have issues wrapping your head around it. I hope that helps.
kamilogorek commentedon Dec 11, 2019
Yeah, it's totally fine to do that
ajjindal commentedon Sep 21, 2020
Update: Sentry now supports automated error capture for Node/Lambda environments: https://docs.sentry.io/platforms/node/guides/aws-lambda/
armando25723 commentedon Nov 9, 2020
I'm using @sentry/serverless like this:
It does not work on lambda.
In my testing env it was working but in prod where there is a lot a concurrent executions and the containers are reuse it is logging just about 10% of the total amount.
Any advice?
marshall-lee commentedon Nov 10, 2020
@armando25723
Please tell how did your measure that it loses the exception events? Do you have a code sample of how such lost exception was thrown? Need more context.
armando25723 commentedon Nov 10, 2020
What is happening?
If the function is invoked several times with short interval between invocations the event is only logged once.
If the time interval between invocations is larger all events are logged.
I assume that the problem is when the invocation is over a reused container.
I have tried also
await Sentry.captureException(error);and:
await Sentry.flush();and without flushing
same result
armando25723 commentedon Nov 11, 2020
@marshall-lee what do you recomend? Should I create an issue, I'm stuck here.
ajjindal commentedon Nov 11, 2020
@armando25723 Looks like the server is responding with 429 (too many exceptions) while sending these events. We throw that in case of over quota/rate limiting scenarios. Do you know if you are sequentially sending errors or over quota? We can debug further if you think these are real error events getting dropped and you are under our 5k limit for the free tier.
armando25723 commentedon Nov 11, 2020
@ajjindal all other projects are working fine with sentry. The organization slug is "alegra", project name is mail-dispatch-serverless under #mail-micros. We have been using sentry for a long time, but first time with serverless. This is not free tier, I can't tell you exactly which plan we are using but it is a paid one.
It will be nice if you could help me to debug further.
Thanks for reply : )
PD: is Team Plan
dborstelmann commentedon Nov 13, 2021
I'm having the same issue here, doing
await Sentry.flush(1000);after captureException is not sending the errors to sentry. Filed a new issue here if anyone has any advice: #4155