spanDetailsTable.tsx 7.5 KB

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