trace.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import Loading from 'sentry/components/loadingIndicator';
  4. import Placeholder from 'sentry/components/placeholder';
  5. import {IconSad} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import type {Organization} from 'sentry/types/organization';
  8. import type EventView from 'sentry/utils/discover/eventView';
  9. import type {TraceError} from 'sentry/utils/performance/quickTrace/types';
  10. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  11. import {useLocation} from 'sentry/utils/useLocation';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import useProjects from 'sentry/utils/useProjects';
  14. import {
  15. getTraceViewQueryStatus,
  16. TraceViewWaterfall,
  17. } from 'sentry/views/performance/newTraceDetails';
  18. import {useReplayTraceMeta} from 'sentry/views/performance/newTraceDetails/traceApi/useReplayTraceMeta';
  19. import {useTrace} from 'sentry/views/performance/newTraceDetails/traceApi/useTrace';
  20. import {useTraceRootEvent} from 'sentry/views/performance/newTraceDetails/traceApi/useTraceRootEvent';
  21. import type {TraceTree} from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  22. import type {TracePreferencesState} from 'sentry/views/performance/newTraceDetails/traceState/tracePreferences';
  23. import {loadTraceViewPreferences} from 'sentry/views/performance/newTraceDetails/traceState/tracePreferences';
  24. import {TraceStateProvider} from 'sentry/views/performance/newTraceDetails/traceState/traceStateProvider';
  25. import TraceView, {
  26. StyledTracePanel,
  27. } from 'sentry/views/performance/traceDetails/traceView';
  28. import {hasTraceData} from 'sentry/views/performance/traceDetails/utils';
  29. import EmptyState from 'sentry/views/replays/detail/emptyState';
  30. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  31. import {
  32. useFetchTransactions,
  33. useTransactionData,
  34. } from 'sentry/views/replays/detail/trace/replayTransactionContext';
  35. import type {ReplayRecord} from 'sentry/views/replays/types';
  36. import {useReplayTraces} from './useReplayTraces';
  37. function TracesNotFound({performanceActive}: {performanceActive: boolean}) {
  38. // We want to send the 'trace_status' data if the project actively uses and has access to the performance monitoring.
  39. useRouteAnalyticsParams(performanceActive ? {trace_status: 'trace missing'} : {});
  40. return (
  41. <BorderedSection data-test-id="replay-details-trace-tab">
  42. <EmptyState data-test-id="empty-state">
  43. <p>{t('No traces found')}</p>
  44. </EmptyState>
  45. </BorderedSection>
  46. );
  47. }
  48. function TraceFound({
  49. organization,
  50. performanceActive,
  51. eventView,
  52. traces,
  53. orphanErrors,
  54. }: {
  55. eventView: EventView | null;
  56. organization: Organization;
  57. performanceActive: boolean;
  58. traces: TraceTree.Transaction[] | null;
  59. orphanErrors?: TraceError[];
  60. }) {
  61. const location = useLocation();
  62. // We want to send the 'trace_status' data if the project actively uses and has access to the performance monitoring.
  63. useRouteAnalyticsParams(performanceActive ? {trace_status: 'success'} : {});
  64. return (
  65. <OverflowScrollBorderedSection>
  66. <TraceView
  67. meta={null}
  68. traces={traces || []}
  69. location={location}
  70. organization={organization}
  71. traceEventView={eventView!}
  72. traceSlug="Replay"
  73. orphanErrors={orphanErrors}
  74. />
  75. </OverflowScrollBorderedSection>
  76. );
  77. }
  78. const DEFAULT_REPLAY_TRACE_VIEW_PREFERENCES: TracePreferencesState = {
  79. drawer: {
  80. minimized: false,
  81. sizes: {
  82. 'drawer left': 0.33,
  83. 'drawer right': 0.33,
  84. 'drawer bottom': 0.4,
  85. },
  86. layoutOptions: [],
  87. },
  88. layout: 'drawer bottom',
  89. list: {
  90. width: 0.5,
  91. },
  92. };
  93. function Trace({replayRecord}: {replayRecord: undefined | ReplayRecord}) {
  94. const organization = useOrganization();
  95. const {projects} = useProjects();
  96. const {
  97. state: {didInit, errors, isFetching, traces, orphanErrors},
  98. eventView,
  99. } = useTransactionData();
  100. useFetchTransactions();
  101. if (errors.length) {
  102. // Same style as <EmptyStateWarning>
  103. return (
  104. <BorderedSection>
  105. <EmptyState withIcon={false}>
  106. <IconSad legacySize="54px" />
  107. <p>{t('Unable to retrieve traces')}</p>
  108. </EmptyState>
  109. </BorderedSection>
  110. );
  111. }
  112. if (!replayRecord || !didInit || (isFetching && !traces?.length) || !eventView) {
  113. // Show the blank screen until we start fetching, thats when you get a spinner
  114. return (
  115. <StyledPlaceholder height="100%">
  116. {isFetching ? <Loading /> : null}
  117. </StyledPlaceholder>
  118. );
  119. }
  120. const project = projects.find(p => p.id === replayRecord.project_id);
  121. const hasPerformance = project?.firstTransactionEvent === true;
  122. const performanceActive =
  123. organization.features.includes('performance-view') && hasPerformance;
  124. if (!hasTraceData(traces, orphanErrors)) {
  125. return <TracesNotFound performanceActive={performanceActive} />;
  126. }
  127. return (
  128. <TraceFound
  129. performanceActive={performanceActive}
  130. organization={organization}
  131. eventView={eventView}
  132. traces={(traces as TraceTree.Transaction[]) ?? []}
  133. orphanErrors={orphanErrors}
  134. />
  135. );
  136. }
  137. export function NewTraceView({replayRecord}: {replayRecord: undefined | ReplayRecord}) {
  138. const organization = useOrganization();
  139. const {projects} = useProjects();
  140. const {eventView, indexComplete, indexError, replayTraces} = useReplayTraces({
  141. replayRecord,
  142. });
  143. const firstTrace = replayTraces?.[0];
  144. const trace = useTrace({
  145. traceSlug: firstTrace?.traceSlug,
  146. timestamp: firstTrace?.timestamp,
  147. });
  148. const rootEvent = useTraceRootEvent(trace.data ?? null);
  149. const metaResults = useReplayTraceMeta(replayRecord);
  150. const preferences = useMemo(
  151. () =>
  152. loadTraceViewPreferences('replay-trace-view-preferences') ||
  153. DEFAULT_REPLAY_TRACE_VIEW_PREFERENCES,
  154. []
  155. );
  156. const otherReplayTraces = useMemo(() => {
  157. if (!replayTraces) {
  158. return [];
  159. }
  160. return replayTraces.slice(1);
  161. }, [replayTraces]);
  162. if (indexError) {
  163. // Same style as <EmptyStateWarning>
  164. return (
  165. <BorderedSection>
  166. <EmptyState withIcon={false}>
  167. <IconSad legacySize="54px" />
  168. <p>{t('Unable to retrieve traces')}</p>
  169. </EmptyState>
  170. </BorderedSection>
  171. );
  172. }
  173. if (!replayRecord || !indexComplete || !replayTraces || !eventView) {
  174. // Show the blank screen until we start fetching, thats when you get a spinner
  175. return (
  176. <StyledPlaceholder height="100%">
  177. {!indexComplete ? <Loading /> : null}
  178. </StyledPlaceholder>
  179. );
  180. }
  181. const project = projects.find(p => p.id === replayRecord.project_id);
  182. const hasPerformance = project?.firstTransactionEvent === true;
  183. const performanceActive =
  184. organization.features.includes('performance-view') && hasPerformance;
  185. if (replayTraces.length === 0) {
  186. return <TracesNotFound performanceActive={performanceActive} />;
  187. }
  188. return (
  189. <TraceStateProvider
  190. initialPreferences={preferences}
  191. preferencesStorageKey="replay-trace-view-preferences"
  192. >
  193. <TraceViewWaterfallWrapper>
  194. <TraceViewWaterfall
  195. traceSlug={undefined}
  196. trace={trace.data ?? null}
  197. status={getTraceViewQueryStatus(trace.status, metaResults.status)}
  198. rootEvent={rootEvent}
  199. replayTraces={otherReplayTraces}
  200. organization={organization}
  201. traceEventView={eventView}
  202. metaResults={metaResults}
  203. source="replay"
  204. replayRecord={replayRecord}
  205. />
  206. </TraceViewWaterfallWrapper>
  207. </TraceStateProvider>
  208. );
  209. }
  210. // This has the gray background, to match other loaders on Replay Details
  211. const StyledPlaceholder = styled(Placeholder)`
  212. border: 1px solid ${p => p.theme.border};
  213. border-radius: ${p => p.theme.borderRadius};
  214. `;
  215. // White background, to match the loaded component
  216. const BorderedSection = styled(FluidHeight)`
  217. border: 1px solid ${p => p.theme.border};
  218. border-radius: ${p => p.theme.borderRadius};
  219. `;
  220. const OverflowScrollBorderedSection = styled(BorderedSection)`
  221. overflow: scroll;
  222. ${StyledTracePanel} {
  223. border: none;
  224. }
  225. `;
  226. const TraceViewWaterfallWrapper = styled('div')`
  227. display: flex;
  228. flex-direction: column;
  229. height: 100%;
  230. `;
  231. export default Trace;