useTraceSpans.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  2. import type {PageFilters} from 'sentry/types/core';
  3. import type {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {useApiQuery} from 'sentry/utils/queryClient';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import type {TraceResult} from './useTraces';
  8. export type SpanResult<F extends string> = Record<F, any>;
  9. export interface SpanResults<F extends string> {
  10. data: SpanResult<F>[];
  11. meta: any;
  12. }
  13. interface UseTraceSpansOptions<F extends string> {
  14. fields: F[];
  15. trace: TraceResult;
  16. dataset?: DiscoverDatasets;
  17. datetime?: PageFilters['datetime'];
  18. enabled?: boolean;
  19. limit?: number;
  20. query?: string | string[];
  21. sort?: string[];
  22. }
  23. export function useTraceSpans<F extends string>({
  24. fields,
  25. trace,
  26. dataset,
  27. datetime,
  28. enabled,
  29. limit,
  30. query,
  31. sort,
  32. }: UseTraceSpansOptions<F>) {
  33. const organization = useOrganization();
  34. const {selection} = usePageFilters();
  35. const path = `/organizations/${organization.slug}/trace/${trace.trace}/spans/`;
  36. const endpointOptions = {
  37. query: {
  38. project: selection.projects,
  39. environment: selection.environments,
  40. ...normalizeDateTimeParams(datetime ?? selection.datetime),
  41. dataset,
  42. field: fields,
  43. query,
  44. sort,
  45. per_page: limit,
  46. maxSpansPerTrace: 10,
  47. },
  48. };
  49. const result = useApiQuery<SpanResults<F>>([path, endpointOptions], {
  50. staleTime: 0,
  51. refetchOnWindowFocus: false,
  52. retry: false,
  53. enabled,
  54. });
  55. return result;
  56. }