edgeWrapperUtils.js 2.1 KB

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