useSpanTransactionMetrics.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 {SpanMetricsField} from 'sentry/views/starfish/types';
  8. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  9. const {SPAN_SELF_TIME, SPAN_GROUP} = SpanMetricsField;
  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. cursor?: string
  25. ) => {
  26. const location = useLocation();
  27. const {transactions, sorts} = options;
  28. const eventView = getEventView(group, location, transactions ?? [], sorts);
  29. return useWrappedDiscoverQuery<SpanTransactionMetrics[]>({
  30. eventView,
  31. initialData: [],
  32. enabled: Boolean(group),
  33. limit: 25,
  34. referrer,
  35. cursor,
  36. });
  37. };
  38. function getEventView(
  39. group: string,
  40. location: Location,
  41. transactions: string[],
  42. sorts?: Sort[]
  43. ) {
  44. const search = new MutableSearch('');
  45. search.addFilterValues(SPAN_GROUP, [group]);
  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. 'spm()',
  58. `sum(${SPAN_SELF_TIME})`,
  59. `avg(${SPAN_SELF_TIME})`,
  60. 'time_spent_percentage(local)',
  61. 'transaction.op',
  62. 'http_error_count()',
  63. ],
  64. orderby: '-time_spent_percentage_local',
  65. dataset: DiscoverDatasets.SPANS_METRICS,
  66. version: 2,
  67. },
  68. location
  69. );
  70. if (sorts) {
  71. eventView.sorts = sorts;
  72. }
  73. return eventView;
  74. }