useSpanTransactionMetrics.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. transactionMethod: string;
  23. };
  24. export const useSpanTransactionMetrics = (
  25. span: Pick<IndexedSpan, 'group'>,
  26. options: {sorts?: Sort[]; transactions?: string[]},
  27. _referrer = '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. referrer: _referrer,
  37. });
  38. };
  39. function getEventView(
  40. span: {group: string},
  41. location: Location,
  42. transactions: string[],
  43. sorts?: Sort[]
  44. ) {
  45. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  46. const search = new MutableSearch('');
  47. search.addFilterValues(SPAN_GROUP, [cleanGroupId]);
  48. search.addFilterValues('transaction.op', ['http.server']);
  49. if (transactions.length > 0) {
  50. search.addFilterValues('transaction', transactions);
  51. }
  52. const eventView = EventView.fromNewQueryWithLocation(
  53. {
  54. name: '',
  55. query: search.formatString(),
  56. fields: [
  57. 'transaction',
  58. 'transaction.method',
  59. 'sps()',
  60. 'sps_percent_change()',
  61. `sum(${SPAN_SELF_TIME})`,
  62. `p95(${SPAN_SELF_TIME})`,
  63. `percentile_percent_change(${SPAN_SELF_TIME}, 0.95)`,
  64. 'time_spent_percentage(local)',
  65. 'transaction.op',
  66. 'http_error_count()',
  67. 'http_error_count_percent_change()',
  68. ],
  69. orderby: '-time_spent_percentage_local',
  70. dataset: DiscoverDatasets.SPANS_METRICS,
  71. version: 2,
  72. },
  73. location
  74. );
  75. if (sorts) {
  76. eventView.sorts = sorts;
  77. }
  78. return eventView;
  79. }