eventEntries.tsx 17 KB

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