useSpanMetrics.tsx 2.0 KB

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