spanSamplesTable.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import styled from '@emotion/styled';
  2. import {LinkButton} from 'sentry/components/button';
  3. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  4. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  5. import Link from 'sentry/components/links/link';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconProfiling} from 'sentry/icons/iconProfiling';
  8. import {t} from 'sentry/locale';
  9. import {trackAnalytics} from 'sentry/utils/analytics';
  10. import {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
  11. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import {DurationComparisonCell} from 'sentry/views/insights/common/components/samplesTable/common';
  15. import {DurationCell} from 'sentry/views/insights/common/components/tableCells/durationCell';
  16. import FilenameCell from 'sentry/views/insights/common/components/tableCells/filenameCell';
  17. import ResourceSizeCell from 'sentry/views/insights/common/components/tableCells/resourceSizeCell';
  18. import {
  19. OverflowEllipsisTextContainer,
  20. TextAlignRight,
  21. } from 'sentry/views/insights/common/components/textAlign';
  22. import type {SpanSample} from 'sentry/views/insights/common/queries/useSpanSamples';
  23. import {useDomainViewFilters} from 'sentry/views/insights/pages/useFilters';
  24. import {type ModuleName, SpanMetricsField} from 'sentry/views/insights/types';
  25. import type {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceHeader/breadcrumbs';
  26. const {HTTP_RESPONSE_CONTENT_LENGTH, SPAN_DESCRIPTION} = SpanMetricsField;
  27. type Keys =
  28. | 'transaction_id'
  29. | 'span_id'
  30. | 'profile_id'
  31. | 'timestamp'
  32. | 'duration'
  33. | 'p95_comparison'
  34. | 'avg_comparison'
  35. | 'http.response_content_length'
  36. | 'span.description';
  37. export type SamplesTableColumnHeader = GridColumnHeader<Keys>;
  38. export const DEFAULT_COLUMN_ORDER: SamplesTableColumnHeader[] = [
  39. {
  40. key: 'span_id',
  41. name: 'Span ID',
  42. width: COL_WIDTH_UNDEFINED,
  43. },
  44. {
  45. key: 'duration',
  46. name: 'Span Duration',
  47. width: COL_WIDTH_UNDEFINED,
  48. },
  49. {
  50. key: 'avg_comparison',
  51. name: 'Compared to Average',
  52. width: COL_WIDTH_UNDEFINED,
  53. },
  54. ];
  55. type SpanTableRow = {
  56. op: string;
  57. trace: string;
  58. transaction: {
  59. 'project.name': string;
  60. timestamp: string;
  61. 'transaction.duration': number;
  62. };
  63. 'transaction.id': string;
  64. } & SpanSample;
  65. type Props = {
  66. avg: number;
  67. data: SpanTableRow[];
  68. groupId: string;
  69. isLoading: boolean;
  70. moduleName: ModuleName;
  71. columnOrder?: SamplesTableColumnHeader[];
  72. highlightedSpanId?: string;
  73. onMouseLeaveSample?: () => void;
  74. onMouseOverSample?: (sample: SpanSample) => void;
  75. source?: TraceViewSources;
  76. };
  77. export function SpanSamplesTable({
  78. groupId,
  79. isLoading,
  80. data,
  81. avg,
  82. moduleName,
  83. highlightedSpanId,
  84. onMouseLeaveSample,
  85. onMouseOverSample,
  86. columnOrder,
  87. source,
  88. }: Props) {
  89. const location = useLocation();
  90. const organization = useOrganization();
  91. const {view} = useDomainViewFilters();
  92. function renderHeadCell(column: GridColumnHeader): React.ReactNode {
  93. if (
  94. column.key === 'p95_comparison' ||
  95. column.key === 'avg_comparison' ||
  96. column.key === 'duration' ||
  97. column.key === HTTP_RESPONSE_CONTENT_LENGTH
  98. ) {
  99. return (
  100. <TextAlignRight>
  101. <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>
  102. </TextAlignRight>
  103. );
  104. }
  105. return <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>;
  106. }
  107. function renderBodyCell(column: GridColumnHeader, row: SpanTableRow): React.ReactNode {
  108. if (column.key === 'transaction_id') {
  109. return (
  110. <Link
  111. to={generateLinkToEventInTraceView({
  112. eventId: row['transaction.id'],
  113. timestamp: row.timestamp,
  114. traceSlug: row.trace,
  115. projectSlug: row.project,
  116. organization,
  117. location: {
  118. ...location,
  119. query: {
  120. ...location.query,
  121. groupId,
  122. },
  123. },
  124. spanId: row.span_id,
  125. source,
  126. view,
  127. })}
  128. >
  129. {row['transaction.id'].slice(0, 8)}
  130. </Link>
  131. );
  132. }
  133. if (column.key === 'span_id') {
  134. return (
  135. <Link
  136. onClick={() =>
  137. trackAnalytics('performance_views.sample_spans.span_clicked', {
  138. organization,
  139. source: moduleName,
  140. })
  141. }
  142. to={generateLinkToEventInTraceView({
  143. eventId: row['transaction.id'],
  144. timestamp: row.timestamp,
  145. traceSlug: row.trace,
  146. projectSlug: row.project,
  147. organization,
  148. location: {
  149. ...location,
  150. query: {
  151. ...location.query,
  152. groupId,
  153. },
  154. },
  155. spanId: row.span_id,
  156. source,
  157. view,
  158. })}
  159. >
  160. {row.span_id}
  161. </Link>
  162. );
  163. }
  164. if (column.key === HTTP_RESPONSE_CONTENT_LENGTH) {
  165. const size = parseInt(row[HTTP_RESPONSE_CONTENT_LENGTH], 10);
  166. return <ResourceSizeCell bytes={size} />;
  167. }
  168. if (column.key === SPAN_DESCRIPTION) {
  169. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  170. return <FilenameCell url={row[SPAN_DESCRIPTION]} />;
  171. }
  172. if (column.key === 'profile_id') {
  173. return (
  174. <IconWrapper>
  175. {row.profile_id ? (
  176. <Tooltip title={t('View Profile')}>
  177. <LinkButton
  178. to={normalizeUrl(
  179. `/organizations/${organization.slug}/profiling/profile/${row.project}/${row.profile_id}/flamegraph/?spanId=${row.span_id}`
  180. )}
  181. size="xs"
  182. >
  183. <IconProfiling size="xs" />
  184. </LinkButton>
  185. </Tooltip>
  186. ) : (
  187. <div>(no value)</div>
  188. )}
  189. </IconWrapper>
  190. );
  191. }
  192. if (column.key === 'duration') {
  193. return <DurationCell milliseconds={row['span.self_time']} />;
  194. }
  195. if (column.key === 'avg_comparison') {
  196. return (
  197. <DurationComparisonCell
  198. duration={row['span.self_time']}
  199. compareToDuration={avg}
  200. />
  201. );
  202. }
  203. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  204. return <span>{row[column.key]}</span>;
  205. }
  206. return (
  207. <GridEditable
  208. isLoading={isLoading}
  209. data={data}
  210. columnOrder={columnOrder ?? DEFAULT_COLUMN_ORDER}
  211. columnSortBy={[]}
  212. onRowMouseOver={onMouseOverSample}
  213. onRowMouseOut={onMouseLeaveSample}
  214. highlightedRowKey={data.findIndex(sample => sample.span_id === highlightedSpanId)}
  215. grid={{
  216. renderHeadCell,
  217. renderBodyCell,
  218. }}
  219. />
  220. );
  221. }
  222. const IconWrapper = styled('div')`
  223. text-align: right;
  224. width: 100%;
  225. height: 26px;
  226. `;