newTraceDetailsContent.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. {
  138. getShortEventId(replay_id)
  139. }
  140. <IconPlay size='xs'/>
  141. </ReplayLinkBody>
  142. </Link>}
  143. subtext={null}
  144. />
  145. }
  146. </TraceHeaderRow>
  147. )}
  148. <TraceHeaderRow>
  149. <GuideAnchor target="trace_view_guide_breakdown">
  150. <MetaData
  151. headingText={t('Events')}
  152. tooltipText=""
  153. bodyText={meta?.transactions ?? traceInfo.transactions.size}
  154. subtext={null}
  155. />
  156. </GuideAnchor>
  157. <MetaData
  158. headingText={t('Issues')}
  159. tooltipText=""
  160. bodyText={
  161. <Tooltip
  162. title={
  163. errors + performanceIssues > 0 ?
  164. <Fragment>
  165. <div>{tn('%s error issue', '%s error issues', errors)}</div>
  166. <div>{tn('%s performance issue', '%s performance issues', performanceIssues)}</div>
  167. </Fragment>
  168. : null
  169. }
  170. showUnderline
  171. position='bottom'
  172. >
  173. {errors + performanceIssues}
  174. </Tooltip>}
  175. subtext={null}
  176. />
  177. <MetaData
  178. headingText={t('Total Duration')}
  179. tooltipText=""
  180. bodyText={getDuration(
  181. traceInfo.endTimestamp - traceInfo.startTimestamp,
  182. 2,
  183. true
  184. )}
  185. subtext={null}
  186. />
  187. </TraceHeaderRow>
  188. </TraceHeaderContainer>
  189. );
  190. };
  191. const getTraceType = (): TraceType => {
  192. const {traces, orphanErrors} = props;
  193. const {roots, orphans} = (traces ?? []).reduce(
  194. (counts, trace) => {
  195. if (isRootTransaction(trace)) {
  196. counts.roots++;
  197. } else {
  198. counts.orphans++;
  199. }
  200. return counts;
  201. },
  202. {roots: 0, orphans: 0}
  203. );
  204. if (roots === 0 && orphans > 0) {
  205. return TraceType.NO_ROOT;
  206. }
  207. if (roots === 1 && orphans > 0) {
  208. return TraceType.BROKEN_SUBTRACES;
  209. }
  210. if (roots > 1) {
  211. return TraceType.MULTIPLE_ROOTS;
  212. }
  213. if (orphanErrors && orphanErrors.length > 1) {
  214. return TraceType.ONLY_ERRORS;
  215. }
  216. if (roots === 1) {
  217. return TraceType.ONE_ROOT;
  218. }
  219. if (roots === 0 && orphans === 0) {
  220. return TraceType.EMPTY_TRACE;
  221. }
  222. throw new Error('Unknown trace type');
  223. };
  224. const renderTraceWarnings = () => {
  225. let warning: React.ReactNode = null;
  226. const traceType = getTraceType();
  227. switch (traceType) {
  228. case TraceType.NO_ROOT:
  229. warning = (
  230. <Alert type="info" showIcon>
  231. <ExternalLink href="https://docs.sentry.io/product/performance/trace-view/#orphan-traces-and-broken-subtraces">
  232. {t(
  233. 'A root transaction is missing. Transactions linked by a dashed line have been orphaned and cannot be directly linked to the root.'
  234. )}
  235. </ExternalLink>
  236. </Alert>
  237. );
  238. break;
  239. case TraceType.BROKEN_SUBTRACES:
  240. warning = (
  241. <Alert type="info" showIcon>
  242. <ExternalLink href="https://docs.sentry.io/product/performance/trace-view/#orphan-traces-and-broken-subtraces">
  243. {t(
  244. 'This trace has broken subtraces. Transactions linked by a dashed line have been orphaned and cannot be directly linked to the root.'
  245. )}
  246. </ExternalLink>
  247. </Alert>
  248. );
  249. break;
  250. case TraceType.MULTIPLE_ROOTS:
  251. warning = (
  252. <Alert type="info" showIcon>
  253. <ExternalLink href="https://docs.sentry.io/product/sentry-basics/tracing/trace-view/#multiple-roots">
  254. {t('Multiple root transactions have been found with this trace ID.')}
  255. </ExternalLink>
  256. </Alert>
  257. );
  258. break;
  259. case TraceType.ONLY_ERRORS:
  260. warning = (
  261. <Alert type="info" showIcon>
  262. {tct(
  263. "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.",
  264. {
  265. tracingLink: (
  266. <ExternalLink href="https://docs.sentry.io/product/performance/getting-started/" />
  267. ),
  268. }
  269. )}
  270. </Alert>
  271. );
  272. break;
  273. default:
  274. }
  275. return warning;
  276. };
  277. const renderFooter = () => {
  278. const {traceEventView, organization, location, meta, orphanErrors} = props;
  279. const orphanErrorsCount = orphanErrors?.length ?? 0;
  280. const transactionsCount = meta?.transactions ?? traceInfo?.transactions.size ?? 0;
  281. const totalNumOfEvents = transactionsCount + orphanErrorsCount;
  282. const webVitals = Object.keys(rootEvent?.measurements ?? {})
  283. .filter(name => Boolean(WEB_VITAL_DETAILS[`measurements.${name}`]))
  284. .sort();
  285. return (
  286. rootEvent && (
  287. <TraceHeaderWrapper>
  288. {webVitals.length > 0 && (
  289. <div style={{flex: 1}}>
  290. <EventVitals event={rootEvent} />
  291. </div>
  292. )}
  293. <div style={{flex: 1}}>
  294. <Tags
  295. generateUrl={(key: string, value: string) => {
  296. const url = traceEventView.getResultsViewUrlTarget(
  297. organization.slug,
  298. false
  299. );
  300. url.query = generateQueryWithTag(url.query, {
  301. key: formatTagKey(key),
  302. value,
  303. });
  304. return url;
  305. }}
  306. totalValues={totalNumOfEvents}
  307. eventView={traceEventView}
  308. organization={organization}
  309. location={location}
  310. />
  311. </div>
  312. </TraceHeaderWrapper>
  313. )
  314. );
  315. };
  316. const renderContent = () => {
  317. const {
  318. dateSelected,
  319. isLoading,
  320. error,
  321. organization,
  322. location,
  323. traceEventView,
  324. traceSlug,
  325. traces,
  326. meta,
  327. orphanErrors,
  328. } = props;
  329. if (!dateSelected) {
  330. return renderTraceRequiresDateRangeSelection();
  331. }
  332. const hasOrphanErrors = orphanErrors && orphanErrors.length > 0;
  333. const onlyOrphanErrors = hasOrphanErrors && (!traces || traces.length === 0);
  334. if (isLoading || (isRootEventLoading && !onlyOrphanErrors)) {
  335. return renderTraceLoading();
  336. }
  337. const hasData = hasTraceData(traces, orphanErrors);
  338. if (error !== null || !hasData) {
  339. return (
  340. <TraceNotFound
  341. meta={meta}
  342. traceEventView={traceEventView}
  343. traceSlug={traceSlug}
  344. location={location}
  345. organization={organization}
  346. />
  347. );
  348. }
  349. return (
  350. <Fragment>
  351. {renderTraceWarnings()}
  352. {traceInfo && renderTraceHeader()}
  353. <Margin>
  354. <VisuallyCompleteWithData id="PerformanceDetails-TraceView" hasData={hasData}>
  355. <NewTraceView
  356. traceType={getTraceType()}
  357. rootEvent={rootEvent}
  358. traceInfo={traceInfo}
  359. location={location}
  360. organization={organization}
  361. traceEventView={traceEventView}
  362. traceSlug={traceSlug}
  363. onRowClick={setDetail}
  364. traces={traces || []}
  365. meta={meta}
  366. orphanErrors={orphanErrors || []}
  367. />
  368. </VisuallyCompleteWithData>
  369. </Margin>
  370. {renderFooter()}
  371. <TraceViewDetailPanel
  372. detail={detail}
  373. onClose={() => {
  374. router.replace({
  375. ...location,
  376. hash: undefined,
  377. });
  378. setDetail(undefined);
  379. }}
  380. />
  381. </Fragment>
  382. );
  383. };
  384. const {organization, location, traceEventView, traceSlug} = props;
  385. return (
  386. <Fragment>
  387. <Layout.Header>
  388. <Layout.HeaderContent>
  389. <Breadcrumb
  390. organization={organization}
  391. location={location}
  392. transaction={
  393. {
  394. project: rootEvent?.projectID ?? '',
  395. name: rootEvent?.title ?? ''
  396. }
  397. }
  398. traceSlug={traceSlug}
  399. />
  400. <Layout.Title data-test-id="trace-header">
  401. {t('Trace ID: %s', traceSlug)}
  402. </Layout.Title>
  403. </Layout.HeaderContent>
  404. <Layout.HeaderActions>
  405. <ButtonBar gap={1}>
  406. <DiscoverButton
  407. size="sm"
  408. to={traceEventView.getResultsViewUrlTarget(organization.slug)}
  409. onClick={() => {
  410. trackAnalytics('performance_views.trace_view.open_in_discover', {
  411. organization,
  412. });
  413. }}
  414. >
  415. {t('Open in Discover')}
  416. </DiscoverButton>
  417. </ButtonBar>
  418. </Layout.HeaderActions>
  419. </Layout.Header>
  420. <Layout.Body>
  421. <Layout.Main fullWidth>{renderContent()}</Layout.Main>
  422. </Layout.Body>
  423. </Fragment>
  424. );
  425. }
  426. const StyledLoadingIndicator = styled(LoadingIndicator)`
  427. margin-bottom: 0;
  428. `;
  429. const LoadingContainer = styled('div')`
  430. font-size: ${p => p.theme.fontSizeLarge};
  431. color: ${p => p.theme.subText};
  432. text-align: center;
  433. `;
  434. const ReplayLinkBody = styled('div')`
  435. display: flex;
  436. align-items: center;
  437. gap: ${space(0.25)}
  438. `;
  439. const TraceHeaderContainer = styled('div')`
  440. display: flex;
  441. align-items: center;
  442. justify-content: space-between;
  443. `;
  444. const TraceHeaderRow = styled('div')`
  445. display: flex;
  446. align-items: center;
  447. gap: ${space(2)};
  448. `;
  449. const Margin = styled('div')`
  450. margin-top: ${space(2)};
  451. `;
  452. const TraceHeaderWrapper = styled('div')`
  453. display: flex;
  454. gap: ${space(2)};
  455. `;
  456. export default NewTraceDetailsContent;