useSpanTransactionMetrics.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 {SpanMetricsFields} from 'sentry/views/starfish/types';
  8. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  9. const {SPAN_SELF_TIME, SPAN_GROUP} = SpanMetricsFields;
  10. export type SpanTransactionMetrics = {
  11. 'http_error_count()': number;
  12. 'http_error_count_percent_change()': number;
  13. 'p50(span.self_time)': number;
  14. 'p95(span.self_time)': number;
  15. 'percentile_percent_change(span.self_time, 0.95)': number;
  16. 'sps()': number;
  17. 'sps_percent_change()': number;
  18. 'sum(span.self_time)': number;
  19. 'time_spent_percentage(local)': number;
  20. transaction: string;
  21. 'transaction.method': string;
  22. };
  23. export const useSpanTransactionMetrics = (
  24. group: string,
  25. options: {sorts?: Sort[]; transactions?: string[]},
  26. _referrer = 'api.starfish.span-transaction-metrics'
  27. ) => {
  28. const location = useLocation();
  29. const {transactions, sorts} = options;
  30. const eventView = getEventView(group, location, transactions ?? [], sorts);
  31. return useWrappedDiscoverQuery<SpanTransactionMetrics[]>({
  32. eventView,
  33. initialData: [],
  34. enabled: Boolean(group),
  35. limit: 25,
  36. referrer: _referrer,
  37. });
  38. };
  39. function getEventView(
  40. group: string,
  41. location: Location,
  42. transactions: string[],
  43. sorts?: Sort[]
  44. ) {
  45. const search = new MutableSearch('');
  46. search.addFilterValues(SPAN_GROUP, [group]);
  47. search.addFilterValues('transaction.op', ['http.server']);
  48. if (transactions.length > 0) {
  49. search.addFilterValues('transaction', transactions);
  50. }
  51. const eventView = EventView.fromNewQueryWithLocation(
  52. {
  53. name: '',
  54. query: search.formatString(),
  55. fields: [
  56. 'transaction',
  57. 'transaction.method',
  58. 'sps()',
  59. `sum(${SPAN_SELF_TIME})`,
  60. `p95(${SPAN_SELF_TIME})`,
  61. 'time_spent_percentage(local)',
  62. 'transaction.op',
  63. 'http_error_count()',
  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. }