trace.tsx 8.0 KB

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