spanSamplesTable.tsx 6.1 KB

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