suspectSpansTable.tsx 6.4 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, Project} from 'sentry/types';
  9. import {defined} from 'sentry/utils';
  10. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  11. import type {ColumnType} from 'sentry/utils/discover/fields';
  12. import {fieldAlignment} from 'sentry/utils/discover/fields';
  13. import {Container as TableCellContainer} from 'sentry/utils/discover/styles';
  14. import type {SuspectSpans} from 'sentry/utils/performance/suspectSpans/types';
  15. import {spanDetailsRouteWithQuery} from './spanDetails/utils';
  16. import type {SpanSort, SpansTotalValues} from './types';
  17. import {SpanSortOthers, SpanSortPercentiles} 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. frequency:
  44. // Frequency is computed using the `uniq` function in ClickHouse.
  45. // Because it is an approximation, it can occasionally exceed the number of events.
  46. defined(suspectSpan.frequency) && defined(totals?.['count()'])
  47. ? Math.min(1, suspectSpan.frequency / totals!['count()'])
  48. : null,
  49. avgOccurrences: suspectSpan.avgOccurrences,
  50. p50ExclusiveTime: suspectSpan.p50ExclusiveTime,
  51. p75ExclusiveTime: suspectSpan.p75ExclusiveTime,
  52. p95ExclusiveTime: suspectSpan.p95ExclusiveTime,
  53. p99ExclusiveTime: suspectSpan.p99ExclusiveTime,
  54. sumExclusiveTime: suspectSpan.sumExclusiveTime,
  55. }));
  56. return (
  57. <GridEditable
  58. isLoading={isLoading}
  59. data={data}
  60. columnOrder={COLUMN_ORDER[sort].map(column => COLUMNS[column])}
  61. columnSortBy={[]}
  62. grid={{
  63. renderHeadCell,
  64. renderBodyCell: renderBodyCellWithMeta(
  65. location,
  66. organization,
  67. transactionName,
  68. project
  69. ),
  70. }}
  71. location={location}
  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. };