useSpanTransactionMetrics.tsx 2.0 KB

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