transactionsTable.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import * as qs from 'query-string';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. type GridColumnHeader,
  8. } from 'sentry/components/gridEditable';
  9. import Link from 'sentry/components/links/link';
  10. import type {CursorHandler} from 'sentry/components/pagination';
  11. import Pagination from 'sentry/components/pagination';
  12. import {t} from 'sentry/locale';
  13. import type {Organization} from 'sentry/types/organization';
  14. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  15. import {FIELD_FORMATTERS, getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  16. import type {Sort} from 'sentry/utils/discover/fields';
  17. import {decodeScalar, decodeSorts} from 'sentry/utils/queryString';
  18. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  19. import {useLocation} from 'sentry/utils/useLocation';
  20. import {useNavigate} from 'sentry/utils/useNavigate';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  23. import {useModuleURL} from 'sentry/views/insights/common/utils/useModuleURL';
  24. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  25. import {useQueuesByTransactionQuery} from 'sentry/views/insights/queues/queries/useQueuesByTransactionQuery';
  26. import {Referrer} from 'sentry/views/insights/queues/referrers';
  27. import {SpanFunction, type SpanMetricsResponse} from 'sentry/views/insights/types';
  28. type Row = Pick<
  29. SpanMetricsResponse,
  30. | 'sum(span.duration)'
  31. | 'transaction'
  32. | `avg_if(${string},${string},${string})`
  33. | `count_op(${string})`
  34. >;
  35. type Column = GridColumnHeader<string>;
  36. const COLUMN_ORDER: Column[] = [
  37. {
  38. key: 'transaction',
  39. name: t('Transactions'),
  40. width: COL_WIDTH_UNDEFINED,
  41. },
  42. {
  43. key: 'span.op',
  44. name: t('Type'),
  45. width: COL_WIDTH_UNDEFINED,
  46. },
  47. {
  48. key: 'avg(messaging.message.receive.latency)',
  49. name: t('Avg Time in Queue'),
  50. width: COL_WIDTH_UNDEFINED,
  51. },
  52. {
  53. key: 'avg_if(span.duration,span.op,queue.process)',
  54. name: t('Avg Processing Time'),
  55. width: COL_WIDTH_UNDEFINED,
  56. },
  57. {
  58. key: 'trace_status_rate(ok)',
  59. name: t('Error Rate'),
  60. width: COL_WIDTH_UNDEFINED,
  61. },
  62. {
  63. key: 'count_op(queue.publish)',
  64. name: t('Published'),
  65. width: COL_WIDTH_UNDEFINED,
  66. },
  67. {
  68. key: 'count_op(queue.process)',
  69. name: t('Processed'),
  70. width: COL_WIDTH_UNDEFINED,
  71. },
  72. {
  73. key: 'time_spent_percentage(app,span.duration)',
  74. name: t('Time Spent'),
  75. width: COL_WIDTH_UNDEFINED,
  76. },
  77. ];
  78. const SORTABLE_FIELDS = [
  79. 'transaction',
  80. 'count_op(queue.publish)',
  81. 'count_op(queue.process)',
  82. 'avg_if(span.duration,span.op,queue.process)',
  83. 'avg(messaging.message.receive.latency)',
  84. `${SpanFunction.TIME_SPENT_PERCENTAGE}(app,span.duration)`,
  85. ] as const;
  86. type ValidSort = Sort & {
  87. field: (typeof SORTABLE_FIELDS)[number];
  88. };
  89. export function isAValidSort(sort: Sort): sort is ValidSort {
  90. return (SORTABLE_FIELDS as unknown as string[]).includes(sort.field);
  91. }
  92. const DEFAULT_SORT = {
  93. field: 'time_spent_percentage(app,span.duration)' as const,
  94. kind: 'desc' as const,
  95. };
  96. export function TransactionsTable() {
  97. const navigate = useNavigate();
  98. const location = useLocation();
  99. const organization = useOrganization();
  100. const locationQuery = useLocationQuery({
  101. fields: {
  102. destination: decodeScalar,
  103. [QueryParameterNames.DESTINATIONS_SORT]: decodeScalar,
  104. },
  105. });
  106. const sort =
  107. decodeSorts(locationQuery[QueryParameterNames.DESTINATIONS_SORT])
  108. .filter(isAValidSort)
  109. .at(0) ?? DEFAULT_SORT;
  110. const {data, isPending, meta, pageLinks, error} = useQueuesByTransactionQuery({
  111. destination: locationQuery.destination,
  112. sort,
  113. referrer: Referrer.QUEUES_SUMMARY_TRANSACTIONS_TABLE,
  114. });
  115. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  116. navigate({
  117. pathname,
  118. query: {...query, [QueryParameterNames.TRANSACTIONS_CURSOR]: newCursor},
  119. });
  120. };
  121. return (
  122. <Fragment>
  123. <GridEditable
  124. aria-label={t('Transactions')}
  125. isLoading={isPending}
  126. error={error}
  127. data={data}
  128. columnOrder={COLUMN_ORDER}
  129. columnSortBy={[
  130. {
  131. key: sort.field,
  132. order: sort.kind,
  133. },
  134. ]}
  135. grid={{
  136. renderHeadCell: column =>
  137. renderHeadCell({
  138. column,
  139. sort,
  140. location,
  141. sortParameterName: QueryParameterNames.DESTINATIONS_SORT,
  142. }),
  143. renderBodyCell: (column, row) =>
  144. renderBodyCell(column, row, meta, location, organization),
  145. }}
  146. />
  147. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  148. </Fragment>
  149. );
  150. }
  151. function renderBodyCell(
  152. column: Column,
  153. row: Row,
  154. meta: EventsMetaType | undefined,
  155. location: Location,
  156. organization: Organization
  157. ) {
  158. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  159. const op = row['span.op'];
  160. const isProducer = op === 'queue.publish';
  161. const isConsumer = op === 'queue.process';
  162. const key = column.key;
  163. if (
  164. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  165. row[key] === undefined ||
  166. (isConsumer && ['count_op(queue.publish)'].includes(key)) ||
  167. (isProducer &&
  168. [
  169. 'count_op(queue.process)',
  170. 'avg(messaging.message.receive.latency)',
  171. 'avg_if(span.duration,span.op,queue.process)',
  172. ].includes(key))
  173. ) {
  174. return (
  175. <AlignRight>
  176. <NoValue>{' \u2014 '}</NoValue>
  177. </AlignRight>
  178. );
  179. }
  180. if (key === 'transaction') {
  181. return <TransactionCell transaction={row[key]} op={op} />;
  182. }
  183. // Need to invert trace_status_rate(ok) to show error rate
  184. if (key === 'trace_status_rate(ok)') {
  185. const formatter = FIELD_FORMATTERS.percentage.renderFunc;
  186. return (
  187. <AlignRight>
  188. {/* @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message */}
  189. {formatter(key, {'trace_status_rate(ok)': 1 - (row[key] ?? 0)})}
  190. </AlignRight>
  191. );
  192. }
  193. if (!meta?.fields) {
  194. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  195. return row[column.key];
  196. }
  197. if (key.startsWith('avg')) {
  198. const renderer = FIELD_FORMATTERS.duration.renderFunc;
  199. return renderer(key, row);
  200. }
  201. if (key === 'span.op') {
  202. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  203. switch (row[key]) {
  204. case 'queue.publish':
  205. return t('Producer');
  206. case 'queue.process':
  207. return t('Consumer');
  208. default:
  209. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  210. return row[key];
  211. }
  212. }
  213. const renderer = getFieldRenderer(column.key, meta.fields, false);
  214. return renderer(row, {
  215. location,
  216. organization,
  217. unit: meta.units?.[column.key],
  218. });
  219. }
  220. function TransactionCell({transaction, op}: {op: string; transaction: string}) {
  221. const moduleURL = useModuleURL('queue');
  222. const {query} = useLocation();
  223. const queryString = {
  224. ...query,
  225. transaction,
  226. 'span.op': op,
  227. };
  228. return (
  229. <NoOverflow>
  230. <Link to={`${moduleURL}/destination/?${qs.stringify(queryString)}`}>
  231. {transaction}
  232. </Link>
  233. </NoOverflow>
  234. );
  235. }
  236. const NoOverflow = styled('span')`
  237. overflow: hidden;
  238. `;
  239. const AlignRight = styled('span')`
  240. text-align: right;
  241. `;
  242. const NoValue = styled('span')`
  243. color: ${p => p.theme.gray300};
  244. `;