content.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import {Fragment, useCallback, useMemo, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {useTheme} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import {Button} from 'sentry/components/button';
  6. import Count from 'sentry/components/count';
  7. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  11. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  12. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  13. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  14. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  15. import Panel from 'sentry/components/panels/panel';
  16. import PanelHeader from 'sentry/components/panels/panelHeader';
  17. import PanelItem from 'sentry/components/panels/panelItem';
  18. import PerformanceDuration from 'sentry/components/performanceDuration';
  19. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  20. import {IconChevron} from 'sentry/icons/iconChevron';
  21. import {t} from 'sentry/locale';
  22. import {space} from 'sentry/styles/space';
  23. import type {PageFilters} from 'sentry/types/core';
  24. import {useApiQuery} from 'sentry/utils/queryClient';
  25. import {decodeInteger, decodeScalar} from 'sentry/utils/queryString';
  26. import {useLocation} from 'sentry/utils/useLocation';
  27. import useOrganization from 'sentry/utils/useOrganization';
  28. import usePageFilters from 'sentry/utils/usePageFilters';
  29. import {type Field, FIELDS, SORTS} from './data';
  30. import {
  31. ProjectRenderer,
  32. SpanBreakdownSliceRenderer,
  33. SpanIdRenderer,
  34. TraceBreakdownContainer,
  35. TraceBreakdownRenderer,
  36. TraceIdRenderer,
  37. TraceIssuesRenderer,
  38. } from './fieldRenderers';
  39. import {TracesSearchBar} from './tracesSearchBar';
  40. import {normalizeTraces} from './utils';
  41. const DEFAULT_PER_PAGE = 20;
  42. export function Content() {
  43. const location = useLocation();
  44. const query = useMemo(() => {
  45. return decodeScalar(location.query.query, '');
  46. }, [location.query.query]);
  47. const limit = useMemo(() => {
  48. return decodeInteger(location.query.perPage, DEFAULT_PER_PAGE);
  49. }, [location.query.perPage]);
  50. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  51. (searchQuery: string) => {
  52. browserHistory.push({
  53. ...location,
  54. query: {
  55. ...location.query,
  56. cursor: undefined,
  57. query: searchQuery || undefined,
  58. },
  59. });
  60. },
  61. [location]
  62. );
  63. const traces = useTraces<Field>({
  64. fields: [
  65. ...FIELDS,
  66. ...SORTS.map(field =>
  67. field.startsWith('-') ? (field.substring(1) as Field) : (field as Field)
  68. ),
  69. ],
  70. limit,
  71. query,
  72. sort: SORTS,
  73. });
  74. const isLoading = traces.isFetching;
  75. const isError = !isLoading && traces.isError;
  76. const isEmpty = !isLoading && !isError && (traces?.data?.data?.length ?? 0) === 0;
  77. const data = normalizeTraces(!isLoading && !isError ? traces?.data?.data : undefined);
  78. return (
  79. <LayoutMain fullWidth>
  80. <PageFilterBar condensed>
  81. <ProjectPageFilter />
  82. <EnvironmentPageFilter />
  83. <DatePageFilter />
  84. </PageFilterBar>
  85. <TracesSearchBar query={query} handleSearch={handleSearch} />
  86. <StyledPanel>
  87. <TracePanelContent>
  88. <StyledPanelHeader align="right" lightText>
  89. {t('Trace ID')}
  90. </StyledPanelHeader>
  91. <StyledPanelHeader align="left" lightText>
  92. {t('Trace Root Name')}
  93. </StyledPanelHeader>
  94. <StyledPanelHeader align="right" lightText>
  95. {t('Spans')}
  96. </StyledPanelHeader>
  97. <StyledPanelHeader align="right" lightText>
  98. {t('Breakdown')}
  99. </StyledPanelHeader>
  100. <StyledPanelHeader align="right" lightText>
  101. {t('Trace Duration')}
  102. </StyledPanelHeader>
  103. <StyledPanelHeader align="right" lightText>
  104. {t('Issues')}
  105. </StyledPanelHeader>
  106. <StyledPanelHeader align="right" lightText style={{padding: '5px'}} />
  107. {isLoading && (
  108. <StyledPanelItem span={7}>
  109. <LoadingIndicator />
  110. </StyledPanelItem>
  111. )}
  112. {isError && ( // TODO: need an error state
  113. <StyledPanelItem span={7}>
  114. <EmptyStateWarning withIcon />
  115. </StyledPanelItem>
  116. )}
  117. {isEmpty && (
  118. <StyledPanelItem span={7}>
  119. <EmptyStateWarning withIcon />
  120. </StyledPanelItem>
  121. )}
  122. {data?.map(trace => <TraceRow key={trace.trace} trace={trace} />)}
  123. </TracePanelContent>
  124. </StyledPanel>
  125. </LayoutMain>
  126. );
  127. }
  128. function TraceRow({trace}: {trace: TraceResult<Field>}) {
  129. const [expanded, setExpanded] = useState<boolean>(false);
  130. return (
  131. <Fragment>
  132. <StyledPanelItem align="center" center>
  133. <Button
  134. icon={<IconChevron size="xs" direction={expanded ? 'down' : 'right'} />}
  135. aria-label={t('Toggle trace details')}
  136. aria-expanded={expanded}
  137. size="zero"
  138. borderless
  139. onClick={() => setExpanded(e => !e)}
  140. />
  141. <TraceIdRenderer traceId={trace.trace} timestamp={trace.spans[0].timestamp} />
  142. </StyledPanelItem>
  143. <StyledPanelItem align="left">
  144. {trace.name ? (
  145. trace.name
  146. ) : (
  147. <EmptyValueContainer>{t('No Name Available')}</EmptyValueContainer>
  148. )}
  149. </StyledPanelItem>
  150. <StyledPanelItem align="right">
  151. <Count value={trace.numSpans} />
  152. </StyledPanelItem>
  153. <StyledPanelItem align="right">
  154. <TraceBreakdownRenderer trace={trace} />
  155. </StyledPanelItem>
  156. <StyledPanelItem align="right">
  157. <PerformanceDuration milliseconds={trace.duration} abbreviation />
  158. </StyledPanelItem>
  159. <StyledPanelItem align="right">
  160. <TraceIssuesRenderer trace={trace} />
  161. </StyledPanelItem>
  162. <StyledPanelItem style={{padding: '5px'}} />
  163. {expanded && <SpanTable spans={trace.spans} trace={trace} />}
  164. </Fragment>
  165. );
  166. }
  167. function SpanTable({
  168. spans,
  169. trace,
  170. }: {
  171. spans: SpanResult<Field>[];
  172. trace: TraceResult<Field>;
  173. }) {
  174. return (
  175. <SpanTablePanelItem span={7}>
  176. <StyledPanel>
  177. <SpanPanelContent>
  178. <StyledPanelHeader align="left" lightText>
  179. {t('Span ID')}
  180. </StyledPanelHeader>
  181. <StyledPanelHeader align="left" lightText>
  182. {t('Span Description')}
  183. </StyledPanelHeader>
  184. <StyledPanelHeader align="right" lightText />
  185. <StyledPanelHeader align="right" lightText>
  186. {t('Span Duration')}
  187. </StyledPanelHeader>
  188. <StyledPanelHeader align="right" lightText>
  189. {t('Issues')}
  190. </StyledPanelHeader>
  191. {spans.map(span => (
  192. <SpanRow key={span.id} span={span} trace={trace} />
  193. ))}
  194. </SpanPanelContent>
  195. </StyledPanel>
  196. </SpanTablePanelItem>
  197. );
  198. }
  199. function SpanRow({span, trace}: {span: SpanResult<Field>; trace: TraceResult<Field>}) {
  200. const theme = useTheme();
  201. return (
  202. <Fragment>
  203. <StyledSpanPanelItem align="right">
  204. <SpanIdRenderer
  205. projectSlug={span.project}
  206. transactionId={span['transaction.id']}
  207. spanId={span.id}
  208. traceId={trace.trace}
  209. timestamp={span.timestamp}
  210. />
  211. </StyledSpanPanelItem>
  212. <StyledSpanPanelItem align="left">
  213. <Description>
  214. <ProjectRenderer projectSlug={span.project} hideName />
  215. <strong>{span['span.op']}</strong>
  216. <em>{'\u2014'}</em>
  217. {span['span.description']}
  218. </Description>
  219. </StyledSpanPanelItem>
  220. <StyledSpanPanelItem align="right">
  221. <TraceBreakdownContainer>
  222. <SpanBreakdownSliceRenderer
  223. sliceName={span.project}
  224. sliceStart={Math.ceil(span['precise.start_ts'] * 1000)}
  225. sliceEnd={Math.floor(span['precise.finish_ts'] * 1000)}
  226. trace={trace}
  227. theme={theme}
  228. />
  229. </TraceBreakdownContainer>
  230. </StyledSpanPanelItem>
  231. <StyledSpanPanelItem align="right">
  232. <PerformanceDuration milliseconds={span['span.duration']} abbreviation />
  233. </StyledSpanPanelItem>
  234. <StyledSpanPanelItem align="right">
  235. <EmptyValueContainer>{'\u2014'}</EmptyValueContainer>
  236. </StyledSpanPanelItem>
  237. </Fragment>
  238. );
  239. }
  240. type SpanResult<F extends string> = Record<F, any>;
  241. export interface TraceResult<F extends string> {
  242. breakdowns: TraceBreakdownResult[];
  243. duration: number;
  244. end: number;
  245. name: string | null;
  246. numSpans: number;
  247. spans: SpanResult<F>[];
  248. start: number;
  249. trace: string;
  250. }
  251. interface TraceBreakdownBase {
  252. end: number;
  253. start: number;
  254. }
  255. type TraceBreakdownProject = TraceBreakdownBase & {
  256. kind: 'project';
  257. project: string;
  258. };
  259. type TraceBreakdownMissing = TraceBreakdownBase & {
  260. kind: 'missing';
  261. project: null;
  262. };
  263. export type TraceBreakdownResult = TraceBreakdownProject | TraceBreakdownMissing;
  264. interface TraceResults<F extends string> {
  265. data: TraceResult<F>[];
  266. meta: any;
  267. }
  268. interface UseTracesOptions<F extends string> {
  269. fields: F[];
  270. datetime?: PageFilters['datetime'];
  271. enabled?: boolean;
  272. limit?: number;
  273. query?: string;
  274. sort?: string[];
  275. suggestedQuery?: string;
  276. }
  277. function useTraces<F extends string>({
  278. fields,
  279. datetime,
  280. enabled,
  281. limit,
  282. query,
  283. suggestedQuery,
  284. sort,
  285. }: UseTracesOptions<F>) {
  286. const organization = useOrganization();
  287. const {selection} = usePageFilters();
  288. const path = `/organizations/${organization.slug}/traces/`;
  289. const endpointOptions = {
  290. query: {
  291. project: selection.projects,
  292. environment: selection.environments,
  293. ...(datetime ?? normalizeDateTimeParams(selection.datetime)),
  294. field: fields,
  295. query,
  296. suggestedQuery,
  297. sort,
  298. per_page: limit,
  299. maxSpansPerTrace: 10,
  300. },
  301. };
  302. return useApiQuery<TraceResults<F>>([path, endpointOptions], {
  303. staleTime: 0,
  304. refetchOnWindowFocus: false,
  305. retry: false,
  306. enabled,
  307. });
  308. }
  309. const LayoutMain = styled(Layout.Main)`
  310. display: flex;
  311. flex-direction: column;
  312. gap: ${space(2)};
  313. `;
  314. const StyledPanel = styled(Panel)`
  315. margin-bottom: 0px;
  316. `;
  317. const TracePanelContent = styled('div')`
  318. width: 100%;
  319. display: grid;
  320. grid-template-columns: repeat(1, min-content) auto repeat(2, min-content) 120px 66px 10px;
  321. `;
  322. const SpanPanelContent = styled('div')`
  323. width: 100%;
  324. display: grid;
  325. grid-template-columns: repeat(1, min-content) auto repeat(1, min-content) 120px 66px;
  326. `;
  327. const StyledPanelHeader = styled(PanelHeader)<{align: 'left' | 'right'}>`
  328. white-space: nowrap;
  329. justify-content: ${p => (p.align === 'left' ? 'flex-start' : 'flex-end')};
  330. padding: ${space(2)} ${space(1)};
  331. `;
  332. const Description = styled('div')`
  333. ${p => p.theme.overflowEllipsis};
  334. display: flex;
  335. flex-direction: row;
  336. align-items: center;
  337. gap: ${space(1)};
  338. `;
  339. const StyledPanelItem = styled(PanelItem)<{
  340. align?: 'left' | 'center' | 'right';
  341. span?: number;
  342. }>`
  343. padding: ${space(1)};
  344. ${p => p.theme.overflowEllipsis};
  345. ${p =>
  346. p.align === 'center'
  347. ? `
  348. justify-content: space-around;`
  349. : p.align === 'left' || p.align === 'right'
  350. ? `text-align: ${p.align};`
  351. : undefined}
  352. ${p => p.span && `grid-column: auto / span ${p.span}`}
  353. `;
  354. const StyledSpanPanelItem = styled(StyledPanelItem)`
  355. &:nth-child(10n + 1),
  356. &:nth-child(10n + 2),
  357. &:nth-child(10n + 3),
  358. &:nth-child(10n + 4),
  359. &:nth-child(10n + 5) {
  360. background-color: ${p => p.theme.backgroundSecondary};
  361. }
  362. `;
  363. const SpanTablePanelItem = styled(StyledPanelItem)`
  364. background-color: ${p => p.theme.gray100};
  365. `;
  366. const EmptyValueContainer = styled('span')`
  367. color: ${p => p.theme.gray300};
  368. `;