responseEnd.js 2.5 KB

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