serviceEntrySpansTable.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Button, LinkButton} from 'sentry/components/button';
  5. import {CompactSelect} from 'sentry/components/compactSelect';
  6. import type {DropdownOption} from 'sentry/components/discover/transactionsList';
  7. import {InvestigationRuleCreation} from 'sentry/components/dynamicSampling/investigationRule';
  8. import GridEditable, {
  9. COL_WIDTH_UNDEFINED,
  10. type GridColumnHeader,
  11. } from 'sentry/components/gridEditable';
  12. import Pagination, {type CursorHandler} from 'sentry/components/pagination';
  13. import {IconPlay, IconProfiling} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {Organization} from 'sentry/types/organization';
  17. import {parseCursor} from 'sentry/utils/cursor';
  18. import type EventView from 'sentry/utils/discover/eventView';
  19. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  20. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  21. import {decodeScalar} from 'sentry/utils/queryString';
  22. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  23. import {useLocation} from 'sentry/utils/useLocation';
  24. import {useNavigate} from 'sentry/utils/useNavigate';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import useProjects from 'sentry/utils/useProjects';
  27. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  28. import {SpanIdCell} from 'sentry/views/insights/common/components/tableCells/spanIdCell';
  29. import {useEAPSpans} from 'sentry/views/insights/common/queries/useDiscover';
  30. import {type EAPSpanResponse, ModuleName} from 'sentry/views/insights/types';
  31. import {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceHeader/breadcrumbs';
  32. import {TransactionFilterOptions} from 'sentry/views/performance/transactionSummary/utils';
  33. // TODO: When supported, also add span operation breakdown as a field
  34. type Row = Pick<
  35. EAPSpanResponse,
  36. | 'span_id'
  37. | 'user.display'
  38. | 'user.id'
  39. | 'user.email'
  40. | 'user.username'
  41. | 'user.ip'
  42. | 'span.duration'
  43. | 'trace'
  44. | 'timestamp'
  45. | 'replayId'
  46. | 'profile.id'
  47. | 'profiler.id'
  48. | 'thread.id'
  49. | 'precise.start_ts'
  50. | 'precise.finish_ts'
  51. >;
  52. type Column = GridColumnHeader<
  53. | 'span_id'
  54. | 'user.display'
  55. | 'span.duration'
  56. | 'trace'
  57. | 'timestamp'
  58. | 'replayId'
  59. | 'profile.id'
  60. >;
  61. const COLUMN_ORDER: Column[] = [
  62. {
  63. key: 'trace',
  64. name: t('Trace ID'),
  65. width: COL_WIDTH_UNDEFINED,
  66. },
  67. {
  68. key: 'span_id',
  69. name: t('Span ID'),
  70. width: COL_WIDTH_UNDEFINED,
  71. },
  72. {
  73. key: 'user.display',
  74. name: t('User'),
  75. width: COL_WIDTH_UNDEFINED,
  76. },
  77. {
  78. key: 'span.duration',
  79. name: t('Total Duration'),
  80. width: COL_WIDTH_UNDEFINED,
  81. },
  82. {
  83. key: 'timestamp',
  84. name: t('Timestamp'),
  85. width: COL_WIDTH_UNDEFINED,
  86. },
  87. {
  88. key: 'replayId',
  89. name: t('Replay'),
  90. width: COL_WIDTH_UNDEFINED,
  91. },
  92. {
  93. key: 'profile.id',
  94. name: t('Profile'),
  95. width: COL_WIDTH_UNDEFINED,
  96. },
  97. ];
  98. const LIMIT = 5;
  99. const PAGINATION_CURSOR_SIZE = 'xs';
  100. const CURSOR_NAME = 'serviceEntrySpansCursor';
  101. type Props = {
  102. eventView: EventView;
  103. handleDropdownChange: (k: string) => void;
  104. totalValues: Record<string, number> | null;
  105. transactionName: string;
  106. showViewSampledEventsButton?: boolean;
  107. supportsInvestigationRule?: boolean;
  108. };
  109. export function ServiceEntrySpansTable({
  110. eventView,
  111. handleDropdownChange,
  112. totalValues,
  113. transactionName,
  114. supportsInvestigationRule,
  115. showViewSampledEventsButton,
  116. }: Props) {
  117. const location = useLocation();
  118. const organization = useOrganization();
  119. const {projects} = useProjects();
  120. const navigate = useNavigate();
  121. const projectSlug = projects.find(p => p.id === `${eventView.project}`)?.slug;
  122. const cursor = decodeScalar(location.query?.[CURSOR_NAME]);
  123. const {selected, options} = getOTelTransactionsListSort(location);
  124. const p95 = totalValues?.['p95()'] ?? 0;
  125. const eventViewQuery = new MutableSearch(eventView.query);
  126. if (selected.value === TransactionFilterOptions.SLOW && p95) {
  127. eventViewQuery.addFilterValue('span.duration', `<=${p95.toFixed(0)}`);
  128. }
  129. const {
  130. data: tableData,
  131. isLoading,
  132. pageLinks,
  133. meta,
  134. error,
  135. } = useEAPSpans(
  136. {
  137. search: eventViewQuery.formatString(),
  138. fields: [
  139. 'span_id',
  140. 'user.id',
  141. 'user.email',
  142. 'user.username',
  143. 'user.ip',
  144. 'span.duration',
  145. 'trace',
  146. 'timestamp',
  147. 'replayId',
  148. 'profile.id',
  149. 'profiler.id',
  150. 'thread.id',
  151. 'precise.start_ts',
  152. 'precise.finish_ts',
  153. ],
  154. sorts: [selected.sort],
  155. limit: LIMIT,
  156. cursor,
  157. },
  158. 'api.performance.service-entry-spans-table',
  159. true
  160. );
  161. const consolidatedData = tableData?.map(row => {
  162. const user =
  163. row['user.username'] || row['user.email'] || row['user.ip'] || row['user.id'];
  164. return {
  165. ...row,
  166. 'user.display': user,
  167. };
  168. });
  169. const handleCursor: CursorHandler = (_cursor, pathname, query) => {
  170. navigate({
  171. pathname,
  172. query: {...query, [CURSOR_NAME]: _cursor},
  173. });
  174. };
  175. const cursorOffset = parseCursor(cursor)?.offset ?? 0;
  176. const totalNumSamples = cursorOffset;
  177. const handleViewSampledEvents = () => {
  178. if (!projectSlug) {
  179. return;
  180. }
  181. navigate({
  182. pathname: `${location.pathname}events/`,
  183. query: {
  184. ...location.query,
  185. transaction: transactionName,
  186. project: `${eventView.project}`,
  187. },
  188. });
  189. };
  190. return (
  191. <Fragment>
  192. <Header>
  193. <CompactSelect
  194. triggerProps={{prefix: t('Filter'), size: 'xs'}}
  195. value={selected.value}
  196. options={options}
  197. onChange={opt => handleDropdownChange(opt.value)}
  198. />
  199. <HeaderButtonWrapper>
  200. {supportsInvestigationRule && (
  201. <InvestigationRuleWrapper>
  202. <InvestigationRuleCreation
  203. buttonProps={{size: 'xs'}}
  204. eventView={eventView}
  205. numSamples={totalNumSamples}
  206. />
  207. </InvestigationRuleWrapper>
  208. )}
  209. {showViewSampledEventsButton && (
  210. <Button
  211. size="xs"
  212. data-test-id="transaction-events-open"
  213. onClick={handleViewSampledEvents}
  214. >
  215. {t('View Sampled Events')}
  216. </Button>
  217. )}
  218. </HeaderButtonWrapper>
  219. <CustomPagination
  220. pageLinks={pageLinks}
  221. onCursor={handleCursor}
  222. isLoading={isLoading}
  223. />
  224. </Header>
  225. <GridEditable
  226. isLoading={isLoading}
  227. error={error}
  228. data={consolidatedData}
  229. columnOrder={COLUMN_ORDER}
  230. columnSortBy={[]}
  231. grid={{
  232. renderHeadCell: column =>
  233. renderHeadCell({
  234. column,
  235. }),
  236. renderBodyCell: (column, row) =>
  237. renderBodyCell(column, row, meta, projectSlug, location, organization),
  238. }}
  239. />
  240. </Fragment>
  241. );
  242. }
  243. function renderBodyCell(
  244. column: Column,
  245. row: Row,
  246. meta: EventsMetaType | undefined,
  247. projectSlug: string | undefined,
  248. location: Location,
  249. organization: Organization
  250. ) {
  251. if (column.key === 'span_id') {
  252. return (
  253. <SpanIdCell
  254. moduleName={ModuleName.OTHER}
  255. projectSlug={projectSlug ?? ''}
  256. traceId={row.trace}
  257. timestamp={row.timestamp}
  258. transactionId={row.span_id}
  259. spanId={row.span_id}
  260. source={TraceViewSources.PERFORMANCE_TRANSACTION_SUMMARY}
  261. location={location}
  262. />
  263. );
  264. }
  265. if (column.key === 'profile.id') {
  266. return (
  267. <div>
  268. <LinkButton
  269. size="xs"
  270. icon={<IconProfiling size="xs" />}
  271. to={{
  272. pathname: `/organizations/${organization.slug}/profiling/profile/${projectSlug}/${row['profile.id']}/flamegraph/`,
  273. query: {
  274. referrer: 'performance',
  275. },
  276. }}
  277. aria-label={t('View Profile')}
  278. disabled={!row['profile.id']}
  279. />
  280. </div>
  281. );
  282. }
  283. if (column.key === 'replayId') {
  284. return (
  285. <div>
  286. <LinkButton
  287. size="xs"
  288. icon={<IconPlay size="xs" />}
  289. to={{
  290. pathname: `/organizations/${organization.slug}/replays/${row.replayId}/`,
  291. query: {
  292. referrer: 'performance',
  293. },
  294. }}
  295. disabled={!row.replayId}
  296. aria-label={t('View Replay')}
  297. />
  298. </div>
  299. );
  300. }
  301. if (!meta || !meta?.fields) {
  302. return row[column.key];
  303. }
  304. const renderer = getFieldRenderer(column.key, meta.fields, false);
  305. const rendered = renderer(row, {
  306. location,
  307. organization,
  308. unit: meta.units?.[column.key],
  309. });
  310. return rendered;
  311. }
  312. // A wrapper component that handles the isLoading state. This will allow the component to not disappear when the data is loading.
  313. function CustomPagination({
  314. pageLinks,
  315. onCursor,
  316. isLoading,
  317. }: {
  318. isLoading: boolean;
  319. onCursor: CursorHandler;
  320. pageLinks: string | undefined;
  321. }) {
  322. if (isLoading) {
  323. return (
  324. <StyledPagination
  325. pageLinks={'n/a'}
  326. disabled
  327. onCursor={() => {}}
  328. size={PAGINATION_CURSOR_SIZE}
  329. />
  330. );
  331. }
  332. return (
  333. <StyledPagination
  334. pageLinks={pageLinks}
  335. onCursor={onCursor}
  336. size={PAGINATION_CURSOR_SIZE}
  337. />
  338. );
  339. }
  340. // TODO: The span ops breakdown filter will not work here due to OTLP changes.
  341. // this may need to be adjusted in the future to handle the new breakdown filter that will replace it.
  342. function getOTelFilterOptions(): DropdownOption[] {
  343. return [
  344. {
  345. sort: {kind: 'asc', field: 'span.duration'},
  346. value: TransactionFilterOptions.FASTEST,
  347. label: t('Fastest Transactions'),
  348. },
  349. {
  350. sort: {kind: 'desc', field: 'span.duration'},
  351. value: TransactionFilterOptions.SLOW,
  352. label: t('Slow Transactions (p95)'),
  353. },
  354. {
  355. sort: {kind: 'desc', field: 'span.duration'},
  356. value: TransactionFilterOptions.OUTLIER,
  357. label: t('Outlier Transactions (p100)'),
  358. },
  359. {
  360. sort: {kind: 'desc', field: 'timestamp'},
  361. value: TransactionFilterOptions.RECENT,
  362. label: t('Recent Transactions'),
  363. },
  364. ];
  365. }
  366. function getOTelTransactionsListSort(location: Location): {
  367. options: DropdownOption[];
  368. selected: DropdownOption;
  369. } {
  370. const sortOptions = getOTelFilterOptions();
  371. const urlParam = decodeScalar(
  372. location.query.showTransactions,
  373. TransactionFilterOptions.SLOW
  374. );
  375. const selectedSort = sortOptions.find(opt => opt.value === urlParam) || sortOptions[0]!;
  376. return {selected: selectedSort, options: sortOptions};
  377. }
  378. const Header = styled('div')`
  379. display: grid;
  380. grid-template-columns: 1fr auto auto auto;
  381. margin-bottom: ${space(1)};
  382. align-items: center;
  383. `;
  384. const StyledPagination = styled(Pagination)`
  385. margin: 0 0 0 ${space(1)};
  386. `;
  387. const HeaderButtonWrapper = styled('div')`
  388. display: flex;
  389. `;
  390. const InvestigationRuleWrapper = styled('div')`
  391. margin-right: ${space(1)};
  392. `;