initializeSdk.tsx 10.0 KB

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