newTraceDetailsContent.tsx 15 KB

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