eventEntries.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import {memo, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {Location} from 'history';
  5. import uniq from 'lodash/uniq';
  6. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  7. import {Client} from 'sentry/api';
  8. import ErrorBoundary from 'sentry/components/errorBoundary';
  9. import EventContexts from 'sentry/components/events/contexts';
  10. import EventContextSummary from 'sentry/components/events/contextSummary';
  11. import EventDevice from 'sentry/components/events/device';
  12. import EventErrors, {Error} from 'sentry/components/events/errors';
  13. import EventAttachments from 'sentry/components/events/eventAttachments';
  14. import EventCause from 'sentry/components/events/eventCause';
  15. import EventCauseEmpty from 'sentry/components/events/eventCauseEmpty';
  16. import EventDataSection from 'sentry/components/events/eventDataSection';
  17. import EventExtraData from 'sentry/components/events/eventExtraData';
  18. import {EventSdk} from 'sentry/components/events/eventSdk';
  19. import {EventTags} from 'sentry/components/events/eventTags';
  20. import EventGroupingInfo from 'sentry/components/events/groupingInfo';
  21. import {EventPackageData} from 'sentry/components/events/packageData';
  22. import RRWebIntegration from 'sentry/components/events/rrwebIntegration';
  23. import EventSdkUpdates from 'sentry/components/events/sdkUpdates';
  24. import {DataSection} from 'sentry/components/events/styles';
  25. import EventUserFeedback from 'sentry/components/events/userFeedback';
  26. import ExternalLink from 'sentry/components/links/externalLink';
  27. import {t, tct} from 'sentry/locale';
  28. import space from 'sentry/styles/space';
  29. import {
  30. Entry,
  31. EntryType,
  32. Event,
  33. ExceptionValue,
  34. Group,
  35. IssueAttachment,
  36. Organization,
  37. Project,
  38. SharedViewOrganization,
  39. Thread,
  40. } from 'sentry/types';
  41. import {DebugFile} from 'sentry/types/debugFiles';
  42. import {Image} from 'sentry/types/debugImage';
  43. import {isNotSharedOrganization} from 'sentry/types/utils';
  44. import {defined, objectIsEmpty} from 'sentry/utils';
  45. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  46. import withApi from 'sentry/utils/withApi';
  47. import withOrganization from 'sentry/utils/withOrganization';
  48. import {projectProcessingIssuesMessages} from 'sentry/views/settings/project/projectProcessingIssues';
  49. import findBestThread from './interfaces/threads/threadSelector/findBestThread';
  50. import getThreadException from './interfaces/threads/threadSelector/getThreadException';
  51. import EventEntry from './eventEntry';
  52. import EventReplay from './eventReplay';
  53. import EventTagsAndScreenshot from './eventTagsAndScreenshot';
  54. const MINIFIED_DATA_JAVA_EVENT_REGEX_MATCH =
  55. /^(([\w\$]\.[\w\$]{1,2})|([\w\$]{2}\.[\w\$]\.[\w\$]))(\.|$)/g;
  56. type ProGuardErrors = Array<Error>;
  57. type Props = Pick<React.ComponentProps<typeof EventEntry>, 'route' | 'router'> & {
  58. api: Client;
  59. location: Location;
  60. /**
  61. * The organization can be the shared view on a public issue view.
  62. */
  63. organization: Organization | SharedViewOrganization;
  64. project: Project;
  65. className?: string;
  66. event?: Event;
  67. group?: Group;
  68. isShare?: boolean;
  69. showExampleCommit?: boolean;
  70. showTagSummary?: boolean;
  71. };
  72. const EventEntries = memo(
  73. ({
  74. organization,
  75. project,
  76. location,
  77. api,
  78. event,
  79. group,
  80. className,
  81. router,
  82. route,
  83. isShare = false,
  84. showExampleCommit = false,
  85. showTagSummary = true,
  86. }: Props) => {
  87. const [isLoading, setIsLoading] = useState(true);
  88. const [proGuardErrors, setProGuardErrors] = useState<ProGuardErrors>([]);
  89. const [attachments, setAttachments] = useState<IssueAttachment[]>([]);
  90. const orgSlug = organization.slug;
  91. const projectSlug = project.slug;
  92. const orgFeatures = organization?.features ?? [];
  93. const hasEventAttachmentsFeature = orgFeatures.includes('event-attachments');
  94. const replayId = event?.tags?.find(({key}) => key === 'replayId')?.value;
  95. useEffect(() => {
  96. checkProGuardError();
  97. recordIssueError();
  98. fetchAttachments();
  99. }, []);
  100. function recordIssueError() {
  101. if (!event || !event.errors || !(event.errors.length > 0)) {
  102. return;
  103. }
  104. const errors = event.errors;
  105. const errorTypes = errors.map(errorEntries => errorEntries.type);
  106. const errorMessages = errors.map(errorEntries => errorEntries.message);
  107. const platform = project.platform;
  108. // uniquify the array types
  109. trackAdvancedAnalyticsEvent('issue_error_banner.viewed', {
  110. organization: organization as Organization,
  111. group: event?.groupID,
  112. error_type: uniq(errorTypes),
  113. error_message: uniq(errorMessages),
  114. ...(platform && {platform}),
  115. });
  116. }
  117. async function fetchProguardMappingFiles(query: string): Promise<Array<DebugFile>> {
  118. try {
  119. const proguardMappingFiles = await api.requestPromise(
  120. `/projects/${orgSlug}/${projectSlug}/files/dsyms/`,
  121. {
  122. method: 'GET',
  123. query: {
  124. query,
  125. file_formats: 'proguard',
  126. },
  127. }
  128. );
  129. return proguardMappingFiles;
  130. } catch (error) {
  131. Sentry.captureException(error);
  132. // do nothing, the UI will not display extra error details
  133. return [];
  134. }
  135. }
  136. function isDataMinified(str: string | null) {
  137. if (!str) {
  138. return false;
  139. }
  140. return !![...str.matchAll(MINIFIED_DATA_JAVA_EVENT_REGEX_MATCH)].length;
  141. }
  142. function hasThreadOrExceptionMinifiedFrameData(
  143. definedEvent: Event,
  144. bestThread?: Thread
  145. ) {
  146. if (!bestThread) {
  147. const exceptionValues: Array<ExceptionValue> =
  148. definedEvent.entries?.find(e => e.type === EntryType.EXCEPTION)?.data?.values ??
  149. [];
  150. return !!exceptionValues.find(exceptionValue =>
  151. exceptionValue.stacktrace?.frames?.find(frame => isDataMinified(frame.module))
  152. );
  153. }
  154. const threadExceptionValues = getThreadException(definedEvent, bestThread)?.values;
  155. return !!(threadExceptionValues
  156. ? threadExceptionValues.find(threadExceptionValue =>
  157. threadExceptionValue.stacktrace?.frames?.find(frame =>
  158. isDataMinified(frame.module)
  159. )
  160. )
  161. : bestThread?.stacktrace?.frames?.find(frame => isDataMinified(frame.module)));
  162. }
  163. async function checkProGuardError() {
  164. if (!event || event.platform !== 'java') {
  165. setIsLoading(false);
  166. return;
  167. }
  168. const hasEventErrorsProGuardMissingMapping = event.errors?.find(
  169. error => error.type === 'proguard_missing_mapping'
  170. );
  171. if (hasEventErrorsProGuardMissingMapping) {
  172. setIsLoading(false);
  173. return;
  174. }
  175. const newProGuardErrors: ProGuardErrors = [];
  176. const debugImages = event.entries?.find(e => e.type === EntryType.DEBUGMETA)?.data
  177. .images as undefined | Array<Image>;
  178. // When debugImages contains a 'proguard' entry, it must always be only one entry
  179. const proGuardImage = debugImages?.find(
  180. debugImage => debugImage?.type === 'proguard'
  181. );
  182. const proGuardImageUuid = proGuardImage?.uuid;
  183. // If an entry is of type 'proguard' and has 'uuid',
  184. // it means that the Sentry Gradle plugin has been executed,
  185. // otherwise the proguard id wouldn't be in the event.
  186. // But maybe it failed to upload the mappings file
  187. if (defined(proGuardImageUuid)) {
  188. if (isShare) {
  189. setIsLoading(false);
  190. return;
  191. }
  192. const proguardMappingFiles = await fetchProguardMappingFiles(proGuardImageUuid);
  193. if (!proguardMappingFiles.length) {
  194. newProGuardErrors.push({
  195. type: 'proguard_missing_mapping',
  196. message: projectProcessingIssuesMessages.proguard_missing_mapping,
  197. data: {mapping_uuid: proGuardImageUuid},
  198. });
  199. }
  200. setProGuardErrors(newProGuardErrors);
  201. setIsLoading(false);
  202. return;
  203. }
  204. if (proGuardImage) {
  205. Sentry.withScope(function (s) {
  206. s.setLevel('warning');
  207. if (event.sdk) {
  208. s.setTag('offending.event.sdk.name', event.sdk.name);
  209. s.setTag('offending.event.sdk.version', event.sdk.version);
  210. }
  211. Sentry.captureMessage('Event contains proguard image but not uuid');
  212. });
  213. }
  214. const threads: Array<Thread> =
  215. event.entries?.find(e => e.type === EntryType.THREADS)?.data?.values ?? [];
  216. const bestThread = findBestThread(threads);
  217. const hasThreadOrExceptionMinifiedData = hasThreadOrExceptionMinifiedFrameData(
  218. event,
  219. bestThread
  220. );
  221. if (hasThreadOrExceptionMinifiedData) {
  222. newProGuardErrors.push({
  223. type: 'proguard_potentially_misconfigured_plugin',
  224. message: tct(
  225. 'Some frames appear to be minified. Did you configure the [plugin]?',
  226. {
  227. plugin: (
  228. <ExternalLink href="https://docs.sentry.io/platforms/android/proguard/#gradle">
  229. Sentry Gradle Plugin
  230. </ExternalLink>
  231. ),
  232. }
  233. ),
  234. });
  235. }
  236. setProGuardErrors(newProGuardErrors);
  237. setIsLoading(false);
  238. }
  239. async function fetchAttachments() {
  240. if (!event || isShare || !hasEventAttachmentsFeature) {
  241. return;
  242. }
  243. try {
  244. const response = await api.requestPromise(
  245. `/projects/${orgSlug}/${projectSlug}/events/${event.id}/attachments/`
  246. );
  247. setAttachments(response);
  248. } catch (error) {
  249. Sentry.captureException(error);
  250. addErrorMessage('An error occurred while fetching attachments');
  251. }
  252. }
  253. function renderEntries(definedEvent: Event) {
  254. const entries = definedEvent.entries;
  255. if (!Array.isArray(entries)) {
  256. return null;
  257. }
  258. return (entries as Array<Entry>).map((entry, entryIdx) => (
  259. <ErrorBoundary
  260. key={`entry-${entryIdx}`}
  261. customComponent={
  262. <EventDataSection type={entry.type} title={entry.type}>
  263. <p>{t('There was an error rendering this data.')}</p>
  264. </EventDataSection>
  265. }
  266. >
  267. <EventEntry
  268. projectSlug={projectSlug}
  269. group={group}
  270. organization={organization}
  271. event={definedEvent}
  272. entry={entry}
  273. route={route}
  274. router={router}
  275. />
  276. </ErrorBoundary>
  277. ));
  278. }
  279. async function handleDeleteAttachment(attachmentId: IssueAttachment['id']) {
  280. if (!event) {
  281. return;
  282. }
  283. try {
  284. await api.requestPromise(
  285. `/projects/${orgSlug}/${projectSlug}/events/${event.id}/attachments/${attachmentId}/`,
  286. {
  287. method: 'DELETE',
  288. }
  289. );
  290. setAttachments(attachments.filter(attachment => attachment.id !== attachmentId));
  291. } catch (error) {
  292. Sentry.captureException(error);
  293. addErrorMessage('An error occurred while deleting the attachment');
  294. }
  295. }
  296. if (!event) {
  297. return (
  298. <LatestEventNotAvailable>
  299. <h3>{t('Latest Event Not Available')}</h3>
  300. </LatestEventNotAvailable>
  301. );
  302. }
  303. const hasMobileScreenshotsFeature = orgFeatures.includes('mobile-screenshots');
  304. const hasContext = !objectIsEmpty(event.user ?? {}) || !objectIsEmpty(event.contexts);
  305. const hasErrors = !objectIsEmpty(event.errors) || !!proGuardErrors.length;
  306. return (
  307. <div className={className} data-test-id={`event-entries-loading-${isLoading}`}>
  308. {hasErrors && !isLoading && (
  309. <EventErrors
  310. event={event}
  311. orgSlug={orgSlug}
  312. projectSlug={projectSlug}
  313. proGuardErrors={proGuardErrors}
  314. />
  315. )}
  316. {!isShare &&
  317. isNotSharedOrganization(organization) &&
  318. (showExampleCommit ? (
  319. <EventCauseEmpty
  320. event={event}
  321. organization={organization}
  322. project={project}
  323. />
  324. ) : (
  325. <EventCause project={project} event={event} group={group} />
  326. ))}
  327. {event.userReport && group && (
  328. <StyledEventUserFeedback
  329. report={event.userReport}
  330. orgId={orgSlug}
  331. issueId={group.id}
  332. includeBorder={!hasErrors}
  333. />
  334. )}
  335. {showTagSummary &&
  336. (hasMobileScreenshotsFeature ? (
  337. <EventTagsAndScreenshot
  338. event={event}
  339. organization={organization as Organization}
  340. projectId={projectSlug}
  341. location={location}
  342. isShare={isShare}
  343. hasContext={hasContext}
  344. attachments={attachments}
  345. onDeleteScreenshot={handleDeleteAttachment}
  346. />
  347. ) : (
  348. (!!(event.tags ?? []).length || hasContext) && (
  349. <StyledEventDataSection title={t('Tags')} type="tags">
  350. {hasContext && <EventContextSummary event={event} />}
  351. <EventTags
  352. event={event}
  353. organization={organization as Organization}
  354. projectId={projectSlug}
  355. location={location}
  356. />
  357. </StyledEventDataSection>
  358. )
  359. ))}
  360. {renderEntries(event)}
  361. {hasContext && <EventContexts group={group} event={event} />}
  362. {event && !objectIsEmpty(event.context) && <EventExtraData event={event} />}
  363. {event && !objectIsEmpty(event.packages) && <EventPackageData event={event} />}
  364. {event && !objectIsEmpty(event.device) && <EventDevice event={event} />}
  365. {!isShare && hasEventAttachmentsFeature && (
  366. <EventAttachments
  367. event={event}
  368. orgId={orgSlug}
  369. projectId={projectSlug}
  370. location={location}
  371. attachments={attachments}
  372. onDeleteAttachment={handleDeleteAttachment}
  373. />
  374. )}
  375. {event.sdk && !objectIsEmpty(event.sdk) && (
  376. <EventSdk sdk={event.sdk} meta={event._meta?.sdk} />
  377. )}
  378. {!isShare && event?.sdkUpdates && event.sdkUpdates.length > 0 && (
  379. <EventSdkUpdates event={{sdkUpdates: event.sdkUpdates, ...event}} />
  380. )}
  381. {!isShare && event.groupID && (
  382. <EventGroupingInfo
  383. projectId={projectSlug}
  384. event={event}
  385. showGroupingConfig={orgFeatures.includes('set-grouping-config')}
  386. />
  387. )}
  388. {!isShare && (
  389. <MiniReplayView
  390. event={event}
  391. orgFeatures={orgFeatures}
  392. orgSlug={orgSlug}
  393. projectSlug={projectSlug}
  394. replayId={replayId}
  395. />
  396. )}
  397. </div>
  398. );
  399. }
  400. );
  401. type MiniReplayViewProps = {
  402. event: Event;
  403. orgFeatures: string[];
  404. orgSlug: string;
  405. projectSlug: string;
  406. replayId: undefined | string;
  407. };
  408. function MiniReplayView({
  409. event,
  410. orgFeatures,
  411. orgSlug,
  412. projectSlug,
  413. replayId,
  414. }: MiniReplayViewProps) {
  415. const hasEventAttachmentsFeature = orgFeatures.includes('event-attachments');
  416. const hasSessionReplayFeature = orgFeatures.includes('session-replay-ui');
  417. if (replayId && hasSessionReplayFeature) {
  418. return (
  419. <EventReplay replayId={replayId} orgSlug={orgSlug} projectSlug={projectSlug} />
  420. );
  421. }
  422. if (hasEventAttachmentsFeature) {
  423. return (
  424. <RRWebIntegration
  425. event={event}
  426. orgId={orgSlug}
  427. projectId={projectSlug}
  428. renderer={children => (
  429. <StyledReplayEventDataSection type="context-replay" title={t('Replay')}>
  430. {children}
  431. </StyledReplayEventDataSection>
  432. )}
  433. />
  434. );
  435. }
  436. return null;
  437. }
  438. const StyledEventDataSection = styled(EventDataSection)`
  439. /* Hiding the top border because of the event section appears at this breakpoint */
  440. @media (max-width: 767px) {
  441. &:first-of-type {
  442. border-top: none;
  443. }
  444. }
  445. `;
  446. const LatestEventNotAvailable = styled('div')`
  447. padding: ${space(2)} ${space(4)};
  448. `;
  449. const ErrorContainer = styled('div')`
  450. /*
  451. Remove border on adjacent context summary box.
  452. Once that component uses emotion this will be harder.
  453. */
  454. & + .context-summary {
  455. border-top: none;
  456. }
  457. `;
  458. const BorderlessEventEntries = styled(EventEntries)`
  459. & ${/* sc-selector */ DataSection} {
  460. margin-left: 0 !important;
  461. margin-right: 0 !important;
  462. padding: ${space(3)} 0 0 0;
  463. }
  464. & ${/* sc-selector */ DataSection}:first-child {
  465. padding-top: 0;
  466. border-top: 0;
  467. }
  468. & ${/* sc-selector */ ErrorContainer} {
  469. margin-bottom: ${space(2)};
  470. }
  471. `;
  472. type StyledEventUserFeedbackProps = {
  473. includeBorder: boolean;
  474. };
  475. const StyledEventUserFeedback = styled(EventUserFeedback)<StyledEventUserFeedbackProps>`
  476. border-radius: 0;
  477. box-shadow: none;
  478. padding: ${space(3)} ${space(4)} 0 40px;
  479. border: 0;
  480. ${p => (p.includeBorder ? `border-top: 1px solid ${p.theme.innerBorder};` : '')}
  481. margin: 0;
  482. `;
  483. const StyledReplayEventDataSection = styled(EventDataSection)`
  484. overflow: hidden;
  485. margin-bottom: ${space(3)};
  486. `;
  487. // TODO(ts): any required due to our use of SharedViewOrganization
  488. export default withOrganization<any>(withApi(EventEntries));
  489. export {BorderlessEventEntries};