Skip to content

Add auto-flushing to node server middleware #2001

Closed
@sheerun

Description

@sheerun

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions