transactionSamplesTable.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import type {CSSProperties} from 'react';
  2. import {Fragment} from 'react';
  3. import styled from '@emotion/styled';
  4. import DateTime from 'sentry/components/dateTime';
  5. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  6. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  7. import Link from 'sentry/components/links/link';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import {t} from 'sentry/locale';
  10. import type {NewQuery} from 'sentry/types';
  11. import EventView from 'sentry/utils/discover/eventView';
  12. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  13. import {SPAN_OP_RELATIVE_BREAKDOWN_FIELD} from 'sentry/utils/discover/fields';
  14. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  15. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  16. import {useLocation} from 'sentry/utils/useLocation';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  19. import {DurationComparisonCell} from 'sentry/views/starfish/components/samplesTable/common';
  20. import useErrorSamples from 'sentry/views/starfish/components/samplesTable/useErrorSamples';
  21. import {DurationCell} from 'sentry/views/starfish/components/tableCells/durationCell';
  22. import {
  23. OverflowEllipsisTextContainer,
  24. TextAlignLeft,
  25. TextAlignRight,
  26. } from 'sentry/views/starfish/components/textAlign';
  27. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  28. import type {SampleFilter} from 'sentry/views/starfish/views/webServiceView/endpointOverview';
  29. type Keys =
  30. | 'id'
  31. | 'profile_id'
  32. | 'timestamp'
  33. | 'transaction.duration'
  34. | 'avg_comparison'
  35. | 'span_ops_breakdown.relative'
  36. | 'http.status_code'
  37. | 'transaction.status';
  38. type TableColumnHeader = GridColumnHeader<Keys>;
  39. type Props = {
  40. data: DataRow[];
  41. isLoading: boolean;
  42. queryConditions: string[];
  43. sampleFilter: SampleFilter;
  44. setHighlightedId: (string) => void;
  45. averageDuration?: number;
  46. highlightedId?: string;
  47. };
  48. export type DataRow = {
  49. 'http.status_code': number;
  50. id: string;
  51. profile_id: string;
  52. 'spans.browser': number;
  53. 'spans.db': number;
  54. 'spans.http': number;
  55. 'spans.resource': number;
  56. 'spans.ui': number;
  57. timestamp: string;
  58. 'transaction.duration': number;
  59. 'transaction.status': string;
  60. };
  61. export function TransactionSamplesTable({
  62. queryConditions,
  63. sampleFilter,
  64. data,
  65. isLoading,
  66. averageDuration,
  67. setHighlightedId,
  68. highlightedId,
  69. }: Props) {
  70. const location = useLocation();
  71. const organization = useOrganization();
  72. const query = new MutableSearch(queryConditions);
  73. const savedQuery: NewQuery = {
  74. id: undefined,
  75. name: 'Endpoint Overview Samples',
  76. query: query.formatString(),
  77. fields: [],
  78. dataset: DiscoverDatasets.DISCOVER,
  79. version: 2,
  80. };
  81. const columnOrder: TableColumnHeader[] = [
  82. {
  83. key: 'id',
  84. name: 'Event ID',
  85. width: 100,
  86. },
  87. ...(sampleFilter === 'ALL'
  88. ? [
  89. {
  90. key: 'profile_id',
  91. name: 'Profile ID',
  92. width: 140,
  93. } as TableColumnHeader,
  94. {
  95. key: SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  96. name: 'Operation Duration',
  97. width: 180,
  98. } as TableColumnHeader,
  99. {
  100. key: 'timestamp',
  101. name: 'Timestamp',
  102. width: 230,
  103. } as TableColumnHeader,
  104. {
  105. key: 'transaction.duration',
  106. name: DataTitles.duration,
  107. width: 100,
  108. } as TableColumnHeader,
  109. {
  110. key: 'avg_comparison',
  111. name: 'Compared to Average',
  112. width: 100,
  113. } as TableColumnHeader,
  114. ]
  115. : [
  116. {
  117. key: 'http.status_code',
  118. name: 'Response Code',
  119. width: COL_WIDTH_UNDEFINED,
  120. } as TableColumnHeader,
  121. {
  122. key: 'transaction.status',
  123. name: 'Status',
  124. width: COL_WIDTH_UNDEFINED,
  125. } as TableColumnHeader,
  126. {
  127. key: 'timestamp',
  128. name: 'Timestamp',
  129. width: 230,
  130. } as TableColumnHeader,
  131. ]),
  132. ];
  133. const eventView = EventView.fromNewQueryWithLocation(savedQuery, location);
  134. const {isLoading: isErrorSamplesLoading, data: errorSamples} =
  135. useErrorSamples(eventView);
  136. function renderHeadCell(column: GridColumnHeader): React.ReactNode {
  137. if (column.key === 'avg_comparison' || column.key === 'transaction.duration') {
  138. return (
  139. <TextAlignRight>
  140. <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>
  141. </TextAlignRight>
  142. );
  143. }
  144. if (column.key === SPAN_OP_RELATIVE_BREAKDOWN_FIELD) {
  145. return (
  146. <Fragment>
  147. {column.name}
  148. <StyledIconQuestion
  149. size="xs"
  150. position="top"
  151. title={t(
  152. `Span durations are summed over the course of an entire transaction. Any overlapping spans are only counted once.`
  153. )}
  154. />
  155. </Fragment>
  156. );
  157. }
  158. return <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>;
  159. }
  160. function renderBodyCell(column: TableColumnHeader, row: DataRow): React.ReactNode {
  161. const commonProps = {
  162. style: (row.id === highlightedId
  163. ? {fontWeight: 'bold'}
  164. : {}) satisfies CSSProperties,
  165. onMouseEnter: () => setHighlightedId(row.id),
  166. };
  167. if (column.key === 'id') {
  168. return (
  169. <Link
  170. {...commonProps}
  171. to={normalizeUrl(
  172. `/organizations/${organization.slug}/performance/${row['project.name']}:${row.id}`
  173. )}
  174. >
  175. {row.id.slice(0, 8)}
  176. </Link>
  177. );
  178. }
  179. if (column.key === 'profile_id') {
  180. return row.profile_id ? (
  181. <Link
  182. {...commonProps}
  183. to={normalizeUrl(
  184. `/organizations/${organization.slug}/profiling/profile/${row['project.name']}/${row.profile_id}/flamegraph/`
  185. )}
  186. >
  187. {row.profile_id.slice(0, 8)}
  188. </Link>
  189. ) : (
  190. <div {...commonProps}>(no value)</div>
  191. );
  192. }
  193. if (column.key === 'transaction.duration') {
  194. return <DurationCell milliseconds={row['transaction.duration']} />;
  195. }
  196. if (column.key === 'timestamp') {
  197. return <DateTime {...commonProps} date={row[column.key]} year timeZone seconds />;
  198. }
  199. if (column.key === 'avg_comparison') {
  200. return (
  201. <DurationComparisonContainer {...commonProps}>
  202. <DurationComparisonCell
  203. duration={row['transaction.duration']}
  204. compareToDuration={averageDuration ?? 0}
  205. />
  206. </DurationComparisonContainer>
  207. );
  208. }
  209. if (column.key === SPAN_OP_RELATIVE_BREAKDOWN_FIELD) {
  210. return getFieldRenderer(column.key, {})(row, {
  211. location,
  212. organization,
  213. eventView,
  214. });
  215. }
  216. return <TextAlignLeft {...commonProps}>{row[column.key]}</TextAlignLeft>;
  217. }
  218. return (
  219. <div onMouseLeave={() => setHighlightedId(undefined)}>
  220. <GridEditable
  221. isLoading={sampleFilter === 'ALL' ? isLoading : isErrorSamplesLoading}
  222. data={sampleFilter === 'ALL' ? (data as DataRow[]) : (errorSamples as DataRow[])}
  223. columnOrder={columnOrder}
  224. columnSortBy={[]}
  225. location={location}
  226. grid={{
  227. renderHeadCell,
  228. renderBodyCell,
  229. }}
  230. />
  231. </div>
  232. );
  233. }
  234. const DurationComparisonContainer = styled('div')`
  235. text-align: right;
  236. width: 100%;
  237. display: inline-block;
  238. `;
  239. const StyledIconQuestion = styled(QuestionTooltip)`
  240. position: relative;
  241. left: 4px;
  242. `;