trace.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. missing_instrumentation: true,
  87. autogroup: {
  88. parent: true,
  89. sibling: true,
  90. },
  91. layout: 'drawer bottom',
  92. list: {
  93. width: 0.5,
  94. },
  95. };
  96. function Trace({replay}: {replay: undefined | ReplayRecord}) {
  97. const organization = useOrganization();
  98. const {projects} = useProjects();
  99. const {
  100. state: {didInit, errors, isFetching, traces, orphanErrors},
  101. eventView,
  102. } = useTransactionData();
  103. useFetchTransactions();
  104. if (errors.length) {
  105. // Same style as <EmptyStateWarning>
  106. return (
  107. <BorderedSection>
  108. <EmptyState withIcon={false}>
  109. <IconSad legacySize="54px" />
  110. <p>{t('Unable to retrieve traces')}</p>
  111. </EmptyState>
  112. </BorderedSection>
  113. );
  114. }
  115. if (!replay || !didInit || (isFetching && !traces?.length) || !eventView) {
  116. // Show the blank screen until we start fetching, thats when you get a spinner
  117. return (
  118. <StyledPlaceholder height="100%">
  119. {isFetching ? <Loading /> : null}
  120. </StyledPlaceholder>
  121. );
  122. }
  123. const project = projects.find(p => p.id === replay.project_id);
  124. const hasPerformance = project?.firstTransactionEvent === true;
  125. const performanceActive =
  126. organization.features.includes('performance-view') && hasPerformance;
  127. if (!hasTraceData(traces, orphanErrors)) {
  128. return <TracesNotFound performanceActive={performanceActive} />;
  129. }
  130. return (
  131. <TraceFound
  132. performanceActive={performanceActive}
  133. organization={organization}
  134. eventView={eventView}
  135. traces={(traces as TraceTree.Transaction[]) ?? []}
  136. orphanErrors={orphanErrors}
  137. />
  138. );
  139. }
  140. export function NewTraceView({replay}: {replay: undefined | ReplayRecord}) {
  141. const organization = useOrganization();
  142. const {projects} = useProjects();
  143. const {eventView, indexComplete, indexError, replayTraces} = useReplayTraces({
  144. replayRecord: replay,
  145. });
  146. const firstTrace = replayTraces?.[0];
  147. const trace = useTrace({
  148. traceSlug: firstTrace?.traceSlug,
  149. timestamp: firstTrace?.timestamp,
  150. });
  151. const rootEvent = useTraceRootEvent(trace.data ?? null);
  152. const meta = useReplayTraceMeta(replay);
  153. const tree = useTraceTree({
  154. trace,
  155. meta,
  156. replay: replay ?? null,
  157. });
  158. const preferences = useMemo(
  159. () =>
  160. loadTraceViewPreferences('replay-trace-view-preferences') ||
  161. DEFAULT_REPLAY_TRACE_VIEW_PREFERENCES,
  162. []
  163. );
  164. const otherReplayTraces = useMemo(() => {
  165. if (!replayTraces) {
  166. return [];
  167. }
  168. return replayTraces.slice(1);
  169. }, [replayTraces]);
  170. if (indexError) {
  171. // Same style as <EmptyStateWarning>
  172. return (
  173. <BorderedSection>
  174. <EmptyState withIcon={false}>
  175. <IconSad legacySize="54px" />
  176. <p>{t('Unable to retrieve traces')}</p>
  177. </EmptyState>
  178. </BorderedSection>
  179. );
  180. }
  181. if (!replay || !indexComplete || !replayTraces || !eventView) {
  182. // Show the blank screen until we start fetching, thats when you get a spinner
  183. return (
  184. <StyledPlaceholder height="100%">
  185. {!indexComplete ? <Loading /> : null}
  186. </StyledPlaceholder>
  187. );
  188. }
  189. const project = projects.find(p => p.id === replay.project_id);
  190. const hasPerformance = project?.firstTransactionEvent === true;
  191. const performanceActive =
  192. organization.features.includes('performance-view') && hasPerformance;
  193. if (replayTraces.length === 0) {
  194. return <TracesNotFound performanceActive={performanceActive} />;
  195. }
  196. return (
  197. <TraceStateProvider
  198. initialPreferences={preferences}
  199. preferencesStorageKey="replay-trace-view-preferences"
  200. >
  201. <TraceViewWaterfallWrapper>
  202. <TraceViewWaterfall
  203. traceSlug={undefined}
  204. trace={trace}
  205. tree={tree}
  206. rootEvent={rootEvent}
  207. replayTraces={otherReplayTraces}
  208. organization={organization}
  209. traceEventView={eventView}
  210. meta={meta}
  211. source="replay"
  212. replay={replay}
  213. // Replays might want to enable this in the future
  214. isEmbedded={false}
  215. />
  216. </TraceViewWaterfallWrapper>
  217. </TraceStateProvider>
  218. );
  219. }
  220. // This has the gray background, to match other loaders on Replay Details
  221. const StyledPlaceholder = styled(Placeholder)`
  222. border: 1px solid ${p => p.theme.border};
  223. border-radius: ${p => p.theme.borderRadius};
  224. `;
  225. // White background, to match the loaded component
  226. const BorderedSection = styled(FluidHeight)`
  227. border: 1px solid ${p => p.theme.border};
  228. border-radius: ${p => p.theme.borderRadius};
  229. `;
  230. const OverflowScrollBorderedSection = styled(BorderedSection)`
  231. overflow: scroll;
  232. ${StyledTracePanel} {
  233. border: none;
  234. }
  235. `;
  236. const TraceViewWaterfallWrapper = styled('div')`
  237. display: flex;
  238. flex-direction: column;
  239. height: 100%;
  240. `;
  241. export default Trace;