Skip to content

Add auto-flushing to node server middleware #2001

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

Closed
sheerun opened this issue Apr 6, 2019 · 3 comments
Closed

Add auto-flushing to node server middleware #2001

sheerun opened this issue Apr 6, 2019 · 3 comments

Comments

@sheerun
Copy link
Contributor

sheerun commented Apr 6, 2019

Currently serverless deployments of @sentry/node are suffering from common issue of requests terminating before error is fully sent to Sentry. There has been some solutions proposed as using custom transport or overwriting captureException, but non of them will really work because captureException is almost always called without await, also in native Sentry integrations like Sentry.Handlers.errorHandler(). It means that in such cases lambda request will end without waiting for Sentry to report the error.

The only real solution to this problem is to create afterware that flushes sentry before sending each request (after request ends any remaining asynchronous code is terminated). I'd like to propose @sentry/node integration to add auto-flushing feature to Sentry.Handlers.requestHandler() (optionally Sentry.Handlers.requestHandler({ wait: true })) with code similar to following:

app.use(async (req, res, next) => {
  const _end = res.end

  res.end = async function end (chunk, encoding) {
    await Sentry.flush()
    return _end.call(this, chunk, encoding)
  }

  next()
});

It will force server to wait for any errors to be reported before sending finalizing request.

Please note that it won't fully work until #2000 is fixed because currently _end.call is called before transport has chance to fully deliver event.

@HazAT
Copy link
Member

HazAT commented Apr 9, 2019

@sheerun Nice idea, would you mind to create a PR for Sentry.Handlers.requestHandler({ wait: true })?

@sheerun
Copy link
Contributor Author

sheerun commented May 12, 2019

This is already done

@sheerun sheerun closed this as completed May 12, 2019
@andidev
Copy link

andidev commented Jan 7, 2020

Wow, I've been looking to solve this. The final syntax is Sentry.Handlers.requestHandler({ flushTimeout: 2000 })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants