arabold / serverless-sentry-lib

MIT License
32 stars 15 forks source link

Sentry shows logs from previous invocations #26

Closed demsey2 closed 4 years ago

demsey2 commented 4 years ago

I am having a problem with withSentry wrapper. Not sure if I do something wrong or it's a bug.

If I have a lambda with the following setup

import withSentry from "serverless-sentry-lib";

export const handler = withSentry(async (event, context) => {
  console.log('start');
  Sentry.addBreadcrumb({ type: 'test' });
  throw new Error('testing Sentry');
  return;
});

and when I call that lambda first time (cold start), Sentry reports an error and I can see all logs/breadcrumbs fine when I call that lambda again (not a cold start) then in Sentry I can see logs from that invocations and from the previous one. This

To avoid that issue I had to create a new instance of Sentry every time, call init() and scope.clear(), by doing so I am losing integration with serverless-sentry plugin and I had to manually set tags etc.

arabold commented 4 years ago

Thanks for the bug report. I'll need to reproduce it but I can already tell you that the current version of the lib doesn't do a good job cleaning up the scope when an exception is thrown like in the example above. AWS is reusing your Lambda instance in subsequent invocations which apparently leads to the scope being reused as well, thus reporting logs from previous invocations. That's something I will need to address in the next update of the lib.

arabold commented 4 years ago

@demsey2 , if you get a chance, I would appreciate you trying serverless-sentry-lib v2.2.0. I have added an explicit scope.clear() at the beginning of the Lambda invocation. This should fix the issue you reported but I couldn't verify it myself yet.

demsey2 commented 4 years ago

@arabold I did a quick test and it looks like it works fine, I am going to test that in production with a larger number of functions if I got any issues I will let you know

thanks