initializeSdk.tsx 11 KB

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