useTraces.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  2. import type {PageFilters} from 'sentry/types/core';
  3. import type {QueryError} from 'sentry/utils/discover/genericDiscoverQuery';
  4. import {parseError} from 'sentry/utils/discover/genericDiscoverQuery';
  5. import type {DiscoverDatasets} from 'sentry/utils/discover/types';
  6. import type {UseApiQueryResult} from 'sentry/utils/queryClient';
  7. import {useApiQuery} from 'sentry/utils/queryClient';
  8. import type RequestError from 'sentry/utils/requestError/requestError';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. export const BREAKDOWN_SLICES = 40;
  12. interface TraceBreakdownBase {
  13. duration: number; // Contains the accurate duration for display. Start and end may be quantized.
  14. end: number;
  15. opCategory: string | null;
  16. sdkName: string | null;
  17. sliceEnd: number;
  18. sliceStart: number;
  19. sliceWidth: number;
  20. start: number;
  21. }
  22. type TraceBreakdownProject = TraceBreakdownBase & {
  23. kind: 'project';
  24. project: string;
  25. };
  26. type TraceBreakdownMissing = TraceBreakdownBase & {
  27. kind: 'missing';
  28. project: null;
  29. };
  30. export interface TraceResult {
  31. breakdowns: TraceBreakdownResult[];
  32. duration: number;
  33. end: number;
  34. matchingSpans: number;
  35. name: string | null;
  36. numErrors: number;
  37. numOccurrences: number;
  38. numSpans: number;
  39. project: string | null;
  40. rootDuration: number | null;
  41. slices: number;
  42. start: number;
  43. trace: string;
  44. }
  45. export type TraceBreakdownResult = TraceBreakdownProject | TraceBreakdownMissing;
  46. interface TraceResults {
  47. data: TraceResult[];
  48. meta: any;
  49. }
  50. interface UseTracesOptions {
  51. cursor?: string;
  52. dataset?: DiscoverDatasets;
  53. datetime?: PageFilters['datetime'];
  54. enabled?: boolean;
  55. limit?: number;
  56. query?: string | string[];
  57. sort?: 'timestamp' | '-timestamp';
  58. }
  59. type UseTracesResult = Omit<UseApiQueryResult<TraceResults, RequestError>, 'error'> & {
  60. error: QueryError | null;
  61. };
  62. export function useTraces({
  63. cursor,
  64. dataset,
  65. datetime,
  66. enabled,
  67. limit,
  68. query,
  69. sort,
  70. }: UseTracesOptions): UseTracesResult {
  71. const organization = useOrganization();
  72. const {selection} = usePageFilters();
  73. const path = `/organizations/${organization.slug}/traces/`;
  74. const endpointOptions = {
  75. query: {
  76. project: selection.projects,
  77. environment: selection.environments,
  78. ...normalizeDateTimeParams(datetime ?? selection.datetime),
  79. dataset,
  80. query,
  81. sort, // only has an effect when `dataset` is `EAPSpans`
  82. per_page: limit,
  83. cursor,
  84. breakdownSlices: BREAKDOWN_SLICES,
  85. },
  86. };
  87. const {error, ...rest} = useApiQuery<TraceResults>([path, endpointOptions], {
  88. staleTime: 0,
  89. refetchOnWindowFocus: false,
  90. refetchOnMount: false,
  91. retry: false,
  92. enabled,
  93. });
  94. return {
  95. ...rest,
  96. error: parseError(error),
  97. };
  98. }