useSpanTransactionMetrics.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {Location} from 'history';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import type {IndexedSpan} from 'sentry/views/starfish/queries/types';
  7. import {SpanMetricsFields} from 'sentry/views/starfish/types';
  8. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  9. const {SPAN_SELF_TIME} = SpanMetricsFields;
  10. export type SpanTransactionMetrics = {
  11. 'p50(span.self_time)': number;
  12. 'p95(span.self_time)': number;
  13. 'percentile_percent_change(span.self_time, 0.95)': number;
  14. 'sps()': number;
  15. 'sps_percent_change()': number;
  16. 'sum(span.self_time)': number;
  17. 'time_spent_percentage(local)': number;
  18. transaction: string;
  19. transactionMethod: string;
  20. };
  21. export const useSpanTransactionMetrics = (
  22. span: Pick<IndexedSpan, 'group'>,
  23. transactions?: string[],
  24. _referrer = 'span-transaction-metrics'
  25. ) => {
  26. const location = useLocation();
  27. const eventView = getEventView(span, location, transactions ?? []);
  28. return useWrappedDiscoverQuery<SpanTransactionMetrics[]>({
  29. eventView,
  30. initialData: [],
  31. enabled: Boolean(span),
  32. });
  33. };
  34. function getEventView(span: {group: string}, location: Location, transactions: string[]) {
  35. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  36. const search = new MutableSearch('');
  37. search.addFilterValues('span.group', [cleanGroupId]);
  38. search.addFilterValues('transaction.op', ['http.server']);
  39. if (transactions.length > 0) {
  40. search.addFilterValues('transaction', transactions);
  41. }
  42. return EventView.fromNewQueryWithLocation(
  43. {
  44. name: '',
  45. query: search.formatString(),
  46. fields: [
  47. 'transaction',
  48. 'transaction.method',
  49. 'sps()',
  50. 'sps_percent_change()',
  51. `sum(${SPAN_SELF_TIME})`,
  52. `p95(${SPAN_SELF_TIME})`,
  53. `percentile_percent_change(${SPAN_SELF_TIME}, 0.95)`,
  54. 'time_spent_percentage(local)',
  55. 'transaction.op',
  56. ],
  57. orderby: '-time_spent_percentage_local',
  58. dataset: DiscoverDatasets.SPANS_METRICS,
  59. projects: [1],
  60. version: 2,
  61. },
  62. location
  63. );
  64. }