transactionsTable.tsx 6.8 KB

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