traceViewDetailPanel.tsx 19 KB

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