initializeSdk.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 {addExtraMeasurements, LongTaskObserver} from 'sentry/utils/performanceForSentry';
  9. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  10. const SPA_MODE_ALLOW_URLS = [
  11. 'localhost',
  12. 'dev.getsentry.net',
  13. 'sentry.dev',
  14. 'webpack-internal://',
  15. ];
  16. /**
  17. * We accept a routes argument here because importing `static/routes`
  18. * is expensive in regards to bundle size. Some entrypoints may opt to forgo
  19. * having routing instrumentation in order to have a smaller bundle size.
  20. * (e.g. `static/views/integrationPipeline`)
  21. */
  22. function getSentryIntegrations(sentryConfig: Config['sentryConfig'], routes?: Function) {
  23. const extraTracingOrigins = SPA_DSN
  24. ? SPA_MODE_ALLOW_URLS
  25. : [...sentryConfig?.whitelistUrls];
  26. const partialTracingOptions: Partial<Integrations.BrowserTracing['options']> = {
  27. tracingOrigins: ['localhost', /^\//, ...extraTracingOrigins],
  28. };
  29. const integrations = [
  30. new ExtraErrorData({
  31. // 6 is arbitrary, seems like a nice number
  32. depth: 6,
  33. }),
  34. new Integrations.BrowserTracing({
  35. ...(typeof routes === 'function'
  36. ? {
  37. routingInstrumentation: Sentry.reactRouterV3Instrumentation(
  38. browserHistory as any,
  39. createRoutes(routes()),
  40. match
  41. ),
  42. }
  43. : {}),
  44. _experiments: {
  45. enableInteractions: true,
  46. },
  47. ...partialTracingOptions,
  48. }),
  49. ];
  50. return integrations;
  51. }
  52. /**
  53. * Initialize the Sentry SDK
  54. *
  55. * If `routes` is passed, we will instrument react-router. Not all
  56. * entrypoints require this.
  57. */
  58. export function initializeSdk(config: Config, {routes}: {routes?: Function} = {}) {
  59. const {apmSampling, sentryConfig, userIdentity} = config;
  60. const tracesSampleRate = apmSampling ?? 0;
  61. Sentry.init({
  62. ...sentryConfig,
  63. /**
  64. * For SPA mode, we need a way to overwrite the default DSN from backend
  65. * as well as `whitelistUrls`
  66. */
  67. dsn: SPA_DSN || sentryConfig?.dsn,
  68. /**
  69. * Frontend can be built with a `SENTRY_RELEASE_VERSION` environment
  70. * variable for release string, useful if frontend is deployed separately
  71. * from backend.
  72. */
  73. release: SENTRY_RELEASE_VERSION ?? sentryConfig?.release,
  74. allowUrls: SPA_DSN ? SPA_MODE_ALLOW_URLS : sentryConfig?.whitelistUrls,
  75. integrations: getSentryIntegrations(sentryConfig, routes),
  76. tracesSampleRate,
  77. tracesSampler: context => {
  78. if (context.transactionContext.op?.startsWith('ui.action')) {
  79. return tracesSampleRate / 100;
  80. }
  81. return tracesSampleRate;
  82. },
  83. beforeSendTransaction(event) {
  84. addExtraMeasurements(event);
  85. event.spans = event.spans?.filter(span => {
  86. // Filter analytic timeout spans.
  87. return ['reload.getsentry.net', 'amplitude.com'].every(
  88. partialDesc => !span.description?.includes(partialDesc)
  89. );
  90. });
  91. if (event.transaction) {
  92. event.transaction = normalizeUrl(event.transaction, {forceCustomerDomain: true});
  93. }
  94. return event;
  95. },
  96. /**
  97. * There is a bug in Safari, that causes `AbortError` when fetch is
  98. * aborted, and you are in the middle of reading the response. In Chrome
  99. * and other browsers, it is handled gracefully, where in Safari, it
  100. * produces additional error, that is jumping outside of the original
  101. * Promise chain and bubbles up to the `unhandledRejection` handler, that
  102. * we then captures as error.
  103. *
  104. * Ref: https://bugs.webkit.org/show_bug.cgi?id=215771
  105. */
  106. ignoreErrors: ['AbortError: Fetch is aborted'],
  107. });
  108. // Track timeOrigin Selection by the SDK to see if it improves transaction durations
  109. Sentry.addGlobalEventProcessor((event: Sentry.Event, _hint?: Sentry.EventHint) => {
  110. event.tags = event.tags || {};
  111. event.tags['timeOrigin.mode'] = _browserPerformanceTimeOriginMode;
  112. return event;
  113. });
  114. if (userIdentity) {
  115. Sentry.setUser(userIdentity);
  116. }
  117. if (window.__SENTRY__VERSION) {
  118. Sentry.setTag('sentry_version', window.__SENTRY__VERSION);
  119. }
  120. const {customerDomain} = window.__initialData;
  121. if (customerDomain) {
  122. Sentry.setTag('isCustomerDomain', 'yes');
  123. Sentry.setTag('customerDomain.organizationUrl', customerDomain.organizationUrl);
  124. Sentry.setTag('customerDomain.sentryUrl', customerDomain.sentryUrl);
  125. Sentry.setTag('customerDomain.subdomain', customerDomain.subdomain);
  126. }
  127. LongTaskObserver.startPerformanceObserver();
  128. }