traceViewDetailPanel.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. import {createRef, Fragment, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import omit from 'lodash/omit';
  5. import Alert from 'sentry/components/alert';
  6. import {Button} from 'sentry/components/button';
  7. import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton';
  8. import DateTime from 'sentry/components/dateTime';
  9. import {Chunk} from 'sentry/components/events/contexts/chunk';
  10. import {EventAttachments} from 'sentry/components/events/eventAttachments';
  11. import {
  12. isNotMarkMeasurement,
  13. isNotPerformanceScoreMeasurement,
  14. TraceEventCustomPerformanceMetric,
  15. } from 'sentry/components/events/eventCustomPerformanceMetrics';
  16. import {Entries} from 'sentry/components/events/eventEntries';
  17. import {EventEvidence} from 'sentry/components/events/eventEvidence';
  18. import {EventExtraData} from 'sentry/components/events/eventExtraData';
  19. import {EventSdk} from 'sentry/components/events/eventSdk';
  20. import {EventViewHierarchy} from 'sentry/components/events/eventViewHierarchy';
  21. import {Breadcrumbs} from 'sentry/components/events/interfaces/breadcrumbs';
  22. import NewTraceDetailsSpanDetail, {
  23. SpanDetailContainer,
  24. SpanDetailProps,
  25. SpanDetails,
  26. } from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanDetails';
  27. import {
  28. getFormattedTimeRangeWithLeadingAndTrailingZero,
  29. getSpanOperation,
  30. } from 'sentry/components/events/interfaces/spans/utils';
  31. import {generateStats} from 'sentry/components/events/opsBreakdown';
  32. import {EventRRWebIntegration} from 'sentry/components/events/rrwebIntegration';
  33. import {DataSection} from 'sentry/components/events/styles';
  34. import FileSize from 'sentry/components/fileSize';
  35. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  36. import Link from 'sentry/components/links/link';
  37. import LoadingIndicator from 'sentry/components/loadingIndicator';
  38. import {
  39. ErrorDot,
  40. ErrorLevel,
  41. ErrorMessageContent,
  42. ErrorMessageTitle,
  43. ErrorTitle,
  44. } from 'sentry/components/performance/waterfall/rowDetails';
  45. import PerformanceDuration from 'sentry/components/performanceDuration';
  46. import QuestionTooltip from 'sentry/components/questionTooltip';
  47. import {generateIssueEventTarget} from 'sentry/components/quickTrace/utils';
  48. import {Tooltip} from 'sentry/components/tooltip';
  49. import {PAGE_URL_PARAM} from 'sentry/constants/pageFilters';
  50. import {IconChevron, IconOpen} from 'sentry/icons';
  51. import {t, tn} from 'sentry/locale';
  52. import {space} from 'sentry/styles/space';
  53. import {EntryBreadcrumbs, EntryType, EventTransaction, Organization} from 'sentry/types';
  54. import {objectIsEmpty} from 'sentry/utils';
  55. import {trackAnalytics} from 'sentry/utils/analytics';
  56. import getDynamicText from 'sentry/utils/getDynamicText';
  57. import {PageErrorProvider} from 'sentry/utils/performance/contexts/pageError';
  58. import {WEB_VITAL_DETAILS} from 'sentry/utils/performance/vitals/constants';
  59. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  60. import {useLocation} from 'sentry/utils/useLocation';
  61. import useOrganization from 'sentry/utils/useOrganization';
  62. import useProjects from 'sentry/utils/useProjects';
  63. import {isCustomMeasurement} from 'sentry/views/dashboards/utils';
  64. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  65. import {ProfileContext, ProfilesProvider} from 'sentry/views/profiling/profilesProvider';
  66. import DetailPanel from 'sentry/views/starfish/components/detailPanel';
  67. import {transactionSummaryRouteWithQuery} from '../transactionSummary/utils';
  68. import {EventDetail} from './newTraceDetailsContent';
  69. import {Row, Tags} from './styles';
  70. type DetailPanelProps = {
  71. detail: EventDetail | SpanDetailProps | undefined;
  72. onClose: () => void;
  73. };
  74. type EventDetailProps = {
  75. detail: EventDetail;
  76. location: Location;
  77. organization: Organization;
  78. };
  79. function OpsBreakdown({event}: {event: EventTransaction}) {
  80. const [showingAll, setShowingAll] = useState(false);
  81. const breakdown = event && generateStats(event, {type: 'no_filter'});
  82. if (!breakdown) {
  83. return null;
  84. }
  85. const renderText = showingAll ? t('Show less') : t('Show more') + '...';
  86. return (
  87. breakdown && (
  88. <Row
  89. title={
  90. <FlexBox style={{gap: '5px'}}>
  91. {t('Ops Breakdown')}
  92. <QuestionTooltip
  93. title={t('Applicable to the children of this event only')}
  94. size="xs"
  95. />
  96. </FlexBox>
  97. }
  98. >
  99. <div style={{display: 'flex', flexDirection: 'column', gap: space(0.25)}}>
  100. {breakdown.slice(0, showingAll ? breakdown.length : 5).map(currOp => {
  101. const {name, percentage, totalInterval} = currOp;
  102. const operationName = typeof name === 'string' ? name : t('Other');
  103. const pctLabel = isFinite(percentage) ? Math.round(percentage * 100) : '∞';
  104. return (
  105. <div key={operationName}>
  106. {operationName}:{' '}
  107. <PerformanceDuration seconds={totalInterval} abbreviation /> ({pctLabel}%)
  108. </div>
  109. );
  110. })}
  111. {breakdown.length > 5 && (
  112. <a onClick={() => setShowingAll(prev => !prev)}>{renderText}</a>
  113. )}
  114. </div>
  115. </Row>
  116. )
  117. );
  118. }
  119. function BreadCrumbsSection({
  120. event,
  121. organization,
  122. }: {
  123. event: EventTransaction;
  124. organization: Organization;
  125. }) {
  126. const [showBreadCrumbs, setShowBreadCrumbs] = useState(false);
  127. const breadCrumbsContainerRef = createRef<HTMLDivElement>();
  128. useEffect(() => {
  129. setTimeout(() => {
  130. if (showBreadCrumbs) {
  131. breadCrumbsContainerRef.current?.scrollIntoView({
  132. behavior: 'smooth',
  133. block: 'end',
  134. });
  135. }
  136. }, 100);
  137. }, [showBreadCrumbs, breadCrumbsContainerRef]);
  138. const matchingEntry: EntryBreadcrumbs | undefined = event?.entries.find(
  139. (entry): entry is EntryBreadcrumbs => entry.type === EntryType.BREADCRUMBS
  140. );
  141. if (!matchingEntry) {
  142. return null;
  143. }
  144. const renderText = showBreadCrumbs ? t('Hide Breadcrumbs') : t('Show Breadcrumbs');
  145. const chevron = <IconChevron size="xs" direction={showBreadCrumbs ? 'up' : 'down'} />;
  146. return (
  147. <Fragment>
  148. <a
  149. style={{display: 'flex', alignItems: 'center', gap: space(0.5)}}
  150. onClick={() => {
  151. setShowBreadCrumbs(prev => !prev);
  152. }}
  153. >
  154. {renderText} {chevron}
  155. </a>
  156. <div ref={breadCrumbsContainerRef}>
  157. {showBreadCrumbs && (
  158. <Breadcrumbs
  159. hideTitle
  160. data={matchingEntry.data}
  161. event={event}
  162. organization={organization}
  163. />
  164. )}
  165. </div>
  166. </Fragment>
  167. );
  168. }
  169. function EventDetails({detail, organization, location}: EventDetailProps) {
  170. const {projects} = useProjects();
  171. if (!detail.event) {
  172. return <LoadingIndicator />;
  173. }
  174. const {user, contexts, projectSlug} = detail.event;
  175. const {feedback} = contexts ?? {};
  176. const eventJsonUrl = `/api/0/projects/${organization.slug}/${detail.traceFullDetailedEvent.project_slug}/events/${detail.traceFullDetailedEvent.event_id}/json/`;
  177. const project = projects.find(proj => proj.slug === detail.event?.projectSlug);
  178. const {errors, performance_issues} = detail.traceFullDetailedEvent;
  179. const hasIssues = errors.length + performance_issues.length > 0;
  180. const startTimestamp = Math.min(
  181. detail.traceFullDetailedEvent.start_timestamp,
  182. detail.traceFullDetailedEvent.timestamp
  183. );
  184. const endTimestamp = Math.max(
  185. detail.traceFullDetailedEvent.start_timestamp,
  186. detail.traceFullDetailedEvent.timestamp
  187. );
  188. const {start: startTimeWithLeadingZero, end: endTimeWithLeadingZero} =
  189. getFormattedTimeRangeWithLeadingAndTrailingZero(startTimestamp, endTimestamp);
  190. const duration = (endTimestamp - startTimestamp) * 1000;
  191. const durationString = `${Number(duration.toFixed(3)).toLocaleString()}ms`;
  192. const measurementNames = Object.keys(detail.traceFullDetailedEvent.measurements ?? {})
  193. .filter(name => isCustomMeasurement(`measurements.${name}`))
  194. .filter(isNotMarkMeasurement)
  195. .filter(isNotPerformanceScoreMeasurement)
  196. .sort();
  197. const renderMeasurements = () => {
  198. if(!detail.event){
  199. return null;
  200. }
  201. const {measurements} = detail.event;
  202. const measurementKeys = Object.keys(measurements ?? {})
  203. .filter(name => Boolean(WEB_VITAL_DETAILS[`measurements.${name}`]))
  204. .sort();
  205. if (!measurements || measurementKeys.length <= 0) {
  206. return null;
  207. }
  208. return (
  209. <Fragment>
  210. {measurementKeys.map(measurement => (
  211. <Row
  212. key={measurement}
  213. title={WEB_VITAL_DETAILS[`measurements.${measurement}`]?.name}
  214. >
  215. <PerformanceDuration milliseconds={Number(measurements[measurement].value.toFixed(3))} abbreviation />
  216. </Row>
  217. ))}
  218. </Fragment>
  219. );
  220. };
  221. const renderGoToProfileButton = () => {
  222. if (!detail.traceFullDetailedEvent.profile_id) {
  223. return null;
  224. }
  225. const target = generateProfileFlamechartRoute({
  226. orgSlug: organization.slug,
  227. projectSlug: detail.traceFullDetailedEvent.project_slug,
  228. profileId: detail.traceFullDetailedEvent.profile_id,
  229. });
  230. function handleOnClick() {
  231. trackAnalytics('profiling_views.go_to_flamegraph', {
  232. organization,
  233. source: 'performance.trace_view',
  234. });
  235. }
  236. return (
  237. <StyledButton size="xs" to={target} onClick={handleOnClick}>
  238. {t('View Profile')}
  239. </StyledButton>
  240. );
  241. };
  242. return (
  243. <Wrapper>
  244. <Actions>
  245. <Button
  246. size="sm"
  247. icon={<IconOpen />}
  248. href={eventJsonUrl}
  249. external
  250. onClick={() =>
  251. trackAnalytics('performance_views.event_details.json_button_click', {
  252. organization,
  253. })
  254. }
  255. >
  256. {t('JSON')} (<FileSize bytes={detail.event?.size} />)
  257. </Button>
  258. </Actions>
  259. <Title>
  260. <Tooltip title={detail.traceFullDetailedEvent.project_slug}>
  261. <ProjectBadge
  262. project={
  263. project ? project : {slug: detail.traceFullDetailedEvent.project_slug}
  264. }
  265. avatarSize={50}
  266. hideName
  267. />
  268. </Tooltip>
  269. <div>
  270. <div>{t('Event')}</div>
  271. <TransactionOp>
  272. {' '}
  273. {detail.traceFullDetailedEvent['transaction.op']}
  274. </TransactionOp>
  275. </div>
  276. </Title>
  277. {hasIssues && (
  278. <Alert
  279. system
  280. defaultExpanded
  281. type="error"
  282. expand={[
  283. ...detail.traceFullDetailedEvent.errors,
  284. ...detail.traceFullDetailedEvent.performance_issues,
  285. ].map(error => (
  286. <ErrorMessageContent key={error.event_id}>
  287. <ErrorDot level={error.level} />
  288. <ErrorLevel>{error.level}</ErrorLevel>
  289. <ErrorTitle>
  290. <Link to={generateIssueEventTarget(error, organization)}>
  291. {error.title}
  292. </Link>
  293. </ErrorTitle>
  294. </ErrorMessageContent>
  295. ))}
  296. >
  297. <ErrorMessageTitle>
  298. {tn(
  299. '%s issue occurred in this transaction.',
  300. '%s issues occurred in this transaction.',
  301. detail.traceFullDetailedEvent.errors.length +
  302. detail.traceFullDetailedEvent.performance_issues.length
  303. )}
  304. </ErrorMessageTitle>
  305. </Alert>
  306. )}
  307. <StyledTable className="table key-value">
  308. <tbody>
  309. <Row title={<TransactionIdTitle>{t('Event ID')}</TransactionIdTitle>}>
  310. {detail.traceFullDetailedEvent.event_id}
  311. <CopyToClipboardButton
  312. borderless
  313. size="zero"
  314. iconSize="xs"
  315. text={`${window.location.href.replace(window.location.hash, '')}#txn-${
  316. detail.traceFullDetailedEvent.event_id
  317. }`}
  318. />
  319. </Row>
  320. <Row title={t('Description')}>
  321. <Link
  322. to={transactionSummaryRouteWithQuery({
  323. orgSlug: organization.slug,
  324. transaction: detail.traceFullDetailedEvent.transaction,
  325. query: omit(location.query, Object.values(PAGE_URL_PARAM)),
  326. projectID: String(detail.traceFullDetailedEvent.project_id),
  327. })}
  328. >
  329. {detail.traceFullDetailedEvent.transaction}
  330. </Link>
  331. </Row>
  332. {detail.traceFullDetailedEvent.profile_id && (
  333. <Row title="Profile ID" extra={renderGoToProfileButton()}>
  334. {detail.traceFullDetailedEvent.profile_id}
  335. </Row>
  336. )}
  337. <Row title="Duration">{durationString}</Row>
  338. <Row title="Date Range">
  339. {getDynamicText({
  340. fixed: 'Mar 19, 2021 11:06:27 AM UTC',
  341. value: (
  342. <Fragment>
  343. <DateTime date={startTimestamp * 1000} />
  344. {` (${startTimeWithLeadingZero})`}
  345. </Fragment>
  346. ),
  347. })}
  348. <br />
  349. {getDynamicText({
  350. fixed: 'Mar 19, 2021 11:06:28 AM UTC',
  351. value: (
  352. <Fragment>
  353. <DateTime date={endTimestamp * 1000} />
  354. {` (${endTimeWithLeadingZero})`}
  355. </Fragment>
  356. ),
  357. })}
  358. </Row>
  359. <OpsBreakdown event={detail.event} />
  360. {renderMeasurements()}
  361. <Tags
  362. enableHiding
  363. location={location}
  364. organization={organization}
  365. transaction={detail.traceFullDetailedEvent}
  366. />
  367. {measurementNames.length > 0 && (
  368. <tr>
  369. <td className="key">{t('Custom Metrics')}</td>
  370. <td className="value">
  371. <Measurements>
  372. {measurementNames.map(name => {
  373. return (
  374. detail.event && (
  375. <TraceEventCustomPerformanceMetric
  376. key={name}
  377. event={detail.event}
  378. name={name}
  379. location={location}
  380. organization={organization}
  381. source={undefined}
  382. isHomepage={false}
  383. />
  384. )
  385. );
  386. })}
  387. </Measurements>
  388. </td>
  389. </tr>
  390. )}
  391. </tbody>
  392. </StyledTable>
  393. {project && <EventEvidence event={detail.event} project={project} />}
  394. {projectSlug && (
  395. <Entries
  396. definedEvent={detail.event}
  397. projectSlug={projectSlug}
  398. group={undefined}
  399. organization={organization}
  400. isShare={false}
  401. hideBeforeReplayEntries
  402. hideBreadCrumbs
  403. />
  404. )}
  405. {!objectIsEmpty(feedback) && (
  406. <Chunk
  407. key="feedback"
  408. type="feedback"
  409. alias="feedback"
  410. group={undefined}
  411. event={detail.event}
  412. value={feedback}
  413. />
  414. )}
  415. {user && !objectIsEmpty(user) && (
  416. <Chunk
  417. key="user"
  418. type="user"
  419. alias="user"
  420. group={undefined}
  421. event={detail.event}
  422. value={user}
  423. />
  424. )}
  425. <EventExtraData event={detail.event} />
  426. <EventSdk sdk={detail.event.sdk} meta={detail.event._meta?.sdk} />
  427. <BreadCrumbsSection event={detail.event} organization={organization} />
  428. {projectSlug && <EventAttachments event={detail.event} projectSlug={projectSlug} />}
  429. {project && <EventViewHierarchy event={detail.event} project={project} />}
  430. {projectSlug && (
  431. <EventRRWebIntegration
  432. event={detail.event}
  433. orgId={organization.slug}
  434. projectSlug={projectSlug}
  435. />
  436. )}
  437. </Wrapper>
  438. );
  439. }
  440. function SpanDetailsBody({
  441. detail,
  442. organization,
  443. }: {
  444. detail: SpanDetailProps;
  445. organization: Organization;
  446. }) {
  447. const {projects} = useProjects();
  448. const project = projects.find(proj => proj.slug === detail.event?.projectSlug);
  449. const profileId = detail?.event?.contexts?.profile?.profile_id ?? null;
  450. return (
  451. <Wrapper>
  452. <Title>
  453. <Tooltip title={detail.event.projectSlug}>
  454. <ProjectBadge
  455. project={project ? project : {slug: detail.event.projectSlug || ''}}
  456. avatarSize={50}
  457. hideName
  458. />
  459. </Tooltip>
  460. <div>
  461. <div>{t('Span')}</div>
  462. <TransactionOp> {getSpanOperation(detail.span)}</TransactionOp>
  463. </div>
  464. </Title>
  465. {detail.event.projectSlug && (
  466. <ProfilesProvider
  467. orgSlug={organization.slug}
  468. projectSlug={detail.event.projectSlug}
  469. profileId={profileId || ''}
  470. >
  471. <ProfileContext.Consumer>
  472. {profiles => (
  473. <ProfileGroupProvider
  474. type="flamechart"
  475. input={profiles?.type === 'resolved' ? profiles.data : null}
  476. traceID={profileId || ''}
  477. >
  478. <NewTraceDetailsSpanDetail {...detail} />
  479. </ProfileGroupProvider>
  480. )}
  481. </ProfileContext.Consumer>
  482. </ProfilesProvider>
  483. )}
  484. </Wrapper>
  485. );
  486. }
  487. export function isEventDetail(
  488. detail: EventDetail | SpanDetailProps
  489. ): detail is EventDetail {
  490. return !('span' in detail);
  491. }
  492. function TraceViewDetailPanel({detail, onClose}: DetailPanelProps) {
  493. const organization = useOrganization();
  494. const location = useLocation();
  495. return (
  496. <PageErrorProvider>
  497. <DetailPanel detailKey={detail ? 'open' : undefined} onClose={onClose}>
  498. {detail &&
  499. (isEventDetail(detail) ? (
  500. <EventDetails
  501. location={location}
  502. organization={organization}
  503. detail={detail}
  504. />
  505. ) : (
  506. <SpanDetailsBody organization={organization} detail={detail} />
  507. ))}
  508. </DetailPanel>
  509. </PageErrorProvider>
  510. );
  511. }
  512. const Wrapper = styled('div')`
  513. display: flex;
  514. flex-direction: column;
  515. gap: ${space(2)};
  516. ${DataSection} {
  517. padding: 0;
  518. }
  519. ${SpanDetails} {
  520. padding: 0;
  521. }
  522. ${SpanDetailContainer} {
  523. border-bottom: none;
  524. }
  525. `;
  526. const FlexBox = styled('div')`
  527. display: flex;
  528. align-items: center;
  529. `;
  530. const Actions = styled('div')`
  531. display: flex;
  532. align-items: center;
  533. justify-content: flex-end;
  534. `;
  535. const Title = styled(FlexBox)`
  536. gap: ${space(2)};
  537. `;
  538. const TransactionOp = styled('div')`
  539. font-size: 25px;
  540. font-weight: bold;
  541. max-width: 600px;
  542. ${p => p.theme.overflowEllipsis}
  543. `;
  544. const TransactionIdTitle = styled('a')`
  545. display: flex;
  546. color: ${p => p.theme.textColor};
  547. :hover {
  548. color: ${p => p.theme.textColor};
  549. }
  550. `;
  551. const Measurements = styled('div')`
  552. display: flex;
  553. flex-wrap: wrap;
  554. gap: ${space(1)};
  555. padding-top: 10px;
  556. `;
  557. const StyledButton = styled(Button)`
  558. position: absolute;
  559. top: ${space(0.75)};
  560. right: ${space(0.5)};
  561. `;
  562. const StyledTable = styled('table')`
  563. margin-bottom: 0 !important;
  564. `;
  565. export default TraceViewDetailPanel;