suspectSpansTable.tsx 6.5 KB

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