wrapGetServerSidePropsWithSentry.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
  2. import { addTracingExtensions, getActiveSpan, spanToTraceHeader, getDynamicSamplingContextFromSpan, getClient, getRootSpan } from '@sentry/core';
  3. import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
  4. import { isBuild } from './utils/isBuild.js';
  5. import { getSpanFromRequest, withErrorInstrumentation, withTracedServerSideDataFetcher } from './utils/wrapperUtils.js';
  6. /**
  7. * Create a wrapped version of the user's exported `getServerSideProps` function
  8. *
  9. * @param origGetServerSideProps The user's `getServerSideProps` function
  10. * @param parameterizedRoute The page's parameterized route
  11. * @returns A wrapped version of the function
  12. */
  13. function wrapGetServerSidePropsWithSentry(
  14. origGetServerSideProps,
  15. parameterizedRoute,
  16. ) {
  17. return new Proxy(origGetServerSideProps, {
  18. apply: async (wrappingTarget, thisArg, args) => {
  19. if (isBuild()) {
  20. return wrappingTarget.apply(thisArg, args);
  21. }
  22. addTracingExtensions();
  23. const [context] = args;
  24. const { req, res } = context;
  25. const errorWrappedGetServerSideProps = withErrorInstrumentation(wrappingTarget);
  26. const options = _optionalChain([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
  27. if (_optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
  28. const tracedGetServerSideProps = withTracedServerSideDataFetcher(errorWrappedGetServerSideProps, req, res, {
  29. dataFetcherRouteName: parameterizedRoute,
  30. requestedRouteName: parameterizedRoute,
  31. dataFetchingMethodName: 'getServerSideProps',
  32. });
  33. const serverSideProps = await (tracedGetServerSideProps.apply(thisArg, args)
  34. );
  35. if (serverSideProps && 'props' in serverSideProps) {
  36. const activeSpan = getActiveSpan();
  37. const requestTransaction = _nullishCoalesce(getSpanFromRequest(req), () => ( (activeSpan ? getRootSpan(activeSpan) : undefined)));
  38. if (requestTransaction) {
  39. serverSideProps.props._sentryTraceData = spanToTraceHeader(requestTransaction);
  40. const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestTransaction);
  41. serverSideProps.props._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
  42. }
  43. }
  44. return serverSideProps;
  45. } else {
  46. return errorWrappedGetServerSideProps.apply(thisArg, args);
  47. }
  48. },
  49. });
  50. }
  51. /**
  52. * @deprecated Use `withSentryGetServerSideProps` instead.
  53. */
  54. const withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry;
  55. export { withSentryGetServerSideProps, wrapGetServerSidePropsWithSentry };
  56. //# sourceMappingURL=wrapGetServerSidePropsWithSentry.js.map