tracesTable.tsx 8.8 KB

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