index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import type {Dispatch, SetStateAction} from 'react';
  2. import {Fragment, useCallback, useEffect, useMemo, useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import debounce from 'lodash/debounce';
  5. import {Button} from 'sentry/components/button';
  6. import Count from 'sentry/components/count';
  7. import EmptyStateWarning, {EmptyStreamWrapper} from 'sentry/components/emptyStateWarning';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import Pagination from 'sentry/components/pagination';
  11. import PerformanceDuration from 'sentry/components/performanceDuration';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {DEFAULT_PER_PAGE, SPAN_PROPS_DOCS_URL} from 'sentry/constants';
  14. import {IconChevron} from 'sentry/icons/iconChevron';
  15. import {IconWarning} from 'sentry/icons/iconWarning';
  16. import {t, tct} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import type {Confidence} from 'sentry/types/organization';
  19. import {defined} from 'sentry/utils';
  20. import {trackAnalytics} from 'sentry/utils/analytics';
  21. import {decodeScalar} from 'sentry/utils/queryString';
  22. import {useLocation} from 'sentry/utils/useLocation';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import usePageFilters from 'sentry/utils/usePageFilters';
  25. import useProjects from 'sentry/utils/useProjects';
  26. import {
  27. useExploreDataset,
  28. useExploreQuery,
  29. useExploreTitle,
  30. useExploreVisualizes,
  31. } from 'sentry/views/explore/contexts/pageParamsContext';
  32. import {useAnalytics} from 'sentry/views/explore/hooks/useAnalytics';
  33. import {type TraceResult, useTraces} from 'sentry/views/explore/hooks/useTraces';
  34. import {
  35. Description,
  36. ProjectBadgeWrapper,
  37. ProjectsRenderer,
  38. SpanTimeRenderer,
  39. TraceBreakdownRenderer,
  40. TraceIdRenderer,
  41. } from 'sentry/views/explore/tables/tracesTable/fieldRenderers';
  42. import {SpanTable} from 'sentry/views/explore/tables/tracesTable/spansTable';
  43. import {
  44. BreakdownPanelItem,
  45. EmptyStateText,
  46. EmptyValueContainer,
  47. StyledPanel,
  48. StyledPanelHeader,
  49. StyledPanelItem,
  50. TracePanelContent,
  51. WrappingText,
  52. } from 'sentry/views/explore/tables/tracesTable/styles';
  53. interface TracesTableProps {
  54. confidence: Confidence;
  55. setError: Dispatch<SetStateAction<string>>;
  56. }
  57. export function TracesTable({confidence, setError}: TracesTableProps) {
  58. const title = useExploreTitle();
  59. const dataset = useExploreDataset();
  60. const query = useExploreQuery();
  61. const visualizes = useExploreVisualizes();
  62. const organization = useOrganization();
  63. const location = useLocation();
  64. const cursor = decodeScalar(location.query.cursor);
  65. const result = useTraces({
  66. dataset,
  67. query,
  68. limit: DEFAULT_PER_PAGE,
  69. sort: '-timestamp',
  70. cursor,
  71. });
  72. useEffect(() => {
  73. setError(result.error?.message ?? '');
  74. }, [setError, result.error?.message]);
  75. useAnalytics({
  76. dataset,
  77. resultLength: result.data?.data?.length,
  78. resultMode: 'trace samples',
  79. resultStatus: result.status,
  80. resultMissingRoot: result.data?.data?.filter(trace => !defined(trace.name))?.length,
  81. visualizes,
  82. organization,
  83. columns: [
  84. 'trace id',
  85. 'trace root',
  86. 'total spans',
  87. 'timeline',
  88. 'root duration',
  89. 'timestamp',
  90. ],
  91. userQuery: query,
  92. confidence,
  93. title,
  94. });
  95. const {data, isPending, isError, getResponseHeader} = result;
  96. const showErrorState = !isPending && isError;
  97. const showEmptyState = !isPending && !showErrorState && (data?.data?.length ?? 0) === 0;
  98. return (
  99. <Fragment>
  100. <StyledPanel>
  101. <TracePanelContent>
  102. <StyledPanelHeader align="left" lightText>
  103. {t('Trace ID')}
  104. </StyledPanelHeader>
  105. <StyledPanelHeader align="left" lightText>
  106. {t('Trace Root')}
  107. </StyledPanelHeader>
  108. <StyledPanelHeader align="right" lightText>
  109. {!query ? t('Total Spans') : t('Matching Spans')}
  110. </StyledPanelHeader>
  111. <StyledPanelHeader align="left" lightText>
  112. {t('Timeline')}
  113. </StyledPanelHeader>
  114. <StyledPanelHeader align="right" lightText>
  115. {t('Root Duration')}
  116. </StyledPanelHeader>
  117. <StyledPanelHeader align="right" lightText>
  118. {t('Timestamp')}
  119. </StyledPanelHeader>
  120. {isPending && (
  121. <StyledPanelItem span={6} overflow>
  122. <LoadingIndicator />
  123. </StyledPanelItem>
  124. )}
  125. {showErrorState && (
  126. <StyledPanelItem span={6} overflow>
  127. <WarningStreamWrapper>
  128. <IconWarning data-test-id="error-indicator" color="gray300" size="lg" />
  129. </WarningStreamWrapper>
  130. </StyledPanelItem>
  131. )}
  132. {showEmptyState && (
  133. <StyledPanelItem span={6} overflow>
  134. <EmptyStateWarning withIcon>
  135. <EmptyStateText size="fontSizeExtraLarge">
  136. {t('No trace results found')}
  137. </EmptyStateText>
  138. <EmptyStateText size="fontSizeMedium">
  139. {tct('Try adjusting your filters or refer to [docSearchProps].', {
  140. docSearchProps: (
  141. <ExternalLink href={SPAN_PROPS_DOCS_URL}>
  142. {t('docs for search properties')}
  143. </ExternalLink>
  144. ),
  145. })}
  146. </EmptyStateText>
  147. </EmptyStateWarning>
  148. </StyledPanelItem>
  149. )}
  150. {data?.data?.map((trace, i) => (
  151. <TraceRow
  152. key={trace.trace}
  153. trace={trace}
  154. defaultExpanded={query && i === 0}
  155. query={query}
  156. />
  157. ))}
  158. </TracePanelContent>
  159. </StyledPanel>
  160. <Pagination pageLinks={getResponseHeader?.('Link')} />
  161. </Fragment>
  162. );
  163. }
  164. function TraceRow({
  165. defaultExpanded,
  166. trace,
  167. query,
  168. }: {
  169. defaultExpanded;
  170. query: string;
  171. trace: TraceResult;
  172. }) {
  173. const {selection} = usePageFilters();
  174. const {projects} = useProjects();
  175. const [expanded, setExpanded] = useState<boolean>(defaultExpanded);
  176. const location = useLocation();
  177. const organization = useOrganization();
  178. const onClickExpand = useCallback(() => setExpanded(e => !e), [setExpanded]);
  179. const selectedProjects = useMemo(() => {
  180. const selectedProjectIds = new Set(
  181. selection.projects.map(project => project.toString())
  182. );
  183. return new Set(
  184. projects
  185. .filter(project => selectedProjectIds.has(project.id))
  186. .map(project => project.slug)
  187. );
  188. }, [projects, selection.projects]);
  189. const traceProjects = useMemo(() => {
  190. const seenProjects: Set<string> = new Set();
  191. const leadingProjects: string[] = [];
  192. const trailingProjects: string[] = [];
  193. for (let i = 0; i < trace.breakdowns.length; i++) {
  194. const project = trace.breakdowns[i]!.project;
  195. if (!defined(project) || seenProjects.has(project)) {
  196. continue;
  197. }
  198. seenProjects.add(project);
  199. // Priotize projects that are selected in the page filters
  200. if (selectedProjects.has(project)) {
  201. leadingProjects.push(project);
  202. } else {
  203. trailingProjects.push(project);
  204. }
  205. }
  206. return [...leadingProjects, ...trailingProjects];
  207. }, [selectedProjects, trace]);
  208. return (
  209. <Fragment>
  210. <StyledPanelItem align="center" center onClick={onClickExpand}>
  211. <StyledButton
  212. icon={<IconChevron size="xs" direction={expanded ? 'down' : 'right'} />}
  213. aria-label={t('Toggle trace details')}
  214. aria-expanded={expanded}
  215. size="zero"
  216. borderless
  217. onClick={() =>
  218. trackAnalytics('trace_explorer.toggle_trace_details', {
  219. organization,
  220. expanded,
  221. source: 'new explore',
  222. })
  223. }
  224. />
  225. <TraceIdRenderer
  226. traceId={trace.trace}
  227. timestamp={trace.end}
  228. onClick={event => {
  229. event.stopPropagation();
  230. trackAnalytics('trace_explorer.open_trace', {
  231. organization,
  232. source: 'new explore',
  233. });
  234. }}
  235. location={location}
  236. />
  237. </StyledPanelItem>
  238. <StyledPanelItem align="left" overflow>
  239. <Tooltip title={trace.name} containerDisplayMode="block" showOnlyOnOverflow>
  240. <Description>
  241. <ProjectBadgeWrapper>
  242. <ProjectsRenderer
  243. projectSlugs={
  244. traceProjects.length > 0
  245. ? traceProjects
  246. : trace.project
  247. ? [trace.project]
  248. : []
  249. }
  250. />
  251. </ProjectBadgeWrapper>
  252. {trace.name ? (
  253. <WrappingText>{trace.name}</WrappingText>
  254. ) : (
  255. <EmptyValueContainer>{t('Missing Trace Root')}</EmptyValueContainer>
  256. )}
  257. </Description>
  258. </Tooltip>
  259. </StyledPanelItem>
  260. <StyledPanelItem align="right">
  261. {query ? (
  262. tct('[numerator][space]of[space][denominator]', {
  263. numerator: <Count value={trace.matchingSpans} />,
  264. denominator: <Count value={trace.numSpans} />,
  265. space: <Fragment>&nbsp;</Fragment>,
  266. })
  267. ) : (
  268. <Count value={trace.numSpans} />
  269. )}
  270. </StyledPanelItem>
  271. <Breakdown trace={trace} />
  272. <StyledPanelItem align="right">
  273. {defined(trace.rootDuration) ? (
  274. <PerformanceDuration milliseconds={trace.rootDuration} abbreviation />
  275. ) : (
  276. <EmptyValueContainer />
  277. )}
  278. </StyledPanelItem>
  279. <StyledPanelItem align="right">
  280. <SpanTimeRenderer timestamp={trace.end} tooltipShowSeconds />
  281. </StyledPanelItem>
  282. {expanded && <SpanTable trace={trace} />}
  283. </Fragment>
  284. );
  285. }
  286. function Breakdown({trace}: {trace: TraceResult}) {
  287. const [highlightedSliceName, _setHighlightedSliceName] = useState('');
  288. const setHighlightedSliceName = useMemo(
  289. () =>
  290. debounce(sliceName => _setHighlightedSliceName(sliceName), 100, {
  291. leading: true,
  292. }),
  293. [_setHighlightedSliceName]
  294. );
  295. return (
  296. <BreakdownPanelItem
  297. align="right"
  298. highlightedSliceName={highlightedSliceName}
  299. onMouseLeave={() => setHighlightedSliceName('')}
  300. >
  301. <TraceBreakdownRenderer
  302. trace={trace}
  303. setHighlightedSliceName={setHighlightedSliceName}
  304. />
  305. </BreakdownPanelItem>
  306. );
  307. }
  308. const StyledButton = styled(Button)`
  309. margin-right: ${space(0.5)};
  310. `;
  311. const WarningStreamWrapper = styled(EmptyStreamWrapper)`
  312. > svg {
  313. fill: ${p => p.theme.gray300};
  314. }
  315. `;