spanSummaryTable.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import SearchBar from 'sentry/components/events/searchBar';
  6. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  7. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  8. import Pagination, {type CursorHandler} from 'sentry/components/pagination';
  9. import {ROW_HEIGHT, ROW_PADDING} from 'sentry/components/performance/waterfall/constants';
  10. import PerformanceDuration from 'sentry/components/performanceDuration';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import type {Organization} from 'sentry/types/organization';
  15. import type {Project} from 'sentry/types/project';
  16. import {defined} from 'sentry/utils';
  17. import EventView, {type MetaType} from 'sentry/utils/discover/eventView';
  18. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  19. import type {ColumnType} from 'sentry/utils/discover/fields';
  20. import {
  21. type DiscoverQueryProps,
  22. useGenericDiscoverQuery,
  23. } from 'sentry/utils/discover/genericDiscoverQuery';
  24. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  25. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  26. import {decodeScalar} from 'sentry/utils/queryString';
  27. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  28. import {useLocation} from 'sentry/utils/useLocation';
  29. import {useNavigate} from 'sentry/utils/useNavigate';
  30. import useOrganization from 'sentry/utils/useOrganization';
  31. import {useParams} from 'sentry/utils/useParams';
  32. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  33. import {SpanIdCell} from 'sentry/views/insights/common/components/tableCells/spanIdCell';
  34. import {useSpansIndexed} from 'sentry/views/insights/common/queries/useDiscover';
  35. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  36. import {
  37. ModuleName,
  38. SpanIndexedField,
  39. type SpanIndexedResponse,
  40. type SpanMetricsQueryFilters,
  41. } from 'sentry/views/insights/types';
  42. import {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceMetadataHeader';
  43. import {SpanDurationBar} from 'sentry/views/performance/transactionSummary/transactionSpans/spanDetails/spanDetailsTable';
  44. import {SpanSummaryReferrer} from 'sentry/views/performance/transactionSummary/transactionSpans/spanSummary/referrers';
  45. import {useSpanSummarySort} from 'sentry/views/performance/transactionSummary/transactionSpans/spanSummary/useSpanSummarySort';
  46. import {useSpanFieldSupportedTags} from 'sentry/views/performance/utils/useSpanFieldSupportedTags';
  47. import Tab from '../../tabs';
  48. type DataRowKeys =
  49. | SpanIndexedField.ID
  50. | SpanIndexedField.TIMESTAMP
  51. | SpanIndexedField.SPAN_DURATION
  52. | SpanIndexedField.TRANSACTION_ID
  53. | SpanIndexedField.TRACE
  54. | SpanIndexedField.PROJECT;
  55. type ColumnKeys =
  56. | SpanIndexedField.ID
  57. | SpanIndexedField.TIMESTAMP
  58. | SpanIndexedField.SPAN_DURATION;
  59. type DataRow = Pick<SpanIndexedResponse, DataRowKeys> & {'transaction.duration': number};
  60. type Column = GridColumnHeader<ColumnKeys>;
  61. const COLUMN_ORDER: Column[] = [
  62. {
  63. key: SpanIndexedField.ID,
  64. name: t('Span ID'),
  65. width: COL_WIDTH_UNDEFINED,
  66. },
  67. {
  68. key: SpanIndexedField.TIMESTAMP,
  69. name: t('Timestamp'),
  70. width: COL_WIDTH_UNDEFINED,
  71. },
  72. {
  73. key: SpanIndexedField.SPAN_DURATION,
  74. name: t('Span Duration'),
  75. width: COL_WIDTH_UNDEFINED,
  76. },
  77. ];
  78. const COLUMN_TYPE: Omit<
  79. Record<ColumnKeys, ColumnType>,
  80. 'spans' | 'transactionDuration'
  81. > = {
  82. span_id: 'string',
  83. timestamp: 'date',
  84. 'span.duration': 'duration',
  85. };
  86. const LIMIT = 8;
  87. type Props = {
  88. project: Project | undefined;
  89. };
  90. export default function SpanSummaryTable(props: Props) {
  91. const {project} = props;
  92. const organization = useOrganization();
  93. const supportedTags = useSpanFieldSupportedTags();
  94. const {spanSlug} = useParams();
  95. const navigate = useNavigate();
  96. const [spanOp, groupId] = spanSlug.split(':');
  97. const location = useLocation();
  98. const {transaction} = location.query;
  99. const spansCursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  100. const spansQuery = decodeScalar(location.query.spansQuery);
  101. const filters: SpanMetricsQueryFilters = {
  102. 'span.group': groupId,
  103. 'span.op': spanOp,
  104. transaction: transaction as string,
  105. };
  106. const sort = useSpanSummarySort();
  107. const spanSearchString = new MutableSearch(spansQuery ?? '').formatString();
  108. const search = MutableSearch.fromQueryObject(filters);
  109. search.addStringMultiFilter(spanSearchString);
  110. const {
  111. data: rowData,
  112. pageLinks,
  113. isLoading: isRowDataLoading,
  114. } = useSpansIndexed(
  115. {
  116. fields: [
  117. SpanIndexedField.ID,
  118. SpanIndexedField.TRANSACTION_ID,
  119. SpanIndexedField.TIMESTAMP,
  120. SpanIndexedField.SPAN_DURATION,
  121. SpanIndexedField.TRACE,
  122. SpanIndexedField.PROJECT,
  123. ],
  124. search,
  125. limit: LIMIT,
  126. sorts: [sort],
  127. cursor: spansCursor,
  128. },
  129. SpanSummaryReferrer.SPAN_SUMMARY_TABLE
  130. );
  131. const transactionIds = rowData?.map(row => row[SpanIndexedField.TRANSACTION_ID]);
  132. const eventView = EventView.fromNewQueryWithLocation(
  133. {
  134. name: 'Transaction Durations',
  135. query: MutableSearch.fromQueryObject({
  136. project: project?.slug,
  137. id: `[${transactionIds?.join() ?? ''}]`,
  138. }).formatString(),
  139. fields: ['id', 'transaction.duration'],
  140. version: 2,
  141. },
  142. location
  143. );
  144. const {
  145. isLoading: isTxnDurationDataLoading,
  146. data: txnDurationData,
  147. isError: isTxnDurationError,
  148. } = useGenericDiscoverQuery<
  149. {
  150. data: any[];
  151. meta: MetaType;
  152. },
  153. DiscoverQueryProps
  154. >({
  155. route: 'events',
  156. eventView,
  157. location,
  158. orgSlug: organization.slug,
  159. getRequestPayload: () => ({
  160. ...eventView.getEventsAPIPayload(location),
  161. interval: eventView.interval,
  162. }),
  163. limit: LIMIT,
  164. options: {
  165. refetchOnWindowFocus: false,
  166. enabled: Boolean(rowData && rowData.length > 0),
  167. },
  168. referrer: SpanSummaryReferrer.SPAN_SUMMARY_TABLE,
  169. });
  170. // Restructure the transaction durations into a map for faster lookup
  171. const transactionDurationMap = {};
  172. txnDurationData?.data.forEach(datum => {
  173. transactionDurationMap[datum.id] = datum['transaction.duration'];
  174. });
  175. const mergedData: DataRow[] =
  176. rowData?.map((row: Pick<SpanIndexedResponse, DataRowKeys>) => {
  177. const transactionId = row[SpanIndexedField.TRANSACTION_ID];
  178. const newRow = {
  179. ...row,
  180. 'transaction.duration': transactionDurationMap[transactionId],
  181. };
  182. return newRow;
  183. }) ?? [];
  184. const handleCursor: CursorHandler = (cursor, pathname, query) => {
  185. browserHistory.push({
  186. pathname,
  187. query: {...query, [QueryParameterNames.SPANS_CURSOR]: cursor},
  188. });
  189. };
  190. const handleSearch = (searchString: string) => {
  191. navigate({
  192. ...location,
  193. query: {
  194. ...location.query,
  195. spansQuery: new MutableSearch(searchString).formatString(),
  196. },
  197. });
  198. };
  199. return (
  200. <Fragment>
  201. <StyledSearchBar
  202. organization={organization}
  203. projectIds={eventView.project}
  204. query={spansQuery}
  205. fields={eventView.fields}
  206. placeholder={t('Search for span attributes')}
  207. supportedTags={supportedTags}
  208. dataset={DiscoverDatasets.SPANS_INDEXED}
  209. onSearch={handleSearch}
  210. />
  211. <VisuallyCompleteWithData
  212. id="SpanDetails-SpanDetailsTable"
  213. hasData={!!mergedData?.length}
  214. isLoading={isRowDataLoading}
  215. >
  216. <GridEditable
  217. isLoading={isRowDataLoading}
  218. data={mergedData}
  219. columnOrder={COLUMN_ORDER}
  220. columnSortBy={[
  221. {
  222. key: sort.field,
  223. order: sort.kind,
  224. },
  225. ]}
  226. grid={{
  227. renderHeadCell: column =>
  228. renderHeadCell({
  229. column,
  230. location,
  231. sort,
  232. }),
  233. renderBodyCell: renderBodyCell(
  234. location,
  235. organization,
  236. spanOp,
  237. isTxnDurationDataLoading || isTxnDurationError
  238. ),
  239. }}
  240. />
  241. </VisuallyCompleteWithData>
  242. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  243. </Fragment>
  244. );
  245. }
  246. function renderBodyCell(
  247. location: Location,
  248. organization: Organization,
  249. spanOp: string = '',
  250. isTxnDurationDataLoading: boolean
  251. ) {
  252. return function (column: Column, dataRow: DataRow): React.ReactNode {
  253. const {timestamp, span_id, trace, project} = dataRow;
  254. const spanDuration = dataRow[SpanIndexedField.SPAN_DURATION];
  255. const transactionId = dataRow[SpanIndexedField.TRANSACTION_ID];
  256. const transactionDuration = dataRow['transaction.duration'];
  257. if (column.key === SpanIndexedField.SPAN_DURATION) {
  258. if (isTxnDurationDataLoading) {
  259. return <EmptySpanDurationBar />;
  260. }
  261. if (!transactionDuration) {
  262. return (
  263. <EmptySpanDurationBar>
  264. <Tooltip
  265. title={t('Transaction duration unknown')}
  266. containerDisplayMode="block"
  267. >
  268. <PerformanceDuration abbreviation milliseconds={spanDuration} />
  269. </Tooltip>
  270. </EmptySpanDurationBar>
  271. );
  272. }
  273. return (
  274. <SpanDurationBar
  275. spanOp={spanOp}
  276. spanDuration={spanDuration}
  277. transactionDuration={transactionDuration}
  278. />
  279. );
  280. }
  281. if (column.key === SpanIndexedField.ID) {
  282. if (!defined(span_id)) {
  283. return null;
  284. }
  285. return (
  286. <SpanIdCell
  287. moduleName={ModuleName.OTHER}
  288. projectSlug={project}
  289. spanId={span_id}
  290. timestamp={timestamp}
  291. traceId={trace}
  292. transactionId={transactionId}
  293. location={{
  294. ...location,
  295. query: {
  296. ...location.query,
  297. tab: Tab.SPANS,
  298. spanSlug: `${spanOp}:${transactionId}`,
  299. },
  300. }}
  301. source={TraceViewSources.PERFORMANCE_TRANSACTION_SUMMARY}
  302. />
  303. );
  304. }
  305. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  306. const rendered = fieldRenderer(dataRow, {location, organization});
  307. return rendered;
  308. };
  309. }
  310. const EmptySpanDurationBar = styled('div')`
  311. height: ${ROW_HEIGHT - 2 * ROW_PADDING}px;
  312. width: 100%;
  313. position: relative;
  314. display: flex;
  315. align-items: center;
  316. top: ${space(0.5)};
  317. background-color: ${p => p.theme.gray100};
  318. padding-left: ${space(1)};
  319. color: ${p => p.theme.gray300};
  320. font-size: ${p => p.theme.fontSizeExtraSmall};
  321. font-variant-numeric: tabular-nums;
  322. line-height: 1;
  323. `;
  324. const StyledSearchBar = styled(SearchBar)`
  325. margin-bottom: ${space(2)};
  326. `;