initializeSdk.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. _experiments: {
  51. enableInteractions: true,
  52. },
  53. ...partialTracingOptions,
  54. }),
  55. ];
  56. return integrations;
  57. }
  58. /**
  59. * Initialize the Sentry SDK
  60. *
  61. * If `routes` is passed, we will instrument react-router. Not all
  62. * entrypoints require this.
  63. */
  64. export function initializeSdk(config: Config, {routes}: {routes?: Function} = {}) {
  65. const {apmSampling, sentryConfig, userIdentity} = config;
  66. const tracesSampleRate = apmSampling ?? 0;
  67. Sentry.init({
  68. ...sentryConfig,
  69. /**
  70. * For SPA mode, we need a way to overwrite the default DSN from backend
  71. * as well as `whitelistUrls`
  72. */
  73. dsn: SPA_DSN || sentryConfig?.dsn,
  74. /**
  75. * Frontend can be built with a `SENTRY_RELEASE_VERSION` environment
  76. * variable for release string, useful if frontend is deployed separately
  77. * from backend.
  78. */
  79. release: SENTRY_RELEASE_VERSION ?? sentryConfig?.release,
  80. allowUrls: SPA_DSN ? SPA_MODE_ALLOW_URLS : sentryConfig?.whitelistUrls,
  81. integrations: getSentryIntegrations(sentryConfig, routes),
  82. tracesSampleRate,
  83. tracesSampler: context => {
  84. if (context.transactionContext.op?.startsWith('ui.action')) {
  85. return tracesSampleRate / 100;
  86. }
  87. return tracesSampleRate;
  88. },
  89. /**
  90. * There is a bug in Safari, that causes `AbortError` when fetch is
  91. * aborted, and you are in the middle of reading the response. In Chrome
  92. * and other browsers, it is handled gracefully, where in Safari, it
  93. * produces additional error, that is jumping outside of the original
  94. * Promise chain and bubbles up to the `unhandledRejection` handler, that
  95. * we then captures as error.
  96. *
  97. * Ref: https://bugs.webkit.org/show_bug.cgi?id=215771
  98. */
  99. ignoreErrors: ['AbortError: Fetch is aborted'],
  100. });
  101. // Track timeOrigin Selection by the SDK to see if it improves transaction durations
  102. Sentry.addGlobalEventProcessor((event: Sentry.Event, _hint?: Sentry.EventHint) => {
  103. event.tags = event.tags || {};
  104. event.tags['timeOrigin.mode'] = _browserPerformanceTimeOriginMode;
  105. return event;
  106. });
  107. if (userIdentity) {
  108. Sentry.setUser(userIdentity);
  109. }
  110. if (window.__SENTRY__VERSION) {
  111. Sentry.setTag('sentry_version', window.__SENTRY__VERSION);
  112. }
  113. const {customerDomain} = window.__initialData;
  114. if (customerDomain) {
  115. Sentry.setTag('isCustomerDomain', 'yes');
  116. Sentry.setTag('customerDomain.organizationUrl', customerDomain.organizationUrl);
  117. Sentry.setTag('customerDomain.sentryUrl', customerDomain.sentryUrl);
  118. Sentry.setTag('customerDomain.subdomain', customerDomain.subdomain);
  119. }
  120. LongTaskObserver.startPerformanceObserver();
  121. initializeMeasureAssetsTimeout();
  122. }