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