useSpanTransactionMetrics.tsx 2.2 KB

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