tracesTable.tsx 8.5 KB

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