index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import type {Dispatch, SetStateAction} from 'react';
  2. import {Fragment, useCallback, useEffect, useMemo, useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import debounce from 'lodash/debounce';
  5. import {Button} from 'sentry/components/button';
  6. import Count from 'sentry/components/count';
  7. import EmptyStateWarning, {EmptyStreamWrapper} from 'sentry/components/emptyStateWarning';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import Pagination from 'sentry/components/pagination';
  11. import PerformanceDuration from 'sentry/components/performanceDuration';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {DEFAULT_PER_PAGE, SPAN_PROPS_DOCS_URL} from 'sentry/constants';
  14. import {IconChevron} from 'sentry/icons/iconChevron';
  15. import {IconWarning} from 'sentry/icons/iconWarning';
  16. import {t, tct} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import {defined} from 'sentry/utils';
  19. import {trackAnalytics} from 'sentry/utils/analytics';
  20. import {decodeScalar} from 'sentry/utils/queryString';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import usePageFilters from 'sentry/utils/usePageFilters';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import {useAnalytics} from 'sentry/views/explore/hooks/useAnalytics';
  26. import {useDataset} from 'sentry/views/explore/hooks/useDataset';
  27. import {type TraceResult, useTraces} from 'sentry/views/explore/hooks/useTraces';
  28. import {useUserQuery} from 'sentry/views/explore/hooks/useUserQuery';
  29. import {useVisualizes} from 'sentry/views/explore/hooks/useVisualizes';
  30. import {
  31. Description,
  32. ProjectBadgeWrapper,
  33. ProjectsRenderer,
  34. SpanTimeRenderer,
  35. TraceBreakdownRenderer,
  36. TraceIdRenderer,
  37. } from 'sentry/views/explore/tables/tracesTable/fieldRenderers';
  38. import {SpanTable} from 'sentry/views/explore/tables/tracesTable/spansTable';
  39. import {
  40. BreakdownPanelItem,
  41. EmptyStateText,
  42. EmptyValueContainer,
  43. StyledPanel,
  44. StyledPanelHeader,
  45. StyledPanelItem,
  46. TracePanelContent,
  47. WrappingText,
  48. } from 'sentry/views/explore/tables/tracesTable/styles';
  49. interface TracesTableProps {
  50. setError: Dispatch<SetStateAction<string>>;
  51. }
  52. export function TracesTable({setError}: TracesTableProps) {
  53. const [dataset] = useDataset();
  54. const [query] = useUserQuery();
  55. const [visualizes] = useVisualizes();
  56. const organization = useOrganization();
  57. const location = useLocation();
  58. const cursor = decodeScalar(location.query.cursor);
  59. const result = useTraces({
  60. dataset,
  61. query,
  62. limit: DEFAULT_PER_PAGE,
  63. sort: '-timestamp',
  64. cursor,
  65. });
  66. useEffect(() => {
  67. setError(result.error?.message ?? '');
  68. }, [setError, result.error?.message]);
  69. useAnalytics({
  70. resultLength: result.data?.data?.length,
  71. resultMode: 'trace samples',
  72. resultStatus: result.status,
  73. resultMissingRoot: result.data?.data?.filter(trace => !defined(trace.name))?.length,
  74. visualizes,
  75. organization,
  76. columns: [
  77. 'trace id',
  78. 'trace root',
  79. 'total spans',
  80. 'timeline',
  81. 'root duration',
  82. 'timestamp',
  83. ],
  84. userQuery: query,
  85. });
  86. const {data, isPending, isError, getResponseHeader} = result;
  87. const showErrorState = !isPending && isError;
  88. const showEmptyState = !isPending && !showErrorState && (data?.data?.length ?? 0) === 0;
  89. return (
  90. <Fragment>
  91. <StyledPanel>
  92. <TracePanelContent>
  93. <StyledPanelHeader align="left" lightText>
  94. {t('Trace ID')}
  95. </StyledPanelHeader>
  96. <StyledPanelHeader align="left" lightText>
  97. {t('Trace Root')}
  98. </StyledPanelHeader>
  99. <StyledPanelHeader align="right" lightText>
  100. {!query ? t('Total Spans') : t('Matching Spans')}
  101. </StyledPanelHeader>
  102. <StyledPanelHeader align="left" lightText>
  103. {t('Timeline')}
  104. </StyledPanelHeader>
  105. <StyledPanelHeader align="right" lightText>
  106. {t('Root Duration')}
  107. </StyledPanelHeader>
  108. <StyledPanelHeader align="right" lightText>
  109. {t('Timestamp')}
  110. </StyledPanelHeader>
  111. {isPending && (
  112. <StyledPanelItem span={6} overflow>
  113. <LoadingIndicator />
  114. </StyledPanelItem>
  115. )}
  116. {showErrorState && (
  117. <StyledPanelItem span={6} overflow>
  118. <WarningStreamWrapper>
  119. <IconWarning data-test-id="error-indicator" color="gray300" size="lg" />
  120. </WarningStreamWrapper>
  121. </StyledPanelItem>
  122. )}
  123. {showEmptyState && (
  124. <StyledPanelItem span={6} overflow>
  125. <EmptyStateWarning withIcon>
  126. <EmptyStateText size="fontSizeExtraLarge">
  127. {t('No trace results found')}
  128. </EmptyStateText>
  129. <EmptyStateText size="fontSizeMedium">
  130. {tct('Try adjusting your filters or refer to [docSearchProps].', {
  131. docSearchProps: (
  132. <ExternalLink href={SPAN_PROPS_DOCS_URL}>
  133. {t('docs for search properties')}
  134. </ExternalLink>
  135. ),
  136. })}
  137. </EmptyStateText>
  138. </EmptyStateWarning>
  139. </StyledPanelItem>
  140. )}
  141. {data?.data?.map((trace, i) => (
  142. <TraceRow
  143. key={trace.trace}
  144. trace={trace}
  145. defaultExpanded={query && i === 0}
  146. query={query}
  147. />
  148. ))}
  149. </TracePanelContent>
  150. </StyledPanel>
  151. <Pagination pageLinks={getResponseHeader?.('Link')} />
  152. </Fragment>
  153. );
  154. }
  155. function TraceRow({
  156. defaultExpanded,
  157. trace,
  158. query,
  159. }: {
  160. defaultExpanded;
  161. query: string;
  162. trace: TraceResult;
  163. }) {
  164. const {selection} = usePageFilters();
  165. const {projects} = useProjects();
  166. const [expanded, setExpanded] = useState<boolean>(defaultExpanded);
  167. const [highlightedSliceName, _setHighlightedSliceName] = useState('');
  168. const location = useLocation();
  169. const organization = useOrganization();
  170. const setHighlightedSliceName = useMemo(
  171. () =>
  172. debounce(sliceName => _setHighlightedSliceName(sliceName), 100, {
  173. leading: true,
  174. }),
  175. [_setHighlightedSliceName]
  176. );
  177. const onClickExpand = useCallback(() => setExpanded(e => !e), [setExpanded]);
  178. const selectedProjects = useMemo(() => {
  179. const selectedProjectIds = new Set(
  180. selection.projects.map(project => project.toString())
  181. );
  182. return new Set(
  183. projects
  184. .filter(project => selectedProjectIds.has(project.id))
  185. .map(project => project.slug)
  186. );
  187. }, [projects, selection.projects]);
  188. const traceProjects = useMemo(() => {
  189. const seenProjects: Set<string> = new Set();
  190. const leadingProjects: string[] = [];
  191. const trailingProjects: string[] = [];
  192. for (let i = 0; i < trace.breakdowns.length; i++) {
  193. const project = trace.breakdowns[i].project;
  194. if (!defined(project) || seenProjects.has(project)) {
  195. continue;
  196. }
  197. seenProjects.add(project);
  198. // Priotize projects that are selected in the page filters
  199. if (selectedProjects.has(project)) {
  200. leadingProjects.push(project);
  201. } else {
  202. trailingProjects.push(project);
  203. }
  204. }
  205. return [...leadingProjects, ...trailingProjects];
  206. }, [selectedProjects, trace]);
  207. return (
  208. <Fragment>
  209. <StyledPanelItem align="center" center onClick={onClickExpand}>
  210. <StyledButton
  211. icon={<IconChevron size="xs" direction={expanded ? 'down' : 'right'} />}
  212. aria-label={t('Toggle trace details')}
  213. aria-expanded={expanded}
  214. size="zero"
  215. borderless
  216. onClick={() =>
  217. trackAnalytics('trace_explorer.toggle_trace_details', {
  218. organization,
  219. expanded,
  220. source: 'new explore',
  221. })
  222. }
  223. />
  224. <TraceIdRenderer
  225. traceId={trace.trace}
  226. timestamp={trace.end}
  227. onClick={() =>
  228. trackAnalytics('trace_explorer.open_trace', {
  229. organization,
  230. source: 'new explore',
  231. })
  232. }
  233. location={location}
  234. />
  235. </StyledPanelItem>
  236. <StyledPanelItem align="left" overflow>
  237. <Tooltip title={trace.name} containerDisplayMode="block" showOnlyOnOverflow>
  238. <Description>
  239. <ProjectBadgeWrapper>
  240. <ProjectsRenderer
  241. projectSlugs={
  242. traceProjects.length > 0
  243. ? traceProjects
  244. : trace.project
  245. ? [trace.project]
  246. : []
  247. }
  248. />
  249. </ProjectBadgeWrapper>
  250. {trace.name ? (
  251. <WrappingText>{trace.name}</WrappingText>
  252. ) : (
  253. <EmptyValueContainer>{t('Missing Trace Root')}</EmptyValueContainer>
  254. )}
  255. </Description>
  256. </Tooltip>
  257. </StyledPanelItem>
  258. <StyledPanelItem align="right">
  259. {query ? (
  260. tct('[numerator][space]of[space][denominator]', {
  261. numerator: <Count value={trace.matchingSpans} />,
  262. denominator: <Count value={trace.numSpans} />,
  263. space: <Fragment>&nbsp;</Fragment>,
  264. })
  265. ) : (
  266. <Count value={trace.numSpans} />
  267. )}
  268. </StyledPanelItem>
  269. <BreakdownPanelItem
  270. align="right"
  271. highlightedSliceName={highlightedSliceName}
  272. onMouseLeave={() => setHighlightedSliceName('')}
  273. >
  274. <TraceBreakdownRenderer
  275. trace={trace}
  276. setHighlightedSliceName={setHighlightedSliceName}
  277. />
  278. </BreakdownPanelItem>
  279. <StyledPanelItem align="right">
  280. {defined(trace.rootDuration) ? (
  281. <PerformanceDuration milliseconds={trace.rootDuration} abbreviation />
  282. ) : (
  283. <EmptyValueContainer />
  284. )}
  285. </StyledPanelItem>
  286. <StyledPanelItem align="right">
  287. <SpanTimeRenderer timestamp={trace.end} tooltipShowSeconds />
  288. </StyledPanelItem>
  289. {expanded && (
  290. <SpanTable trace={trace} setHighlightedSliceName={setHighlightedSliceName} />
  291. )}
  292. </Fragment>
  293. );
  294. }
  295. const StyledButton = styled(Button)`
  296. margin-right: ${space(0.5)};
  297. `;
  298. const WarningStreamWrapper = styled(EmptyStreamWrapper)`
  299. > svg {
  300. fill: ${p => p.theme.gray300};
  301. }
  302. `;