useSpanTransactionMetrics.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {Location} from 'history';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {Sort} from 'sentry/utils/discover/fields';
  4. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  5. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import type {IndexedSpan} from 'sentry/views/starfish/queries/types';
  8. import {SpanMetricsFields} from 'sentry/views/starfish/types';
  9. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  10. const {SPAN_SELF_TIME} = SpanMetricsFields;
  11. export type SpanTransactionMetrics = {
  12. 'p50(span.self_time)': number;
  13. 'p95(span.self_time)': number;
  14. 'percentile_percent_change(span.self_time, 0.95)': number;
  15. 'sps()': number;
  16. 'sps_percent_change()': number;
  17. 'sum(span.self_time)': number;
  18. 'time_spent_percentage(local)': number;
  19. transaction: string;
  20. transactionMethod: string;
  21. };
  22. export const useSpanTransactionMetrics = (
  23. span: Pick<IndexedSpan, 'group'>,
  24. options: {sorts?: Sort[]; transactions?: string[]},
  25. _referrer = 'span-transaction-metrics'
  26. ) => {
  27. const location = useLocation();
  28. const {transactions, sorts} = options;
  29. const eventView = getEventView(span, location, transactions ?? [], sorts);
  30. return useWrappedDiscoverQuery<SpanTransactionMetrics[]>({
  31. eventView,
  32. initialData: [],
  33. enabled: Boolean(span),
  34. });
  35. };
  36. function getEventView(
  37. span: {group: string},
  38. location: Location,
  39. transactions: string[],
  40. sorts?: Sort[]
  41. ) {
  42. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  43. const search = new MutableSearch('');
  44. search.addFilterValues('span.group', [cleanGroupId]);
  45. search.addFilterValues('transaction.op', ['http.server']);
  46. if (transactions.length > 0) {
  47. search.addFilterValues('transaction', transactions);
  48. }
  49. const eventView = EventView.fromNewQueryWithLocation(
  50. {
  51. name: '',
  52. query: search.formatString(),
  53. fields: [
  54. 'transaction',
  55. 'transaction.method',
  56. 'sps()',
  57. 'sps_percent_change()',
  58. `sum(${SPAN_SELF_TIME})`,
  59. `p95(${SPAN_SELF_TIME})`,
  60. `percentile_percent_change(${SPAN_SELF_TIME}, 0.95)`,
  61. 'time_spent_percentage(local)',
  62. 'transaction.op',
  63. ],
  64. orderby: '-time_spent_percentage_local',
  65. dataset: DiscoverDatasets.SPANS_METRICS,
  66. projects: [1],
  67. version: 2,
  68. },
  69. location
  70. );
  71. if (sorts) {
  72. eventView.sorts = sorts;
  73. }
  74. return eventView;
  75. }