tracesTable.tsx 8.7 KB

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