useTraceTimelineEvents.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {useMemo} from 'react';
  2. import type {Event} from 'sentry/types';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {getTraceTimeRangeFromEvent} from 'sentry/utils/performance/quickTrace/utils';
  5. import {useApiQuery} from 'sentry/utils/queryClient';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. interface BaseEvent {
  8. id: string;
  9. issue: string;
  10. 'issue.id': number;
  11. project: string;
  12. 'project.name': string;
  13. timestamp: string;
  14. title: string;
  15. transaction: string;
  16. }
  17. interface TimelineDiscoverEvent extends BaseEvent {}
  18. interface TimelineIssuePlatformEvent extends BaseEvent {
  19. 'event.type': string;
  20. 'stack.function': string[];
  21. }
  22. export type TimelineEvent = TimelineDiscoverEvent | TimelineIssuePlatformEvent;
  23. export interface TraceEventResponse {
  24. data: TimelineEvent[];
  25. meta: unknown;
  26. }
  27. interface UseTraceTimelineEventsOptions {
  28. event: Event;
  29. }
  30. export function useTraceTimelineEvents(
  31. {event}: UseTraceTimelineEventsOptions,
  32. isEnabled: boolean = true
  33. ) {
  34. const organization = useOrganization();
  35. const {start, end} = getTraceTimeRangeFromEvent(event);
  36. const traceId = event.contexts?.trace?.trace_id ?? '';
  37. const enabled = !!traceId && isEnabled;
  38. const {
  39. data: issuePlatformData,
  40. isLoading: isLoadingIssuePlatform,
  41. isError: isErrorIssuePlatform,
  42. } = useApiQuery<TraceEventResponse>(
  43. [
  44. `/organizations/${organization.slug}/events/`,
  45. {
  46. query: {
  47. // Get performance issues
  48. dataset: DiscoverDatasets.ISSUE_PLATFORM,
  49. field: ['title', 'project', 'timestamp', 'issue.id', 'issue', 'transaction'],
  50. per_page: 100,
  51. query: `trace:${traceId}`,
  52. referrer: 'api.issues.issue_events',
  53. sort: '-timestamp',
  54. start,
  55. end,
  56. },
  57. },
  58. ],
  59. {staleTime: Infinity, retry: false, enabled}
  60. );
  61. const {
  62. data: discoverData,
  63. isLoading: isLoadingDiscover,
  64. isError: isErrorDiscover,
  65. } = useApiQuery<{
  66. data: TimelineEvent[];
  67. meta: unknown;
  68. }>(
  69. [
  70. `/organizations/${organization.slug}/events/`,
  71. {
  72. query: {
  73. // Other events
  74. dataset: DiscoverDatasets.DISCOVER,
  75. field: [
  76. 'title',
  77. 'project',
  78. 'timestamp',
  79. 'issue.id',
  80. 'issue',
  81. 'transaction',
  82. 'event.type',
  83. 'stack.function',
  84. ],
  85. per_page: 100,
  86. query: `trace:${traceId}`,
  87. referrer: 'api.issues.issue_events',
  88. sort: '-timestamp',
  89. start,
  90. end,
  91. },
  92. },
  93. ],
  94. {staleTime: Infinity, retry: false, enabled}
  95. );
  96. const eventData = useMemo(() => {
  97. if (
  98. isLoadingIssuePlatform ||
  99. isLoadingDiscover ||
  100. isErrorIssuePlatform ||
  101. isErrorDiscover
  102. ) {
  103. return {
  104. data: [],
  105. startTimestamp: 0,
  106. endTimestamp: 0,
  107. };
  108. }
  109. const events = [...issuePlatformData.data, ...discoverData.data];
  110. const timestamps = events.map(e => new Date(e.timestamp).getTime());
  111. const startTimestamp = Math.min(...timestamps);
  112. const endTimestamp = Math.max(...timestamps);
  113. return {
  114. data: events,
  115. startTimestamp,
  116. endTimestamp,
  117. };
  118. }, [
  119. issuePlatformData,
  120. discoverData,
  121. isLoadingIssuePlatform,
  122. isLoadingDiscover,
  123. isErrorIssuePlatform,
  124. isErrorDiscover,
  125. ]);
  126. return {
  127. data: eventData.data,
  128. startTimestamp: eventData.startTimestamp,
  129. endTimestamp: eventData.endTimestamp,
  130. isLoading: isLoadingIssuePlatform || isLoadingDiscover,
  131. isError: isErrorIssuePlatform || isErrorDiscover,
  132. };
  133. }