useSpanMetrics.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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;
  9. };
  10. export const useSpanMetrics = (
  11. span?: Pick<IndexedSpan, 'group'>,
  12. queryFilters: {transactionName?: string} = {},
  13. fields: string[] = [],
  14. referrer: string = 'span-metrics'
  15. ) => {
  16. const location = useLocation();
  17. const filters: string[] = [];
  18. if (queryFilters.transactionName) {
  19. filters.push(`transaction = ${queryFilters.transactionName}`);
  20. }
  21. const eventView = span
  22. ? getEventView(span, location, queryFilters.transactionName, fields)
  23. : undefined;
  24. // TODO: Add referrer
  25. const {isLoading, data} = useSpansQuery<SpanMetrics[]>({
  26. eventView,
  27. initialData: [],
  28. enabled: Boolean(span),
  29. referrer,
  30. });
  31. return {isLoading, data: data[0] ?? {}};
  32. };
  33. function getEventView(
  34. span: {group: string},
  35. location: Location,
  36. transaction?: string,
  37. fields: string[] = []
  38. ) {
  39. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  40. return EventView.fromNewQueryWithLocation(
  41. {
  42. name: '',
  43. query: `span.group:${cleanGroupId}${
  44. transaction ? ` transaction:${transaction}` : ''
  45. }`,
  46. fields,
  47. dataset: DiscoverDatasets.SPANS_METRICS,
  48. projects: [1],
  49. version: 2,
  50. },
  51. location
  52. );
  53. }