useSpanTransactionMetrics.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 'http_error_count()': number;
  13. 'http_error_count_percent_change()': number;
  14. 'p50(span.self_time)': number;
  15. 'p95(span.self_time)': number;
  16. 'percentile_percent_change(span.self_time, 0.95)': number;
  17. 'sps()': number;
  18. 'sps_percent_change()': number;
  19. 'sum(span.self_time)': number;
  20. 'time_spent_percentage(local)': number;
  21. transaction: string;
  22. 'transaction.method': string;
  23. };
  24. export const useSpanTransactionMetrics = (
  25. span: Pick<IndexedSpan, 'group'>,
  26. options: {sorts?: Sort[]; transactions?: string[]},
  27. _referrer = 'api.starfish.span-transaction-metrics'
  28. ) => {
  29. const location = useLocation();
  30. const {transactions, sorts} = options;
  31. const eventView = getEventView(span, location, transactions ?? [], sorts);
  32. return useWrappedDiscoverQuery<SpanTransactionMetrics[]>({
  33. eventView,
  34. initialData: [],
  35. enabled: Boolean(span),
  36. limit: 25,
  37. referrer: _referrer,
  38. });
  39. };
  40. function getEventView(
  41. span: {group: string},
  42. location: Location,
  43. transactions: string[],
  44. sorts?: Sort[]
  45. ) {
  46. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  47. const search = new MutableSearch('');
  48. search.addFilterValues(SPAN_GROUP, [cleanGroupId]);
  49. search.addFilterValues('transaction.op', ['http.server']);
  50. if (transactions.length > 0) {
  51. search.addFilterValues('transaction', transactions);
  52. }
  53. const eventView = EventView.fromNewQueryWithLocation(
  54. {
  55. name: '',
  56. query: search.formatString(),
  57. fields: [
  58. 'transaction',
  59. 'transaction.method',
  60. 'sps()',
  61. `sum(${SPAN_SELF_TIME})`,
  62. `p95(${SPAN_SELF_TIME})`,
  63. 'time_spent_percentage(local)',
  64. 'transaction.op',
  65. 'http_error_count()',
  66. ],
  67. orderby: '-time_spent_percentage_local',
  68. dataset: DiscoverDatasets.SPANS_METRICS,
  69. version: 2,
  70. },
  71. location
  72. );
  73. if (sorts) {
  74. eventView.sorts = sorts;
  75. }
  76. return eventView;
  77. }