edgeWrapperUtils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. const responseEnd = require('./responseEnd.js');
  5. /**
  6. * Wraps a function on the edge runtime with error and performance monitoring.
  7. */
  8. function withEdgeWrapping(
  9. handler,
  10. options,
  11. ) {
  12. return async function ( ...args) {
  13. core.addTracingExtensions();
  14. const req = args[0];
  15. let sentryTrace;
  16. let baggage;
  17. if (req instanceof Request) {
  18. sentryTrace = req.headers.get('sentry-trace') || '';
  19. baggage = req.headers.get('baggage');
  20. }
  21. return core.continueTrace(
  22. {
  23. sentryTrace,
  24. baggage,
  25. },
  26. () => {
  27. return core.startSpan(
  28. {
  29. name: options.spanDescription,
  30. op: options.spanOp,
  31. attributes: {
  32. [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
  33. [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs.withEdgeWrapping',
  34. },
  35. metadata: {
  36. request: req instanceof Request ? utils.winterCGRequestToRequestData(req) : undefined,
  37. },
  38. },
  39. async span => {
  40. const handlerResult = await core.handleCallbackErrors(
  41. () => handler.apply(this, args),
  42. error => {
  43. core.captureException(error, {
  44. mechanism: {
  45. type: 'instrument',
  46. handled: false,
  47. data: {
  48. function: options.mechanismFunctionName,
  49. },
  50. },
  51. });
  52. },
  53. );
  54. if (span) {
  55. if (handlerResult instanceof Response) {
  56. core.setHttpStatus(span, handlerResult.status);
  57. } else {
  58. span.setStatus('ok');
  59. }
  60. }
  61. return handlerResult;
  62. },
  63. ).finally(() => responseEnd.flushQueue());
  64. },
  65. );
  66. };
  67. }
  68. exports.withEdgeWrapping = withEdgeWrapping;
  69. //# sourceMappingURL=edgeWrapperUtils.js.map