initializeSdk.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 {BrowserTracing} from '@sentry/react';
  6. import {_browserPerformanceTimeOriginMode} from '@sentry/utils';
  7. import {Event} from '@sentry/types';
  8. import {SENTRY_RELEASE_VERSION, SPA_DSN} from 'sentry/constants';
  9. import {Config} from 'sentry/types';
  10. import {addExtraMeasurements, addUIElementTag} from 'sentry/utils/performanceForSentry';
  11. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  12. import {HTTPTimingIntegration} from 'sentry/utils/performanceForSentry/integrations';
  13. const SPA_MODE_ALLOW_URLS = [
  14. 'localhost',
  15. 'dev.getsentry.net',
  16. 'sentry.dev',
  17. 'webpack-internal://',
  18. ];
  19. // We check for `window.__initialData.user` property and only enable profiling
  20. // for Sentry employees. This is to prevent a Violation error being visible in
  21. // the browser console for our users.
  22. const shouldEnableBrowserProfiling = window?.__initialData?.user?.isSuperuser;
  23. /**
  24. * We accept a routes argument here because importing `static/routes`
  25. * is expensive in regards to bundle size. Some entrypoints may opt to forgo
  26. * having routing instrumentation in order to have a smaller bundle size.
  27. * (e.g. `static/views/integrationPipeline`)
  28. */
  29. function getSentryIntegrations(sentryConfig: Config['sentryConfig'], routes?: Function) {
  30. const extraTracingOrigins = SPA_DSN
  31. ? SPA_MODE_ALLOW_URLS
  32. : [...sentryConfig?.whitelistUrls];
  33. const partialTracingOptions: Partial<BrowserTracing['options']> = {
  34. tracingOrigins: ['localhost', /^\//, ...extraTracingOrigins],
  35. };
  36. const integrations = [
  37. new ExtraErrorData({
  38. // 6 is arbitrary, seems like a nice number
  39. depth: 6,
  40. }),
  41. new BrowserTracing({
  42. ...(typeof routes === 'function'
  43. ? {
  44. routingInstrumentation: Sentry.reactRouterV3Instrumentation(
  45. browserHistory as any,
  46. createRoutes(routes()),
  47. match
  48. ),
  49. }
  50. : {}),
  51. _experiments: {
  52. enableInteractions: true,
  53. onStartRouteTransaction: Sentry.onProfilingStartRouteTransaction,
  54. },
  55. ...partialTracingOptions,
  56. }),
  57. new Sentry.BrowserProfilingIntegration(),
  58. new HTTPTimingIntegration(),
  59. ];
  60. return integrations;
  61. }
  62. /**
  63. * Initialize the Sentry SDK
  64. *
  65. * If `routes` is passed, we will instrument react-router. Not all
  66. * entrypoints require this.
  67. */
  68. export function initializeSdk(config: Config, {routes}: {routes?: Function} = {}) {
  69. const {apmSampling, sentryConfig, userIdentity} = config;
  70. const tracesSampleRate = apmSampling ?? 0;
  71. Sentry.init({
  72. ...sentryConfig,
  73. /**
  74. * For SPA mode, we need a way to overwrite the default DSN from backend
  75. * as well as `whitelistUrls`
  76. */
  77. dsn: SPA_DSN || sentryConfig?.dsn,
  78. /**
  79. * Frontend can be built with a `SENTRY_RELEASE_VERSION` environment
  80. * variable for release string, useful if frontend is deployed separately
  81. * from backend.
  82. */
  83. release: SENTRY_RELEASE_VERSION ?? sentryConfig?.release,
  84. allowUrls: SPA_DSN ? SPA_MODE_ALLOW_URLS : sentryConfig?.whitelistUrls,
  85. integrations: getSentryIntegrations(sentryConfig, routes),
  86. tracesSampleRate,
  87. // @ts-ignore not part of browser SDK types yet
  88. profilesSampleRate: shouldEnableBrowserProfiling ? 1 : 0,
  89. tracesSampler: context => {
  90. if (context.transactionContext.op?.startsWith('ui.action')) {
  91. return tracesSampleRate / 100;
  92. }
  93. return tracesSampleRate;
  94. },
  95. beforeSendTransaction(event) {
  96. addExtraMeasurements(event);
  97. addUIElementTag(event);
  98. event.spans = event.spans?.filter(span => {
  99. // Filter analytic timeout spans.
  100. return ['reload.getsentry.net', 'amplitude.com'].every(
  101. partialDesc => !span.description?.includes(partialDesc)
  102. );
  103. });
  104. if (event.transaction) {
  105. event.transaction = normalizeUrl(event.transaction, {forceCustomerDomain: true});
  106. }
  107. return event;
  108. },
  109. ignoreErrors: [
  110. /**
  111. * There is a bug in Safari, that causes `AbortError` when fetch is
  112. * aborted, and you are in the middle of reading the response. In Chrome
  113. * and other browsers, it is handled gracefully, where in Safari, it
  114. * produces additional error, that is jumping outside of the original
  115. * Promise chain and bubbles up to the `unhandledRejection` handler, that
  116. * we then captures as error.
  117. *
  118. * Ref: https://bugs.webkit.org/show_bug.cgi?id=215771
  119. */
  120. 'AbortError: Fetch is aborted',
  121. /**
  122. * Thrown when firefox prevents an add-on from refrencing a DOM element
  123. * that has been removed.
  124. */
  125. "TypeError: can't access dead object",
  126. /**
  127. * React internal error thrown when something outside react modifies the DOM
  128. * This is usually because of a browser extension or chrome translate page
  129. */
  130. "NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",
  131. "NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.",
  132. ],
  133. beforeSend(event, _hint) {
  134. if (isFilteredRequestErrorEvent(event) || isEventWithFileUrl(event)) {
  135. return null;
  136. }
  137. return event;
  138. },
  139. });
  140. // Track timeOrigin Selection by the SDK to see if it improves transaction durations
  141. Sentry.addGlobalEventProcessor((event: Sentry.Event, _hint?: Sentry.EventHint) => {
  142. event.tags = event.tags || {};
  143. event.tags['timeOrigin.mode'] = _browserPerformanceTimeOriginMode;
  144. return event;
  145. });
  146. if (userIdentity) {
  147. Sentry.setUser(userIdentity);
  148. }
  149. if (window.__SENTRY__VERSION) {
  150. Sentry.setTag('sentry_version', window.__SENTRY__VERSION);
  151. }
  152. const {customerDomain} = window.__initialData;
  153. if (customerDomain) {
  154. Sentry.setTag('isCustomerDomain', 'yes');
  155. Sentry.setTag('customerDomain.organizationUrl', customerDomain.organizationUrl);
  156. Sentry.setTag('customerDomain.sentryUrl', customerDomain.sentryUrl);
  157. Sentry.setTag('customerDomain.subdomain', customerDomain.subdomain);
  158. }
  159. }
  160. export function isFilteredRequestErrorEvent(event: Event): boolean {
  161. const exceptionValues = event.exception?.values;
  162. if (!exceptionValues) {
  163. return false;
  164. }
  165. // In case there's a chain, we take the last entry, because that's the one
  166. // passed to `captureException`, and the one right before that, since
  167. // `RequestError`s are used as the main error's `cause` value in
  168. // `handleXhrErrorResponse`
  169. const mainAndMaybeCauseErrors = exceptionValues.slice(-2);
  170. for (const error of mainAndMaybeCauseErrors) {
  171. const {type = '', value = ''} = error;
  172. const is200 =
  173. ['RequestError'].includes(type) && !!value.match('(GET|POST|PUT|DELETE) .* 200');
  174. const is401 =
  175. ['UnauthorizedError', 'RequestError'].includes(type) &&
  176. !!value.match('(GET|POST|PUT|DELETE) .* 401');
  177. const is403 =
  178. ['ForbiddenError', 'RequestError'].includes(type) &&
  179. !!value.match('(GET|POST|PUT|DELETE) .* 403');
  180. const is404 =
  181. ['NotFoundError', 'RequestError'].includes(type) &&
  182. !!value.match('(GET|POST|PUT|DELETE) .* 404');
  183. const is429 =
  184. ['TooManyRequestsError', 'RequestError'].includes(type) &&
  185. !!value.match('(GET|POST|PUT|DELETE) .* 429');
  186. if (is200 || is401 || is403 || is404 || is429) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. export function isEventWithFileUrl(event: Event): boolean {
  193. return !!event.request?.url?.startsWith('file://');
  194. }