newTraceDetailsContent.tsx 15 KB

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