useTraceSpans.tsx 1.7 KB

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