trace.tsx 8.1 KB

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