spanSamplesTable.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/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 {type ModuleName, SpanMetricsField} from 'sentry/views/insights/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. groupId: string;
  67. isLoading: boolean;
  68. moduleName: ModuleName;
  69. columnOrder?: SamplesTableColumnHeader[];
  70. highlightedSpanId?: string;
  71. onMouseLeaveSample?: () => void;
  72. onMouseOverSample?: (sample: SpanSample) => void;
  73. source?: string;
  74. };
  75. export function SpanSamplesTable({
  76. groupId,
  77. isLoading,
  78. data,
  79. avg,
  80. moduleName,
  81. highlightedSpanId,
  82. onMouseLeaveSample,
  83. onMouseOverSample,
  84. columnOrder,
  85. source,
  86. }: Props) {
  87. const location = useLocation();
  88. const organization = useOrganization();
  89. function renderHeadCell(column: GridColumnHeader): React.ReactNode {
  90. if (
  91. column.key === 'p95_comparison' ||
  92. column.key === 'avg_comparison' ||
  93. column.key === 'duration' ||
  94. column.key === HTTP_RESPONSE_CONTENT_LENGTH
  95. ) {
  96. return (
  97. <TextAlignRight>
  98. <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>
  99. </TextAlignRight>
  100. );
  101. }
  102. return <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>;
  103. }
  104. function renderBodyCell(column: GridColumnHeader, row: SpanTableRow): React.ReactNode {
  105. if (column.key === 'transaction_id') {
  106. return (
  107. <Link
  108. to={generateLinkToEventInTraceView({
  109. eventId: row['transaction.id'],
  110. timestamp: row.timestamp,
  111. traceSlug: row.trace,
  112. projectSlug: row.project,
  113. organization,
  114. location: {
  115. ...location,
  116. query: {
  117. ...location.query,
  118. groupId,
  119. },
  120. },
  121. spanId: row.span_id,
  122. source,
  123. })}
  124. >
  125. {row['transaction.id'].slice(0, 8)}
  126. </Link>
  127. );
  128. }
  129. if (column.key === 'span_id') {
  130. return (
  131. <Link
  132. onClick={() =>
  133. trackAnalytics('performance_views.sample_spans.span_clicked', {
  134. organization,
  135. source: moduleName,
  136. })
  137. }
  138. to={generateLinkToEventInTraceView({
  139. eventId: row['transaction.id'],
  140. timestamp: row.timestamp,
  141. traceSlug: row.trace,
  142. projectSlug: row.project,
  143. organization,
  144. location: {
  145. ...location,
  146. query: {
  147. ...location.query,
  148. groupId,
  149. },
  150. },
  151. spanId: row.span_id,
  152. source,
  153. })}
  154. >
  155. {row.span_id}
  156. </Link>
  157. );
  158. }
  159. if (column.key === HTTP_RESPONSE_CONTENT_LENGTH) {
  160. const size = parseInt(row[HTTP_RESPONSE_CONTENT_LENGTH], 10);
  161. return <ResourceSizeCell bytes={size} />;
  162. }
  163. if (column.key === SPAN_DESCRIPTION) {
  164. return <FilenameCell url={row[SPAN_DESCRIPTION]} />;
  165. }
  166. if (column.key === 'profile_id') {
  167. return (
  168. <IconWrapper>
  169. {row.profile_id ? (
  170. <Tooltip title={t('View Profile')}>
  171. <LinkButton
  172. to={normalizeUrl(
  173. `/organizations/${organization.slug}/profiling/profile/${row.project}/${row.profile_id}/flamegraph/?spanId=${row.span_id}`
  174. )}
  175. size="xs"
  176. >
  177. <IconProfiling size="xs" />
  178. </LinkButton>
  179. </Tooltip>
  180. ) : (
  181. <div>(no value)</div>
  182. )}
  183. </IconWrapper>
  184. );
  185. }
  186. if (column.key === 'duration') {
  187. return <DurationCell milliseconds={row['span.self_time']} />;
  188. }
  189. if (column.key === 'avg_comparison') {
  190. return (
  191. <DurationComparisonCell
  192. duration={row['span.self_time']}
  193. compareToDuration={avg}
  194. />
  195. );
  196. }
  197. return <span>{row[column.key]}</span>;
  198. }
  199. return (
  200. <GridEditable
  201. isLoading={isLoading}
  202. data={data}
  203. columnOrder={columnOrder ?? DEFAULT_COLUMN_ORDER}
  204. columnSortBy={[]}
  205. onRowMouseOver={onMouseOverSample}
  206. onRowMouseOut={onMouseLeaveSample}
  207. highlightedRowKey={data.findIndex(sample => sample.span_id === highlightedSpanId)}
  208. grid={{
  209. renderHeadCell,
  210. renderBodyCell,
  211. }}
  212. />
  213. );
  214. }
  215. const IconWrapper = styled('div')`
  216. text-align: right;
  217. width: 100%;
  218. height: 26px;
  219. `;