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