useSpanMetrics.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  7. export type SpanMetrics = {
  8. [metric: string]: number | string;
  9. 'p95(span.self_time)': number;
  10. 'span.op': string;
  11. 'sps()': number;
  12. 'time_spent_percentage()': number;
  13. };
  14. export type SpanSummaryQueryFilters = {
  15. 'transaction.method'?: string;
  16. transactionName?: string;
  17. };
  18. export const useSpanMetrics = (
  19. span?: Pick<IndexedSpan, 'group'>,
  20. queryFilters: SpanSummaryQueryFilters = {},
  21. fields: string[] = [],
  22. referrer: string = 'span-metrics'
  23. ) => {
  24. const location = useLocation();
  25. const eventView = span ? getEventView(span, location, queryFilters, fields) : undefined;
  26. // TODO: Add referrer
  27. const result = useSpansQuery<SpanMetrics[]>({
  28. eventView,
  29. initialData: [],
  30. enabled: Boolean(span),
  31. referrer,
  32. });
  33. return {...result, data: result?.data[0] ?? {}};
  34. };
  35. function getEventView(
  36. span: {group: string},
  37. location: Location,
  38. queryFilters?: SpanSummaryQueryFilters,
  39. fields: string[] = []
  40. ) {
  41. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  42. return EventView.fromNewQueryWithLocation(
  43. {
  44. name: '',
  45. query: `span.group:${cleanGroupId}${
  46. queryFilters?.transactionName
  47. ? ` transaction:${queryFilters?.transactionName}`
  48. : ''
  49. }${
  50. queryFilters?.['transaction.method']
  51. ? ` transaction.method:${queryFilters?.['transaction.method']}`
  52. : ''
  53. }`,
  54. fields,
  55. dataset: DiscoverDatasets.SPANS_METRICS,
  56. projects: [1],
  57. version: 2,
  58. },
  59. location
  60. );
  61. }