trace.tsx 8.3 KB

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