useSpanTransactionMetrics.tsx 2.5 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, SPAN_GROUP} = 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. referrer: _referrer,
  35. });
  36. };
  37. function getEventView(
  38. span: {group: string},
  39. location: Location,
  40. transactions: string[],
  41. sorts?: Sort[]
  42. ) {
  43. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  44. const search = new MutableSearch('');
  45. search.addFilterValues(SPAN_GROUP, [cleanGroupId]);
  46. search.addFilterValues('transaction.op', ['http.server']);
  47. if (transactions.length > 0) {
  48. search.addFilterValues('transaction', transactions);
  49. }
  50. const eventView = EventView.fromNewQueryWithLocation(
  51. {
  52. name: '',
  53. query: search.formatString(),
  54. fields: [
  55. 'transaction',
  56. 'transaction.method',
  57. 'sps()',
  58. 'sps_percent_change()',
  59. `sum(${SPAN_SELF_TIME})`,
  60. `p95(${SPAN_SELF_TIME})`,
  61. `percentile_percent_change(${SPAN_SELF_TIME}, 0.95)`,
  62. 'time_spent_percentage(local)',
  63. 'transaction.op',
  64. ],
  65. orderby: '-time_spent_percentage_local',
  66. dataset: DiscoverDatasets.SPANS_METRICS,
  67. version: 2,
  68. },
  69. location
  70. );
  71. if (sorts) {
  72. eventView.sorts = sorts;
  73. }
  74. return eventView;
  75. }