initializeSdk.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {browserHistory, createRoutes, match} from 'react-router';
  2. import {ExtraErrorData} from '@sentry/integrations';
  3. import * as Sentry from '@sentry/react';
  4. import SentryRRWeb from '@sentry/rrweb';
  5. import {Integrations} from '@sentry/tracing';
  6. import {_browserPerformanceTimeOriginMode} from '@sentry/utils';
  7. import {DISABLE_RR_WEB, SENTRY_RELEASE_VERSION, SPA_DSN} from 'sentry/constants';
  8. import {Config} from 'sentry/types';
  9. import {
  10. initializeMeasureAssetsTimeout,
  11. LongTaskObserver,
  12. } from 'sentry/utils/performanceForSentry';
  13. /**
  14. * We accept a routes argument here because importing `static/routes`
  15. * is expensive in regards to bundle size. Some entrypoints may opt to forgo
  16. * having routing instrumentation in order to have a smaller bundle size.
  17. * (e.g. `static/views/integrationPipeline`)
  18. */
  19. function getSentryIntegrations(hasReplays: boolean = false, routes?: Function) {
  20. const integrations = [
  21. new ExtraErrorData({
  22. // 6 is arbitrary, seems like a nice number
  23. depth: 6,
  24. }),
  25. new Integrations.BrowserTracing({
  26. ...(typeof routes === 'function'
  27. ? {
  28. routingInstrumentation: Sentry.reactRouterV3Instrumentation(
  29. browserHistory as any,
  30. createRoutes(routes()),
  31. match
  32. ),
  33. }
  34. : {}),
  35. idleTimeout: 5000,
  36. _metricOptions: {
  37. _reportAllChanges: false,
  38. },
  39. }),
  40. ];
  41. if (hasReplays) {
  42. // eslint-disable-next-line no-console
  43. console.log('[sentry] Instrumenting session with rrweb');
  44. // TODO(ts): The type returned by SentryRRWeb seems to be somewhat
  45. // incompatible. It's a newer plugin, so this can be expected, but we
  46. // should fix.
  47. integrations.push(
  48. new SentryRRWeb({
  49. checkoutEveryNms: 60 * 1000, // 60 seconds
  50. }) as any
  51. );
  52. }
  53. return integrations;
  54. }
  55. /**
  56. * Initialize the Sentry SDK
  57. *
  58. * If `routes` is passed, we will instrument react-router. Not all
  59. * entrypoints require this.
  60. */
  61. export function initializeSdk(config: Config, {routes}: {routes?: Function} = {}) {
  62. const {apmSampling, sentryConfig, userIdentity} = config;
  63. const tracesSampleRate = apmSampling ?? 0;
  64. const hasReplays = userIdentity?.isStaff && !DISABLE_RR_WEB;
  65. Sentry.init({
  66. ...sentryConfig,
  67. /**
  68. * For SPA mode, we need a way to overwrite the default DSN from backend
  69. * as well as `whitelistUrls`
  70. */
  71. dsn: SPA_DSN || sentryConfig?.dsn,
  72. /**
  73. * Frontend can be built with a `SENTRY_RELEASE_VERSION` environment variable for release string, useful if frontend is
  74. * deployed separately from backend.
  75. */
  76. release: SENTRY_RELEASE_VERSION ?? sentryConfig?.release,
  77. allowUrls: SPA_DSN
  78. ? ['localhost', 'dev.getsentry.net', 'sentry.dev', 'webpack-internal://']
  79. : sentryConfig?.whitelistUrls,
  80. integrations: getSentryIntegrations(hasReplays, routes),
  81. tracesSampleRate,
  82. /**
  83. * There is a bug in Safari, that causes `AbortError` when fetch is aborted, and you are in the middle of reading the response.
  84. * In Chrome and other browsers, it is handled gracefully, where in Safari, it produces additional error, that is jumping
  85. * outside of the original Promise chain and bubbles up to the `unhandledRejection` handler, that we then captures as error.
  86. * Ref: https://bugs.webkit.org/show_bug.cgi?id=215771
  87. */
  88. ignoreErrors: ['AbortError: Fetch is aborted'],
  89. });
  90. // Track timeOrigin Selection by the SDK to see if it improves transaction durations
  91. Sentry.addGlobalEventProcessor((event: Sentry.Event, _hint?: Sentry.EventHint) => {
  92. event.tags = event.tags || {};
  93. event.tags['timeOrigin.mode'] = _browserPerformanceTimeOriginMode;
  94. return event;
  95. });
  96. if (userIdentity) {
  97. Sentry.setUser(userIdentity);
  98. }
  99. if (window.__SENTRY__VERSION) {
  100. Sentry.setTag('sentry_version', window.__SENTRY__VERSION);
  101. }
  102. Sentry.setTag('rrweb.active', hasReplays ? 'yes' : 'no');
  103. LongTaskObserver.startPerformanceObserver();
  104. initializeMeasureAssetsTimeout();
  105. }