initializeSdk.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {browserHistory, createRoutes, match} from 'react-router';
  2. import {ExtraErrorData} from '@sentry/integrations';
  3. import * as Sentry from '@sentry/react';
  4. import {Integrations} from '@sentry/tracing';
  5. import {_browserPerformanceTimeOriginMode} from '@sentry/utils';
  6. import {SENTRY_RELEASE_VERSION, SPA_DSN} from 'sentry/constants';
  7. import {Config} from 'sentry/types';
  8. import {
  9. initializeMeasureAssetsTimeout,
  10. LongTaskObserver,
  11. } from 'sentry/utils/performanceForSentry';
  12. const SPA_MODE_ALLOW_URLS = [
  13. 'localhost',
  14. 'dev.getsentry.net',
  15. 'sentry.dev',
  16. 'webpack-internal://',
  17. ];
  18. /**
  19. * We accept a routes argument here because importing `static/routes`
  20. * is expensive in regards to bundle size. Some entrypoints may opt to forgo
  21. * having routing instrumentation in order to have a smaller bundle size.
  22. * (e.g. `static/views/integrationPipeline`)
  23. */
  24. function getSentryIntegrations(sentryConfig: Config['sentryConfig'], routes?: Function) {
  25. const extraTracingOrigins = SPA_DSN
  26. ? SPA_MODE_ALLOW_URLS
  27. : [...sentryConfig?.whitelistUrls];
  28. const partialTracingOptions: Partial<Integrations.BrowserTracing['options']> = {
  29. tracingOrigins: ['localhost', /^\//, ...extraTracingOrigins],
  30. };
  31. const integrations = [
  32. new ExtraErrorData({
  33. // 6 is arbitrary, seems like a nice number
  34. depth: 6,
  35. }),
  36. new Integrations.BrowserTracing({
  37. ...(typeof routes === 'function'
  38. ? {
  39. routingInstrumentation: Sentry.reactRouterV3Instrumentation(
  40. browserHistory as any,
  41. createRoutes(routes()),
  42. match
  43. ),
  44. }
  45. : {}),
  46. idleTimeout: 5000,
  47. _metricOptions: {
  48. _reportAllChanges: false,
  49. },
  50. ...partialTracingOptions,
  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. Sentry.init({
  65. ...sentryConfig,
  66. /**
  67. * For SPA mode, we need a way to overwrite the default DSN from backend
  68. * as well as `whitelistUrls`
  69. */
  70. dsn: SPA_DSN || sentryConfig?.dsn,
  71. /**
  72. * Frontend can be built with a `SENTRY_RELEASE_VERSION` environment
  73. * variable for release string, useful if frontend is deployed separately
  74. * from backend.
  75. */
  76. release: SENTRY_RELEASE_VERSION ?? sentryConfig?.release,
  77. allowUrls: SPA_DSN ? SPA_MODE_ALLOW_URLS : sentryConfig?.whitelistUrls,
  78. integrations: getSentryIntegrations(sentryConfig, routes),
  79. tracesSampleRate,
  80. /**
  81. * There is a bug in Safari, that causes `AbortError` when fetch is
  82. * aborted, and you are in the middle of reading the response. In Chrome
  83. * and other browsers, it is handled gracefully, where in Safari, it
  84. * produces additional error, that is jumping outside of the original
  85. * Promise chain and bubbles up to the `unhandledRejection` handler, that
  86. * we then captures as error.
  87. *
  88. * Ref: https://bugs.webkit.org/show_bug.cgi?id=215771
  89. */
  90. ignoreErrors: ['AbortError: Fetch is aborted'],
  91. });
  92. // Track timeOrigin Selection by the SDK to see if it improves transaction durations
  93. Sentry.addGlobalEventProcessor((event: Sentry.Event, _hint?: Sentry.EventHint) => {
  94. event.tags = event.tags || {};
  95. event.tags['timeOrigin.mode'] = _browserPerformanceTimeOriginMode;
  96. return event;
  97. });
  98. if (userIdentity) {
  99. Sentry.setUser(userIdentity);
  100. }
  101. if (window.__SENTRY__VERSION) {
  102. Sentry.setTag('sentry_version', window.__SENTRY__VERSION);
  103. }
  104. const {customerDomain} = window.__initialData;
  105. if (customerDomain) {
  106. Sentry.setTag('isCustomerDomain', 'yes');
  107. Sentry.setTag('customerDomain.organizationUrl', customerDomain.organizationUrl);
  108. Sentry.setTag('customerDomain.sentryUrl', customerDomain.sentryUrl);
  109. Sentry.setTag('customerDomain.subdomain', customerDomain.subdomain);
  110. }
  111. LongTaskObserver.startPerformanceObserver();
  112. initializeMeasureAssetsTimeout();
  113. }