suspectSpansTable.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {ReactNode} from 'react';
  2. import {Location} from 'history';
  3. import GridEditable, {
  4. COL_WIDTH_UNDEFINED,
  5. GridColumnOrder,
  6. } from 'sentry/components/gridEditable';
  7. import SortLink from 'sentry/components/gridEditable/sortLink';
  8. import Link from 'sentry/components/links/link';
  9. import {t} from 'sentry/locale';
  10. import {Organization, Project} from 'sentry/types';
  11. import {defined} from 'sentry/utils';
  12. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  13. import {ColumnType, fieldAlignment} from 'sentry/utils/discover/fields';
  14. import {Container as TableCellContainer} from 'sentry/utils/discover/styles';
  15. import {SuspectSpans} from 'sentry/utils/performance/suspectSpans/types';
  16. import {spanDetailsRouteWithQuery} from './spanDetails/utils';
  17. import {SpanSort, SpanSortOthers, SpanSortPercentiles, SpansTotalValues} from './types';
  18. type Props = {
  19. isLoading: boolean;
  20. location: Location;
  21. organization: Organization;
  22. sort: SpanSort;
  23. suspectSpans: SuspectSpans;
  24. totals: SpansTotalValues | null;
  25. transactionName: string;
  26. project?: Project;
  27. };
  28. export default function SuspectSpansTable(props: Props) {
  29. const {
  30. location,
  31. organization,
  32. transactionName,
  33. isLoading,
  34. suspectSpans,
  35. totals,
  36. sort,
  37. project,
  38. } = props;
  39. const data: TableDataRowWithExtras[] = suspectSpans.map(suspectSpan => ({
  40. operation: suspectSpan.op,
  41. group: suspectSpan.group,
  42. description: suspectSpan.description,
  43. totalCount: suspectSpan.count,
  44. frequency:
  45. // Frequency is computed using the `uniq` function in ClickHouse.
  46. // Because it is an approximation, it can occasionally exceed the number of events.
  47. defined(suspectSpan.frequency) && defined(totals?.['count()'])
  48. ? Math.min(1, suspectSpan.frequency / totals!['count()'])
  49. : null,
  50. avgOccurrences: suspectSpan.avgOccurrences,
  51. p50ExclusiveTime: suspectSpan.p50ExclusiveTime,
  52. p75ExclusiveTime: suspectSpan.p75ExclusiveTime,
  53. p95ExclusiveTime: suspectSpan.p95ExclusiveTime,
  54. p99ExclusiveTime: suspectSpan.p99ExclusiveTime,
  55. sumExclusiveTime: suspectSpan.sumExclusiveTime,
  56. }));
  57. return (
  58. <GridEditable
  59. isLoading={isLoading}
  60. data={data}
  61. columnOrder={COLUMN_ORDER[sort].map(column => COLUMNS[column])}
  62. columnSortBy={[]}
  63. grid={{
  64. renderHeadCell,
  65. renderBodyCell: renderBodyCellWithMeta(
  66. location,
  67. organization,
  68. transactionName,
  69. project
  70. ),
  71. }}
  72. location={location}
  73. />
  74. );
  75. }
  76. function renderHeadCell(column: TableColumn, _index: number): ReactNode {
  77. const align = fieldAlignment(column.key, COLUMN_TYPE[column.key]);
  78. return (
  79. <SortLink
  80. title={column.name}
  81. align={align}
  82. direction={undefined}
  83. canSort={false}
  84. generateSortLink={() => undefined}
  85. />
  86. );
  87. }
  88. function renderBodyCellWithMeta(
  89. location: Location,
  90. organization: Organization,
  91. transactionName: string,
  92. project?: Project
  93. ) {
  94. return (column: TableColumn, dataRow: TableDataRowWithExtras): React.ReactNode => {
  95. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  96. if (column.key === 'description') {
  97. const target = spanDetailsRouteWithQuery({
  98. orgSlug: organization.slug,
  99. transaction: transactionName,
  100. query: location.query,
  101. spanSlug: {op: dataRow.operation, group: dataRow.group},
  102. projectID: project?.id,
  103. });
  104. return (
  105. <TableCellContainer>
  106. <Link to={target}>{dataRow[column.key] ?? t('(unnamed span)')}</Link>
  107. </TableCellContainer>
  108. );
  109. }
  110. return fieldRenderer(dataRow, {location, organization});
  111. };
  112. }
  113. type TableColumnKey =
  114. | 'operation'
  115. | 'description'
  116. | 'totalCount'
  117. | 'frequency'
  118. | 'avgOccurrences'
  119. | 'p50ExclusiveTime'
  120. | 'p75ExclusiveTime'
  121. | 'p95ExclusiveTime'
  122. | 'p99ExclusiveTime'
  123. | 'sumExclusiveTime';
  124. type TableColumn = GridColumnOrder<TableColumnKey>;
  125. type TableDataRow = Record<TableColumnKey, any>;
  126. type TableDataRowWithExtras = TableDataRow & {
  127. group: string;
  128. };
  129. const COLUMN_ORDER: Record<SpanSort, TableColumnKey[]> = {
  130. [SpanSortOthers.COUNT]: [
  131. 'operation',
  132. 'description',
  133. 'totalCount',
  134. 'frequency',
  135. 'p75ExclusiveTime',
  136. 'sumExclusiveTime',
  137. ],
  138. [SpanSortOthers.AVG_OCCURRENCE]: [
  139. 'operation',
  140. 'description',
  141. 'avgOccurrences',
  142. 'frequency',
  143. 'p75ExclusiveTime',
  144. 'sumExclusiveTime',
  145. ],
  146. [SpanSortOthers.SUM_EXCLUSIVE_TIME]: [
  147. 'operation',
  148. 'description',
  149. 'totalCount',
  150. 'frequency',
  151. 'p75ExclusiveTime',
  152. 'sumExclusiveTime',
  153. ],
  154. [SpanSortPercentiles.P50_EXCLUSIVE_TIME]: [
  155. 'operation',
  156. 'description',
  157. 'totalCount',
  158. 'frequency',
  159. 'p50ExclusiveTime',
  160. 'sumExclusiveTime',
  161. ],
  162. [SpanSortPercentiles.P75_EXCLUSIVE_TIME]: [
  163. 'operation',
  164. 'description',
  165. 'totalCount',
  166. 'frequency',
  167. 'p75ExclusiveTime',
  168. 'sumExclusiveTime',
  169. ],
  170. [SpanSortPercentiles.P95_EXCLUSIVE_TIME]: [
  171. 'operation',
  172. 'description',
  173. 'totalCount',
  174. 'frequency',
  175. 'p95ExclusiveTime',
  176. 'sumExclusiveTime',
  177. ],
  178. [SpanSortPercentiles.P99_EXCLUSIVE_TIME]: [
  179. 'operation',
  180. 'description',
  181. 'totalCount',
  182. 'frequency',
  183. 'p99ExclusiveTime',
  184. 'sumExclusiveTime',
  185. ],
  186. };
  187. const COLUMNS: Record<TableColumnKey, TableColumn> = {
  188. operation: {
  189. key: 'operation',
  190. name: t('Span Operation'),
  191. width: COL_WIDTH_UNDEFINED,
  192. },
  193. description: {
  194. key: 'description',
  195. name: t('Span Name'),
  196. width: COL_WIDTH_UNDEFINED,
  197. },
  198. totalCount: {
  199. key: 'totalCount',
  200. name: t('Total Count'),
  201. width: COL_WIDTH_UNDEFINED,
  202. },
  203. frequency: {
  204. key: 'frequency',
  205. name: t('Frequency'),
  206. width: COL_WIDTH_UNDEFINED,
  207. },
  208. avgOccurrences: {
  209. key: 'avgOccurrences',
  210. name: t('Average Occurrences'),
  211. width: COL_WIDTH_UNDEFINED,
  212. },
  213. p50ExclusiveTime: {
  214. key: 'p50ExclusiveTime',
  215. name: t('P50 Self Time'),
  216. width: COL_WIDTH_UNDEFINED,
  217. },
  218. p75ExclusiveTime: {
  219. key: 'p75ExclusiveTime',
  220. name: t('P75 Self Time'),
  221. width: COL_WIDTH_UNDEFINED,
  222. },
  223. p95ExclusiveTime: {
  224. key: 'p95ExclusiveTime',
  225. name: t('P95 Self Time'),
  226. width: COL_WIDTH_UNDEFINED,
  227. },
  228. p99ExclusiveTime: {
  229. key: 'p99ExclusiveTime',
  230. name: t('P99 Self Time'),
  231. width: COL_WIDTH_UNDEFINED,
  232. },
  233. sumExclusiveTime: {
  234. key: 'sumExclusiveTime',
  235. name: t('Total Self Time'),
  236. width: COL_WIDTH_UNDEFINED,
  237. },
  238. };
  239. const COLUMN_TYPE: Record<TableColumnKey, ColumnType> = {
  240. operation: 'string',
  241. description: 'string',
  242. totalCount: 'integer',
  243. frequency: 'percentage',
  244. avgOccurrences: 'number',
  245. p50ExclusiveTime: 'duration',
  246. p75ExclusiveTime: 'duration',
  247. p95ExclusiveTime: 'duration',
  248. p99ExclusiveTime: 'duration',
  249. sumExclusiveTime: 'duration',
  250. };