initializeSdk.tsx 9.0 KB

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