tracesTable.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. })
  184. }
  185. />
  186. <TraceIdRenderer
  187. traceId={trace.trace}
  188. timestamp={trace.end}
  189. onClick={() =>
  190. trackAnalytics('trace_explorer.open_trace', {
  191. organization,
  192. })
  193. }
  194. location={location}
  195. />
  196. </StyledPanelItem>
  197. <StyledPanelItem align="left" overflow>
  198. <Description>
  199. <ProjectBadgeWrapper>
  200. <ProjectsRenderer
  201. projectSlugs={
  202. traceProjects.length > 0
  203. ? traceProjects
  204. : trace.project
  205. ? [trace.project]
  206. : []
  207. }
  208. />
  209. </ProjectBadgeWrapper>
  210. {trace.name ? (
  211. <WrappingText>{trace.name}</WrappingText>
  212. ) : (
  213. <EmptyValueContainer>{t('Missing Trace Root')}</EmptyValueContainer>
  214. )}
  215. </Description>
  216. </StyledPanelItem>
  217. <StyledPanelItem align="right">
  218. {areQueriesEmpty(queries) ? (
  219. <Count value={trace.numSpans} />
  220. ) : (
  221. tct('[numerator][space]of[space][denominator]', {
  222. numerator: <Count value={trace.matchingSpans} />,
  223. denominator: <Count value={trace.numSpans} />,
  224. space: <Fragment>&nbsp;</Fragment>,
  225. })
  226. )}
  227. </StyledPanelItem>
  228. <BreakdownPanelItem
  229. align="right"
  230. highlightedSliceName={highlightedSliceName}
  231. onMouseLeave={() => setHighlightedSliceName('')}
  232. >
  233. <TraceBreakdownRenderer
  234. trace={trace}
  235. setHighlightedSliceName={setHighlightedSliceName}
  236. />
  237. </BreakdownPanelItem>
  238. <StyledPanelItem align="right">
  239. <PerformanceDuration milliseconds={trace.duration} abbreviation />
  240. </StyledPanelItem>
  241. <StyledPanelItem align="right">
  242. <SpanTimeRenderer timestamp={trace.end} tooltipShowSeconds />
  243. </StyledPanelItem>
  244. <StyledPanelItem align="right">
  245. <TraceIssuesRenderer
  246. trace={trace}
  247. onClick={() =>
  248. trackAnalytics('trace_explorer.open_in_issues', {
  249. organization,
  250. })
  251. }
  252. />
  253. </StyledPanelItem>
  254. {expanded && (
  255. <SpanTable trace={trace} setHighlightedSliceName={setHighlightedSliceName} />
  256. )}
  257. </Fragment>
  258. );
  259. }
  260. const StyledButton = styled(Button)`
  261. margin-right: ${space(0.5)};
  262. `;