responseEnd.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { flush, setHttpStatus } from '@sentry/core';
  2. import { logger, fill } from '@sentry/utils';
  3. import { DEBUG_BUILD } from '../debug-build.js';
  4. /**
  5. * Wrap `res.end()` so that it ends the span and flushes events before letting the request finish.
  6. *
  7. * Note: This wraps a sync method with an async method. While in general that's not a great idea in terms of keeping
  8. * things in the right order, in this case it's safe, because the native `.end()` actually *is* (effectively) async, and
  9. * its run actually *is* (literally) awaited, just manually so (which reflects the fact that the core of the
  10. * request/response code in Node by far predates the introduction of `async`/`await`). When `.end()` is done, it emits
  11. * the `prefinish` event, and only once that fires does request processing continue. See
  12. * https://github.com/nodejs/node/commit/7c9b607048f13741173d397795bac37707405ba7.
  13. *
  14. * Also note: `res.end()` isn't called until *after* all response data and headers have been sent, so blocking inside of
  15. * `end` doesn't delay data getting to the end user. See
  16. * https://nodejs.org/api/http.html#responseenddata-encoding-callback.
  17. *
  18. * @param span The span tracking the request
  19. * @param res: The request's corresponding response
  20. */
  21. function autoEndSpanOnResponseEnd(span, res) {
  22. const wrapEndMethod = (origEnd) => {
  23. return function sentryWrappedEnd( ...args) {
  24. finishSpan(span, this);
  25. return origEnd.call(this, ...args);
  26. };
  27. };
  28. // Prevent double-wrapping
  29. // res.end may be undefined during build when using `next export` to statically export a Next.js app
  30. if (res.end && !(res.end ).__sentry_original__) {
  31. fill(res, 'end', wrapEndMethod);
  32. }
  33. }
  34. /** Finish the given response's span and set HTTP status data */
  35. function finishSpan(span, res) {
  36. if (span) {
  37. setHttpStatus(span, res.statusCode);
  38. span.end();
  39. }
  40. }
  41. /** Flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda ends */
  42. async function flushQueue() {
  43. try {
  44. DEBUG_BUILD && logger.log('Flushing events...');
  45. await flush(2000);
  46. DEBUG_BUILD && logger.log('Done flushing events');
  47. } catch (e) {
  48. DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
  49. }
  50. }
  51. export { autoEndSpanOnResponseEnd, finishSpan, flushQueue };
  52. //# sourceMappingURL=responseEnd.js.map