traceViewDetailPanel.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. import {Fragment, useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import omit from 'lodash/omit';
  5. import {LinkButton} from 'sentry/components/button';
  6. import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton';
  7. import {Alert} from 'sentry/components/core/alert';
  8. import {DateTime} from 'sentry/components/dateTime';
  9. import {EventAttachments} from 'sentry/components/events/eventAttachments';
  10. import {
  11. isNotMarkMeasurement,
  12. isNotPerformanceScoreMeasurement,
  13. TraceEventCustomPerformanceMetric,
  14. } from 'sentry/components/events/eventCustomPerformanceMetrics';
  15. import {Entries} from 'sentry/components/events/eventEntries';
  16. import {EventEvidence} from 'sentry/components/events/eventEvidence';
  17. import {EventExtraData} from 'sentry/components/events/eventExtraData';
  18. import {EventSdk} from 'sentry/components/events/eventSdk';
  19. import {EventViewHierarchy} from 'sentry/components/events/eventViewHierarchy';
  20. import {Breadcrumbs} from 'sentry/components/events/interfaces/breadcrumbs';
  21. import type {SpanDetailProps} from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanDetails';
  22. import NewTraceDetailsSpanDetail, {
  23. SpanDetailContainer,
  24. SpanDetails,
  25. } from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanDetails';
  26. import {
  27. getFormattedTimeRangeWithLeadingAndTrailingZero,
  28. getSpanOperation,
  29. } from 'sentry/components/events/interfaces/spans/utils';
  30. import {generateStats} from 'sentry/components/events/opsBreakdown';
  31. import {EventRRWebIntegration} from 'sentry/components/events/rrwebIntegration';
  32. import {DataSection} from 'sentry/components/events/styles';
  33. import FileSize from 'sentry/components/fileSize';
  34. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  35. import Link from 'sentry/components/links/link';
  36. import LoadingIndicator from 'sentry/components/loadingIndicator';
  37. import {
  38. ErrorDot,
  39. ErrorLevel,
  40. ErrorMessageContent,
  41. ErrorMessageTitle,
  42. ErrorTitle,
  43. } from 'sentry/components/performance/waterfall/rowDetails';
  44. import PerformanceDuration from 'sentry/components/performanceDuration';
  45. import QuestionTooltip from 'sentry/components/questionTooltip';
  46. import {generateIssueEventTarget} from 'sentry/components/quickTrace/utils';
  47. import {Tooltip} from 'sentry/components/tooltip';
  48. import {PAGE_URL_PARAM} from 'sentry/constants/pageFilters';
  49. import {IconChevron, IconOpen} from 'sentry/icons';
  50. import {t, tn} from 'sentry/locale';
  51. import {space} from 'sentry/styles/space';
  52. import type {EntryBreadcrumbs, EventTransaction} from 'sentry/types/event';
  53. import {EntryType} from 'sentry/types/event';
  54. import type {Organization} from 'sentry/types/organization';
  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 DetailPanel from 'sentry/views/insights/common/components/detailPanel';
  65. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  66. import {ProfileContext, ProfilesProvider} from 'sentry/views/profiling/profilesProvider';
  67. import {transactionSummaryRouteWithQuery} from '../transactionSummary/utils';
  68. import type {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 = useRef<HTMLDivElement>(null);
  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 {projectSlug} = detail.event;
  175. const eventJsonUrl = `/api/0/projects/${organization.slug}/${detail.traceFullDetailedEvent.project_slug}/events/${detail.traceFullDetailedEvent.event_id}/json/`;
  176. const project = projects.find(proj => proj.slug === detail.event?.projectSlug);
  177. const {errors, performance_issues} = detail.traceFullDetailedEvent;
  178. const hasIssues = errors.length + performance_issues.length > 0;
  179. const startTimestamp = Math.min(
  180. detail.traceFullDetailedEvent.start_timestamp,
  181. detail.traceFullDetailedEvent.timestamp
  182. );
  183. const endTimestamp = Math.max(
  184. detail.traceFullDetailedEvent.start_timestamp,
  185. detail.traceFullDetailedEvent.timestamp
  186. );
  187. const {start: startTimeWithLeadingZero, end: endTimeWithLeadingZero} =
  188. getFormattedTimeRangeWithLeadingAndTrailingZero(startTimestamp, endTimestamp);
  189. const duration = (endTimestamp - startTimestamp) * 1000;
  190. const durationString = `${Number(duration.toFixed(3)).toLocaleString()}ms`;
  191. const measurementNames = Object.keys(detail.traceFullDetailedEvent.measurements ?? {})
  192. .filter(name => isCustomMeasurement(`measurements.${name}`))
  193. .filter(isNotMarkMeasurement)
  194. .filter(isNotPerformanceScoreMeasurement)
  195. .sort();
  196. const renderMeasurements = () => {
  197. if (!detail.event) {
  198. return null;
  199. }
  200. const {measurements} = detail.event;
  201. const measurementKeys = Object.keys(measurements ?? {})
  202. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  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. organization,
  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. <LinkButton
  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. </LinkButton>
  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.Container>
  283. <Alert
  284. system
  285. defaultExpanded
  286. type="error"
  287. expand={[
  288. ...detail.traceFullDetailedEvent.errors,
  289. ...detail.traceFullDetailedEvent.performance_issues,
  290. ].map(error => (
  291. <ErrorMessageContent key={error.event_id}>
  292. <ErrorDot level={error.level} />
  293. <ErrorLevel>{error.level}</ErrorLevel>
  294. <ErrorTitle>
  295. <Link to={generateIssueEventTarget(error, organization)}>
  296. {error.title}
  297. </Link>
  298. </ErrorTitle>
  299. </ErrorMessageContent>
  300. ))}
  301. >
  302. <ErrorMessageTitle>
  303. {tn(
  304. '%s issue occurred in this transaction.',
  305. '%s issues occurred in this transaction.',
  306. detail.traceFullDetailedEvent.errors.length +
  307. detail.traceFullDetailedEvent.performance_issues.length
  308. )}
  309. </ErrorMessageTitle>
  310. </Alert>
  311. </Alert.Container>
  312. )}
  313. <StyledTable className="table key-value">
  314. <tbody>
  315. <Row title={<TransactionIdTitle>{t('Event ID')}</TransactionIdTitle>}>
  316. {detail.traceFullDetailedEvent.event_id}
  317. <CopyToClipboardButton
  318. borderless
  319. size="zero"
  320. iconSize="xs"
  321. text={`${window.location.href.replace(window.location.hash, '')}#txn-${
  322. detail.traceFullDetailedEvent.event_id
  323. }`}
  324. />
  325. </Row>
  326. <Row title={t('Description')}>
  327. <Link
  328. to={transactionSummaryRouteWithQuery({
  329. organization,
  330. transaction: detail.traceFullDetailedEvent.transaction,
  331. query: omit(location.query, Object.values(PAGE_URL_PARAM)),
  332. projectID: String(detail.traceFullDetailedEvent.project_id),
  333. })}
  334. >
  335. {detail.traceFullDetailedEvent.transaction}
  336. </Link>
  337. </Row>
  338. {detail.traceFullDetailedEvent.profile_id && (
  339. <Row title="Profile ID" extra={renderGoToProfileButton()}>
  340. {detail.traceFullDetailedEvent.profile_id}
  341. </Row>
  342. )}
  343. <Row title="Duration">{durationString}</Row>
  344. <Row title="Date Range">
  345. {getDynamicText({
  346. fixed: 'Mar 19, 2021 11:06:27 AM UTC',
  347. value: (
  348. <Fragment>
  349. <DateTime date={startTimestamp * 1000} />
  350. {` (${startTimeWithLeadingZero})`}
  351. </Fragment>
  352. ),
  353. })}
  354. <br />
  355. {getDynamicText({
  356. fixed: 'Mar 19, 2021 11:06:28 AM UTC',
  357. value: (
  358. <Fragment>
  359. <DateTime date={endTimestamp * 1000} />
  360. {` (${endTimeWithLeadingZero})`}
  361. </Fragment>
  362. ),
  363. })}
  364. </Row>
  365. <OpsBreakdown event={detail.event} />
  366. {renderMeasurements()}
  367. <Tags
  368. enableHiding
  369. location={location}
  370. organization={organization}
  371. tags={detail.traceFullDetailedEvent.tags ?? []}
  372. event={detail.traceFullDetailedEvent}
  373. />
  374. {measurementNames.length > 0 && (
  375. <tr>
  376. <td className="key">{t('Measurements')}</td>
  377. <td className="value">
  378. <Measurements>
  379. {measurementNames.map(name => {
  380. return (
  381. detail.event && (
  382. <TraceEventCustomPerformanceMetric
  383. key={name}
  384. event={detail.event}
  385. name={name}
  386. location={location}
  387. organization={organization}
  388. source={undefined}
  389. isHomepage={false}
  390. />
  391. )
  392. );
  393. })}
  394. </Measurements>
  395. </td>
  396. </tr>
  397. )}
  398. </tbody>
  399. </StyledTable>
  400. {project && <EventEvidence event={detail.event} project={project} />}
  401. {projectSlug && (
  402. <Entries
  403. definedEvent={detail.event}
  404. projectSlug={projectSlug}
  405. group={undefined}
  406. organization={organization}
  407. isShare={false}
  408. hideBeforeReplayEntries
  409. hideBreadCrumbs
  410. />
  411. )}
  412. <EventExtraData event={detail.event} />
  413. <EventSdk sdk={detail.event.sdk} meta={detail.event._meta?.sdk} />
  414. <BreadCrumbsSection event={detail.event} organization={organization} />
  415. {project && (
  416. <EventAttachments event={detail.event} project={project} group={undefined} />
  417. )}
  418. {project && <EventViewHierarchy event={detail.event} project={project} />}
  419. {projectSlug && (
  420. <EventRRWebIntegration
  421. event={detail.event}
  422. orgId={organization.slug}
  423. projectSlug={projectSlug}
  424. />
  425. )}
  426. </Wrapper>
  427. );
  428. }
  429. function SpanDetailsBody({
  430. detail,
  431. organization,
  432. }: {
  433. detail: SpanDetailProps;
  434. organization: Organization;
  435. }) {
  436. const {projects} = useProjects();
  437. const project = projects.find(proj => proj.slug === detail.event?.projectSlug);
  438. const profileId = detail?.event?.contexts?.profile?.profile_id ?? null;
  439. return (
  440. <Wrapper>
  441. <Title>
  442. <Tooltip title={detail.event.projectSlug}>
  443. <ProjectBadge
  444. project={project ? project : {slug: detail.event.projectSlug || ''}}
  445. avatarSize={50}
  446. hideName
  447. />
  448. </Tooltip>
  449. <div>
  450. <div>{t('Span')}</div>
  451. <TransactionOp> {getSpanOperation(detail.node.value)}</TransactionOp>
  452. </div>
  453. </Title>
  454. {detail.event.projectSlug && (
  455. <ProfilesProvider
  456. orgSlug={organization.slug}
  457. projectSlug={detail.event.projectSlug}
  458. profileMeta={profileId || ''}
  459. >
  460. <ProfileContext.Consumer>
  461. {profiles => (
  462. <ProfileGroupProvider
  463. type="flamechart"
  464. input={profiles?.type === 'resolved' ? profiles.data : null}
  465. traceID={profileId || ''}
  466. >
  467. <NewTraceDetailsSpanDetail {...detail} />
  468. </ProfileGroupProvider>
  469. )}
  470. </ProfileContext.Consumer>
  471. </ProfilesProvider>
  472. )}
  473. </Wrapper>
  474. );
  475. }
  476. export function isEventDetail(
  477. detail: EventDetail | SpanDetailProps
  478. ): detail is EventDetail {
  479. return !('span' in detail);
  480. }
  481. function TraceViewDetailPanel({detail, onClose}: DetailPanelProps) {
  482. const organization = useOrganization();
  483. const location = useLocation();
  484. return (
  485. <PageAlertProvider>
  486. <DetailPanel
  487. detailKey={detail && detail.openPanel === 'open' ? 'open' : undefined}
  488. onClose={onClose}
  489. >
  490. {detail &&
  491. (isEventDetail(detail) ? (
  492. <EventDetails
  493. location={location}
  494. organization={organization}
  495. detail={detail}
  496. />
  497. ) : (
  498. <SpanDetailsBody organization={organization} detail={detail} />
  499. ))}
  500. </DetailPanel>
  501. </PageAlertProvider>
  502. );
  503. }
  504. const Wrapper = styled('div')`
  505. display: flex;
  506. flex-direction: column;
  507. gap: ${space(2)};
  508. ${DataSection} {
  509. padding: 0;
  510. }
  511. ${SpanDetails} {
  512. padding: 0;
  513. }
  514. ${SpanDetailContainer} {
  515. border-bottom: none;
  516. }
  517. `;
  518. const FlexBox = styled('div')`
  519. display: flex;
  520. align-items: center;
  521. `;
  522. const Actions = styled('div')`
  523. display: flex;
  524. align-items: center;
  525. justify-content: flex-end;
  526. `;
  527. const Title = styled(FlexBox)`
  528. gap: ${space(2)};
  529. `;
  530. const TransactionOp = styled('div')`
  531. font-size: 25px;
  532. font-weight: ${p => p.theme.fontWeightBold};
  533. max-width: 600px;
  534. ${p => p.theme.overflowEllipsis}
  535. `;
  536. const TransactionIdTitle = styled('a')`
  537. display: flex;
  538. color: ${p => p.theme.textColor};
  539. :hover {
  540. color: ${p => p.theme.textColor};
  541. }
  542. `;
  543. const Measurements = styled('div')`
  544. display: flex;
  545. flex-wrap: wrap;
  546. gap: ${space(1)};
  547. padding-top: 10px;
  548. `;
  549. const StyledButton = styled(LinkButton)`
  550. position: absolute;
  551. top: ${space(0.75)};
  552. right: ${space(0.5)};
  553. `;
  554. const StyledTable = styled('table')`
  555. margin-bottom: 0 !important;
  556. `;
  557. export default TraceViewDetailPanel;