initializeSdk.tsx 5.4 KB

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