newTraceDetailsContent.tsx 15 KB

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