newTraceDetailsContent.tsx 15 KB

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