newTraceDetailsContent.tsx 14 KB

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