suspectSpansTable.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. 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 (column: TableColumn, dataRow: TableDataRowWithExtras): React.ReactNode => {
  94. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  95. if (column.key === 'description') {
  96. const target = spanDetailsRouteWithQuery({
  97. orgSlug: organization.slug,
  98. transaction: transactionName,
  99. query: location.query,
  100. spanSlug: {op: dataRow.operation, group: dataRow.group},
  101. projectID: project?.id,
  102. });
  103. return (
  104. <TableCellContainer>
  105. <Link to={target}>{dataRow[column.key] ?? t('(unnamed span)')}</Link>
  106. </TableCellContainer>
  107. );
  108. }
  109. return fieldRenderer(dataRow, {location, organization});
  110. };
  111. }
  112. type TableColumnKey =
  113. | 'operation'
  114. | 'description'
  115. | 'frequency'
  116. | 'avgOccurrences'
  117. | 'p50ExclusiveTime'
  118. | 'p75ExclusiveTime'
  119. | 'p95ExclusiveTime'
  120. | 'p99ExclusiveTime'
  121. | 'sumExclusiveTime';
  122. type TableColumn = GridColumnOrder<TableColumnKey>;
  123. type TableDataRow = Record<TableColumnKey, any>;
  124. type TableDataRowWithExtras = TableDataRow & {
  125. group: string;
  126. };
  127. const COLUMN_ORDER: Record<SpanSort, TableColumnKey[]> = {
  128. [SpanSortOthers.AVG_OCCURRENCE]: [
  129. 'operation',
  130. 'description',
  131. 'avgOccurrences',
  132. 'frequency',
  133. 'p75ExclusiveTime',
  134. 'sumExclusiveTime',
  135. ],
  136. [SpanSortOthers.SUM_EXCLUSIVE_TIME]: [
  137. 'operation',
  138. 'description',
  139. 'frequency',
  140. 'p75ExclusiveTime',
  141. 'sumExclusiveTime',
  142. ],
  143. [SpanSortPercentiles.P50_EXCLUSIVE_TIME]: [
  144. 'operation',
  145. 'description',
  146. 'frequency',
  147. 'p50ExclusiveTime',
  148. 'sumExclusiveTime',
  149. ],
  150. [SpanSortPercentiles.P75_EXCLUSIVE_TIME]: [
  151. 'operation',
  152. 'description',
  153. 'frequency',
  154. 'p75ExclusiveTime',
  155. 'sumExclusiveTime',
  156. ],
  157. [SpanSortPercentiles.P95_EXCLUSIVE_TIME]: [
  158. 'operation',
  159. 'description',
  160. 'frequency',
  161. 'p95ExclusiveTime',
  162. 'sumExclusiveTime',
  163. ],
  164. [SpanSortPercentiles.P99_EXCLUSIVE_TIME]: [
  165. 'operation',
  166. 'description',
  167. 'frequency',
  168. 'p99ExclusiveTime',
  169. 'sumExclusiveTime',
  170. ],
  171. };
  172. const COLUMNS: Record<TableColumnKey, TableColumn> = {
  173. operation: {
  174. key: 'operation',
  175. name: t('Span Operation'),
  176. width: COL_WIDTH_UNDEFINED,
  177. },
  178. description: {
  179. key: 'description',
  180. name: t('Span Name'),
  181. width: COL_WIDTH_UNDEFINED,
  182. },
  183. frequency: {
  184. key: 'frequency',
  185. name: t('Frequency'),
  186. width: COL_WIDTH_UNDEFINED,
  187. },
  188. avgOccurrences: {
  189. key: 'avgOccurrences',
  190. name: t('Average Occurrences'),
  191. width: COL_WIDTH_UNDEFINED,
  192. },
  193. p50ExclusiveTime: {
  194. key: 'p50ExclusiveTime',
  195. name: t('P50 Self Time'),
  196. width: COL_WIDTH_UNDEFINED,
  197. },
  198. p75ExclusiveTime: {
  199. key: 'p75ExclusiveTime',
  200. name: t('P75 Self Time'),
  201. width: COL_WIDTH_UNDEFINED,
  202. },
  203. p95ExclusiveTime: {
  204. key: 'p95ExclusiveTime',
  205. name: t('P95 Self Time'),
  206. width: COL_WIDTH_UNDEFINED,
  207. },
  208. p99ExclusiveTime: {
  209. key: 'p99ExclusiveTime',
  210. name: t('P99 Self Time'),
  211. width: COL_WIDTH_UNDEFINED,
  212. },
  213. sumExclusiveTime: {
  214. key: 'sumExclusiveTime',
  215. name: t('Total Self Time'),
  216. width: COL_WIDTH_UNDEFINED,
  217. },
  218. };
  219. const COLUMN_TYPE: Record<TableColumnKey, ColumnType> = {
  220. operation: 'string',
  221. description: 'string',
  222. frequency: 'percentage',
  223. avgOccurrences: 'number',
  224. p50ExclusiveTime: 'duration',
  225. p75ExclusiveTime: 'duration',
  226. p95ExclusiveTime: 'duration',
  227. p99ExclusiveTime: 'duration',
  228. sumExclusiveTime: 'duration',
  229. };