newTraceDetailsContent.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. import {Fragment, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import omit from 'lodash/omit';
  4. import {Alert} from 'sentry/components/alert';
  5. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import DiscoverButton from 'sentry/components/discoverButton';
  8. import EventVitals from 'sentry/components/events/eventVitals';
  9. import type {SpanDetailProps} from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanDetails';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import ExternalLink from 'sentry/components/links/externalLink';
  12. import Link from 'sentry/components/links/link';
  13. import LoadingError from 'sentry/components/loadingError';
  14. import LoadingIndicator from 'sentry/components/loadingIndicator';
  15. import {Tooltip} from 'sentry/components/tooltip';
  16. import {IconPlay} from 'sentry/icons';
  17. import {t, tct, tn} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import type {EventTransaction} from 'sentry/types/event';
  20. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  21. import type {Organization} from 'sentry/types/organization';
  22. import {generateQueryWithTag} from 'sentry/utils';
  23. import {trackAnalytics} from 'sentry/utils/analytics';
  24. import type EventView from 'sentry/utils/discover/eventView';
  25. import {formatTagKey} from 'sentry/utils/discover/fields';
  26. import type {QueryError} from 'sentry/utils/discover/genericDiscoverQuery';
  27. import getDuration from 'sentry/utils/duration/getDuration';
  28. import {getShortEventId} from 'sentry/utils/events';
  29. import type {
  30. TraceError,
  31. TraceFullDetailed,
  32. TraceMeta,
  33. } from 'sentry/utils/performance/quickTrace/types';
  34. import {WEB_VITAL_DETAILS} from 'sentry/utils/performance/vitals/constants';
  35. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  36. import {useApiQuery} from 'sentry/utils/queryClient';
  37. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  38. import {useNavigate} from 'sentry/utils/useNavigate';
  39. import Tags from 'sentry/views/discover/tags';
  40. import Breadcrumb from 'sentry/views/performance/breadcrumb';
  41. import type {TraceTree} from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  42. import {TraceShape} from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  43. import {MetaData} from 'sentry/views/performance/transactionDetails/styles';
  44. import {BrowserDisplay} from '../transactionDetails/eventMetas';
  45. import NewTraceView from './newTraceDetailsTraceView';
  46. import TraceNotFound from './traceNotFound';
  47. import TraceViewDetailPanel from './traceViewDetailPanel';
  48. import {getTraceInfo, hasTraceData, isRootTransaction} from './utils';
  49. type Props = Pick<RouteComponentProps<{traceSlug: string}, {}>, 'params' | 'location'> & {
  50. dateSelected: boolean;
  51. error: QueryError | null;
  52. isLoading: boolean;
  53. meta: TraceMeta | null;
  54. organization: Organization;
  55. traceEventView: EventView;
  56. traceSlug: string;
  57. traces: TraceTree.Transaction[] | null;
  58. handleLimitChange?: (newLimit: number) => void;
  59. orphanErrors?: TraceError[];
  60. };
  61. export type EventDetail = {
  62. event: EventTransaction | undefined;
  63. openPanel: string | undefined;
  64. traceFullDetailedEvent: TraceFullDetailed;
  65. };
  66. function NewTraceDetailsContent(props: Props) {
  67. const navigate = useNavigate();
  68. const [detail, setDetail] = useState<EventDetail | SpanDetailProps | undefined>(
  69. undefined
  70. );
  71. const traceInfo = useMemo(
  72. () => getTraceInfo(props.traces ?? [], props.orphanErrors),
  73. [props.traces, props.orphanErrors]
  74. );
  75. const root = props.traces?.[0];
  76. const {data: rootEvent, isPending: isRootEventLoading} = useApiQuery<EventTransaction>(
  77. [
  78. `/organizations/${props.organization.slug}/events/${root?.project_slug}:${root?.event_id}/`,
  79. {
  80. query: {
  81. referrer: 'trace-details-summary',
  82. },
  83. },
  84. ],
  85. {
  86. staleTime: Infinity,
  87. enabled: !!(props.traces && props.traces.length > 0),
  88. }
  89. );
  90. const renderTraceLoading = () => {
  91. return (
  92. <LoadingContainer>
  93. <StyledLoadingIndicator />
  94. {t('Hang in there, as we build your trace view!')}
  95. </LoadingContainer>
  96. );
  97. };
  98. const renderTraceRequiresDateRangeSelection = () => {
  99. return <LoadingError message={t('Trace view requires a date range selection.')} />;
  100. };
  101. const renderTraceHeader = () => {
  102. const {meta} = props;
  103. const errors = meta?.errors ?? traceInfo.errors.size;
  104. const performanceIssues =
  105. meta?.performance_issues ?? traceInfo.performanceIssues.size;
  106. const replay_id = rootEvent?.contexts.replay?.replay_id ?? '';
  107. return (
  108. <TraceHeaderContainer>
  109. {rootEvent && (
  110. <TraceHeaderRow>
  111. <MetaData
  112. headingText={t('User')}
  113. tooltipText=""
  114. bodyText={rootEvent?.user?.email ?? rootEvent?.user?.name ?? '\u2014'}
  115. subtext={null}
  116. />
  117. <MetaData
  118. headingText={t('Browser')}
  119. tooltipText=""
  120. bodyText={<BrowserDisplay event={rootEvent} showVersion />}
  121. subtext={null}
  122. />
  123. {replay_id && (
  124. <MetaData
  125. headingText={t('Replay')}
  126. tooltipText=""
  127. bodyText={
  128. <Link
  129. to={normalizeUrl(
  130. `/organizations/${organization.slug}/replays/${replay_id}/`
  131. )}
  132. >
  133. <ReplayLinkBody>
  134. {getShortEventId(replay_id)}
  135. <IconPlay size="xs" />
  136. </ReplayLinkBody>
  137. </Link>
  138. }
  139. subtext={null}
  140. />
  141. )}
  142. </TraceHeaderRow>
  143. )}
  144. <TraceHeaderRow>
  145. <GuideAnchor target="trace_view_guide_breakdown">
  146. <MetaData
  147. headingText={t('Events')}
  148. tooltipText=""
  149. bodyText={meta?.transactions ?? traceInfo.transactions.size}
  150. subtext={null}
  151. />
  152. </GuideAnchor>
  153. <MetaData
  154. headingText={t('Issues')}
  155. tooltipText=""
  156. bodyText={
  157. <Tooltip
  158. title={
  159. errors + performanceIssues > 0 ? (
  160. <Fragment>
  161. <div>{tn('%s error issue', '%s error issues', errors)}</div>
  162. <div>
  163. {tn(
  164. '%s performance issue',
  165. '%s performance issues',
  166. performanceIssues
  167. )}
  168. </div>
  169. </Fragment>
  170. ) : null
  171. }
  172. showUnderline
  173. position="bottom"
  174. >
  175. {errors + performanceIssues}
  176. </Tooltip>
  177. }
  178. subtext={null}
  179. />
  180. <MetaData
  181. headingText={t('Total Duration')}
  182. tooltipText=""
  183. bodyText={getDuration(
  184. traceInfo.endTimestamp - traceInfo.startTimestamp,
  185. 2,
  186. true
  187. )}
  188. subtext={null}
  189. />
  190. </TraceHeaderRow>
  191. </TraceHeaderContainer>
  192. );
  193. };
  194. const getTraceType = (): TraceShape => {
  195. const {traces, orphanErrors} = props;
  196. const {roots, orphans} = (traces ?? []).reduce(
  197. (counts, trace) => {
  198. if (isRootTransaction(trace)) {
  199. counts.roots++;
  200. } else {
  201. counts.orphans++;
  202. }
  203. return counts;
  204. },
  205. {roots: 0, orphans: 0}
  206. );
  207. if (roots === 0 && orphans > 0) {
  208. return TraceShape.NO_ROOT;
  209. }
  210. if (roots === 1 && orphans > 0) {
  211. return TraceShape.BROKEN_SUBTRACES;
  212. }
  213. if (roots > 1) {
  214. return TraceShape.MULTIPLE_ROOTS;
  215. }
  216. if (orphanErrors && orphanErrors.length > 1) {
  217. return TraceShape.ONLY_ERRORS;
  218. }
  219. if (roots === 1) {
  220. return TraceShape.ONE_ROOT;
  221. }
  222. if (roots === 0 && orphans === 0) {
  223. return TraceShape.EMPTY_TRACE;
  224. }
  225. throw new Error('Unknown trace type');
  226. };
  227. const renderTraceWarnings = () => {
  228. let warning: React.ReactNode = null;
  229. const traceType = getTraceType();
  230. switch (traceType) {
  231. case TraceShape.NO_ROOT:
  232. warning = (
  233. <Alert type="info" showIcon>
  234. <ExternalLink href="https://docs.sentry.io/concepts/key-terms/tracing/trace-view/#orphan-traces-and-broken-subtraces">
  235. {t(
  236. 'A root transaction is missing. Transactions linked by a dashed line have been orphaned and cannot be directly linked to the root.'
  237. )}
  238. </ExternalLink>
  239. </Alert>
  240. );
  241. break;
  242. case TraceShape.BROKEN_SUBTRACES:
  243. warning = (
  244. <Alert type="info" showIcon>
  245. <ExternalLink href="https://docs.sentry.io/concepts/key-terms/tracing/trace-view/#orphan-traces-and-broken-subtraces">
  246. {t(
  247. 'This trace has broken subtraces. Transactions linked by a dashed line have been orphaned and cannot be directly linked to the root.'
  248. )}
  249. </ExternalLink>
  250. </Alert>
  251. );
  252. break;
  253. case TraceShape.MULTIPLE_ROOTS:
  254. warning = (
  255. <Alert type="info" showIcon>
  256. <ExternalLink href="https://docs.sentry.io/concepts/key-terms/tracing/trace-view/#multiple-roots">
  257. {t('Multiple root transactions have been found with this trace ID.')}
  258. </ExternalLink>
  259. </Alert>
  260. );
  261. break;
  262. case TraceShape.ONLY_ERRORS:
  263. warning = (
  264. <Alert type="info" showIcon>
  265. {tct(
  266. "The good news: we know all these errors are related. The bad news: we can't tell you more than that. If you haven't already, [tracingLink:configure tracing for your SDKs] to get a connected view of your software systems and services.",
  267. {
  268. tracingLink: (
  269. <ExternalLink href="https://docs.sentry.io/product/performance/getting-started/" />
  270. ),
  271. }
  272. )}
  273. </Alert>
  274. );
  275. break;
  276. default:
  277. }
  278. return warning;
  279. };
  280. const renderFooter = () => {
  281. const {traceEventView, organization, location, meta, orphanErrors} = props;
  282. const orphanErrorsCount = orphanErrors?.length ?? 0;
  283. const transactionsCount = meta?.transactions ?? traceInfo?.transactions.size ?? 0;
  284. const totalNumOfEvents = transactionsCount + orphanErrorsCount;
  285. const webVitals = Object.keys(rootEvent?.measurements ?? {})
  286. .filter(name => Boolean(WEB_VITAL_DETAILS[`measurements.${name}`]))
  287. .sort();
  288. return (
  289. rootEvent && (
  290. <TraceHeaderWrapper>
  291. {webVitals.length > 0 && (
  292. <div style={{flex: 1}}>
  293. <EventVitals event={rootEvent} />
  294. </div>
  295. )}
  296. <div style={{flex: 1}}>
  297. <Tags
  298. generateUrl={(key: string, value: string) => {
  299. const url = traceEventView.getResultsViewUrlTarget(
  300. organization.slug,
  301. false
  302. );
  303. url.query = generateQueryWithTag(url.query, {
  304. key: formatTagKey(key),
  305. value,
  306. });
  307. return url;
  308. }}
  309. totalValues={totalNumOfEvents}
  310. eventView={traceEventView}
  311. organization={organization}
  312. location={location}
  313. />
  314. </div>
  315. </TraceHeaderWrapper>
  316. )
  317. );
  318. };
  319. const renderContent = () => {
  320. const {
  321. dateSelected,
  322. isLoading,
  323. error,
  324. organization,
  325. location,
  326. traceEventView,
  327. traceSlug,
  328. traces,
  329. meta,
  330. orphanErrors,
  331. } = props;
  332. if (!dateSelected) {
  333. return renderTraceRequiresDateRangeSelection();
  334. }
  335. const hasOrphanErrors = orphanErrors && orphanErrors.length > 0;
  336. const onlyOrphanErrors = hasOrphanErrors && (!traces || traces.length === 0);
  337. const hasData = hasTraceData(traces, orphanErrors);
  338. if (isLoading || (isRootEventLoading && hasData && !onlyOrphanErrors)) {
  339. return renderTraceLoading();
  340. }
  341. if (error !== null || !hasData) {
  342. return (
  343. <TraceNotFound
  344. meta={meta}
  345. traceEventView={traceEventView}
  346. traceSlug={traceSlug}
  347. location={location}
  348. organization={organization}
  349. />
  350. );
  351. }
  352. return (
  353. <Fragment>
  354. {renderTraceWarnings()}
  355. {traceInfo && renderTraceHeader()}
  356. <Margin>
  357. <VisuallyCompleteWithData id="PerformanceDetails-TraceView" hasData={hasData}>
  358. <NewTraceView
  359. traceType={getTraceType()}
  360. rootEvent={rootEvent}
  361. traceInfo={traceInfo}
  362. location={location}
  363. organization={organization}
  364. traceEventView={traceEventView}
  365. traceSlug={traceSlug}
  366. onRowClick={setDetail}
  367. traces={traces || []}
  368. meta={meta}
  369. orphanErrors={orphanErrors || []}
  370. />
  371. </VisuallyCompleteWithData>
  372. </Margin>
  373. {renderFooter()}
  374. <TraceViewDetailPanel
  375. detail={detail}
  376. onClose={() => {
  377. navigate(
  378. {
  379. ...location,
  380. hash: undefined,
  381. query: {
  382. ...omit(location.query, 'openPanel'),
  383. },
  384. },
  385. {replace: true}
  386. );
  387. setDetail(undefined);
  388. }}
  389. />
  390. </Fragment>
  391. );
  392. };
  393. const {organization, location, traceEventView, traceSlug} = props;
  394. return (
  395. <Fragment>
  396. <Layout.Header>
  397. <Layout.HeaderContent>
  398. <Breadcrumb
  399. organization={organization}
  400. location={location}
  401. transaction={{
  402. project: rootEvent?.projectID ?? '',
  403. name: rootEvent?.title ?? '',
  404. }}
  405. traceSlug={traceSlug}
  406. />
  407. <Layout.Title data-test-id="trace-header">
  408. {t('Trace ID: %s', traceSlug)}
  409. </Layout.Title>
  410. </Layout.HeaderContent>
  411. <Layout.HeaderActions>
  412. <ButtonBar gap={1}>
  413. <DiscoverButton
  414. size="sm"
  415. to={traceEventView.getResultsViewUrlTarget(organization.slug)}
  416. onClick={() => {
  417. trackAnalytics('performance_views.trace_view.open_in_discover', {
  418. organization,
  419. });
  420. }}
  421. >
  422. {t('Open in Discover')}
  423. </DiscoverButton>
  424. </ButtonBar>
  425. </Layout.HeaderActions>
  426. </Layout.Header>
  427. <Layout.Body>
  428. <Layout.Main fullWidth>{renderContent()}</Layout.Main>
  429. </Layout.Body>
  430. </Fragment>
  431. );
  432. }
  433. const StyledLoadingIndicator = styled(LoadingIndicator)`
  434. margin-bottom: 0;
  435. `;
  436. const LoadingContainer = styled('div')`
  437. font-size: ${p => p.theme.fontSizeLarge};
  438. color: ${p => p.theme.subText};
  439. text-align: center;
  440. `;
  441. const ReplayLinkBody = styled('div')`
  442. display: flex;
  443. align-items: center;
  444. gap: ${space(0.25)};
  445. `;
  446. const TraceHeaderContainer = styled('div')`
  447. display: flex;
  448. align-items: center;
  449. justify-content: space-between;
  450. `;
  451. const TraceHeaderRow = styled('div')`
  452. display: flex;
  453. align-items: center;
  454. gap: ${space(2)};
  455. `;
  456. const Margin = styled('div')`
  457. margin-top: ${space(2)};
  458. `;
  459. const TraceHeaderWrapper = styled('div')`
  460. display: flex;
  461. gap: ${space(2)};
  462. `;
  463. export default NewTraceDetailsContent;