spanDetailsTable.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import type {GridColumnOrder} from 'sentry/components/gridEditable';
  5. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  6. import SortLink from 'sentry/components/gridEditable/sortLink';
  7. import Link from 'sentry/components/links/link';
  8. import Pagination from 'sentry/components/pagination';
  9. import {DurationPill, RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
  10. import {pickBarColor} from 'sentry/components/performance/waterfall/utils';
  11. import PerformanceDuration from 'sentry/components/performanceDuration';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {Organization} from 'sentry/types/organization';
  16. import type {Project} from 'sentry/types/project';
  17. import {defined} from 'sentry/utils';
  18. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  19. import type {ColumnType} from 'sentry/utils/discover/fields';
  20. import {fieldAlignment} from 'sentry/utils/discover/fields';
  21. import {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
  22. import {formatTraceDuration} from 'sentry/utils/duration/formatTraceDuration';
  23. import {formatPercentage} from 'sentry/utils/number/formatPercentage';
  24. import toPercent from 'sentry/utils/number/toPercent';
  25. import type {
  26. ExampleTransaction,
  27. SuspectSpan,
  28. } from 'sentry/utils/performance/suspectSpans/types';
  29. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  30. import {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceMetadataHeader';
  31. type TableColumnKeys =
  32. | 'id'
  33. | 'timestamp'
  34. | 'transactionDuration'
  35. | 'spanDuration'
  36. | 'occurrences'
  37. | 'cumulativeDuration'
  38. | 'spans'
  39. | 'project';
  40. type TableColumn = GridColumnOrder<TableColumnKeys>;
  41. type TableDataRow = Record<TableColumnKeys, any>;
  42. type Props = {
  43. examples: ExampleTransaction[];
  44. isLoading: boolean;
  45. location: Location;
  46. organization: Organization;
  47. transactionName: string;
  48. pageLinks?: string | null;
  49. project?: Project;
  50. suspectSpan?: SuspectSpan;
  51. };
  52. export default function SpanTable(props: Props) {
  53. const {
  54. location,
  55. organization,
  56. project,
  57. examples,
  58. suspectSpan,
  59. isLoading,
  60. pageLinks,
  61. transactionName,
  62. } = props;
  63. if (!defined(examples)) {
  64. return null;
  65. }
  66. const data = examples
  67. // we assume that the span appears in each example at least once,
  68. // if this assumption is broken, nothing onwards will work so
  69. // filter out such examples
  70. .filter(example => example.spans.length > 0)
  71. .map(example => ({
  72. id: example.id,
  73. project: project?.slug,
  74. // timestamps are in seconds but want them in milliseconds
  75. timestamp: example.finishTimestamp * 1000,
  76. transactionDuration: (example.finishTimestamp - example.startTimestamp) * 1000,
  77. spanDuration: example.nonOverlappingExclusiveTime,
  78. occurrences: example.spans.length,
  79. cumulativeDuration: example.spans.reduce(
  80. (duration, span) => duration + span.exclusiveTime,
  81. 0
  82. ),
  83. spans: example.spans,
  84. }));
  85. return (
  86. <Fragment>
  87. <VisuallyCompleteWithData
  88. id="SpanDetails-SpanDetailsTable"
  89. hasData={!!data.length}
  90. isLoading={isLoading}
  91. >
  92. <GridEditable
  93. isLoading={isLoading}
  94. data={data}
  95. columnOrder={SPANS_TABLE_COLUMN_ORDER}
  96. columnSortBy={[]}
  97. grid={{
  98. renderHeadCell,
  99. renderBodyCell: renderBodyCellWithMeta(
  100. location,
  101. organization,
  102. transactionName,
  103. suspectSpan
  104. ),
  105. }}
  106. />
  107. </VisuallyCompleteWithData>
  108. <Pagination pageLinks={pageLinks ?? null} />
  109. </Fragment>
  110. );
  111. }
  112. function renderHeadCell(column: TableColumn, _index: number): React.ReactNode {
  113. const align = fieldAlignment(column.key, COLUMN_TYPE[column.key]);
  114. return (
  115. <SortLink
  116. title={column.name}
  117. align={align}
  118. direction={undefined}
  119. canSort={false}
  120. generateSortLink={() => undefined}
  121. />
  122. );
  123. }
  124. function renderBodyCellWithMeta(
  125. location: Location,
  126. organization: Organization,
  127. transactionName: string,
  128. suspectSpan?: SuspectSpan
  129. ) {
  130. return function (column: TableColumn, dataRow: TableDataRow): React.ReactNode {
  131. // if the transaction duration is falsey, then just render the span duration on its own
  132. if (column.key === 'spanDuration' && dataRow.transactionDuration) {
  133. return (
  134. <SpanDurationBar
  135. spanOp={suspectSpan?.op ?? ''}
  136. spanDuration={dataRow.spanDuration}
  137. transactionDuration={dataRow.transactionDuration}
  138. />
  139. );
  140. }
  141. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  142. let rendered = fieldRenderer(dataRow, {location, organization});
  143. if (column.key === 'id') {
  144. const traceSlug = dataRow.spans[0] ? dataRow.spans[0].trace : '';
  145. const worstSpan = dataRow.spans.length
  146. ? dataRow.spans.reduce((worst, span) =>
  147. worst.exclusiveTime >= span.exclusiveTime ? worst : span
  148. )
  149. : null;
  150. const target = generateLinkToEventInTraceView({
  151. eventId: dataRow.id,
  152. traceSlug,
  153. timestamp: dataRow.timestamp / 1000,
  154. projectSlug: dataRow.project,
  155. location,
  156. organization,
  157. spanId: worstSpan.id,
  158. transactionName: transactionName,
  159. source: TraceViewSources.PERFORMANCE_TRANSACTION_SUMMARY,
  160. });
  161. rendered = <Link to={target}>{rendered}</Link>;
  162. }
  163. return rendered;
  164. };
  165. }
  166. const COLUMN_TYPE: Omit<
  167. Record<TableColumnKeys, ColumnType>,
  168. 'spans' | 'transactionDuration'
  169. > = {
  170. id: 'string',
  171. timestamp: 'date',
  172. spanDuration: 'duration',
  173. occurrences: 'integer',
  174. cumulativeDuration: 'duration',
  175. project: 'string',
  176. };
  177. const SPANS_TABLE_COLUMN_ORDER: TableColumn[] = [
  178. {
  179. key: 'id',
  180. name: t('Event ID'),
  181. width: COL_WIDTH_UNDEFINED,
  182. },
  183. {
  184. key: 'timestamp',
  185. name: t('Timestamp'),
  186. width: COL_WIDTH_UNDEFINED,
  187. },
  188. {
  189. key: 'spanDuration',
  190. name: t('Span Duration'),
  191. width: COL_WIDTH_UNDEFINED,
  192. },
  193. {
  194. key: 'occurrences',
  195. name: t('Count'),
  196. width: COL_WIDTH_UNDEFINED,
  197. },
  198. {
  199. key: 'cumulativeDuration',
  200. name: t('Cumulative Duration'),
  201. width: COL_WIDTH_UNDEFINED,
  202. },
  203. ];
  204. const DurationBar = styled('div')`
  205. position: relative;
  206. display: flex;
  207. top: ${space(0.5)};
  208. background-color: ${p => p.theme.gray100};
  209. `;
  210. const DurationBarSection = styled(RowRectangle)`
  211. position: relative;
  212. width: 100%;
  213. top: 0;
  214. `;
  215. type SpanDurationBarProps = {
  216. spanDuration: number;
  217. spanOp: string;
  218. transactionDuration: number;
  219. };
  220. export function SpanDurationBar(props: SpanDurationBarProps) {
  221. const {spanOp, spanDuration, transactionDuration} = props;
  222. const widthPercentage = spanDuration / transactionDuration;
  223. const position = widthPercentage < 0.7 ? 'right' : 'inset';
  224. return (
  225. <DurationBar>
  226. <div style={{width: toPercent(widthPercentage)}}>
  227. <Tooltip
  228. title={tct('[percentage] of the transaction ([duration])', {
  229. percentage: formatPercentage(widthPercentage),
  230. duration: formatTraceDuration(transactionDuration),
  231. })}
  232. containerDisplayMode="block"
  233. >
  234. <DurationBarSection style={{backgroundColor: pickBarColor(spanOp)}}>
  235. <DurationPill durationDisplay={position} showDetail={false}>
  236. <PerformanceDuration abbreviation milliseconds={spanDuration} />
  237. </DurationPill>
  238. </DurationBarSection>
  239. </Tooltip>
  240. </div>
  241. </DurationBar>
  242. );
  243. }