trace.tsx 8.4 KB

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