trace.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 {useTraceRootEvent} from 'sentry/views/performance/newTraceDetails/traceApi/useTraceRootEvent';
  20. import type {TracePreferencesState} 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 {loadTraceViewPreferences} from '../../../performance/newTraceDetails/traceState/tracePreferences';
  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: TraceFullDetailed[] | 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. type Props = {
  91. replayRecord: undefined | ReplayRecord;
  92. };
  93. function Trace({replayRecord}: Props) {
  94. const organization = useOrganization();
  95. const {projects} = useProjects();
  96. const {
  97. state: {didInit, errors, isFetching, traces, orphanErrors},
  98. eventView,
  99. } = useTransactionData();
  100. const metaResults = useReplayTraceMeta(replayRecord);
  101. const preferences = useMemo(
  102. () =>
  103. loadTraceViewPreferences('replay-trace-view-preferences') ||
  104. DEFAULT_REPLAY_TRACE_VIEW_PREFERENCES,
  105. []
  106. );
  107. const traceSplitResults = useMemo(() => {
  108. return {
  109. transactions: traces ?? [],
  110. orphan_errors: orphanErrors ?? [],
  111. };
  112. }, [traces, orphanErrors]);
  113. const rootEvent = useTraceRootEvent(traceSplitResults);
  114. useFetchTransactions();
  115. if (errors.length) {
  116. // Same style as <EmptyStateWarning>
  117. return (
  118. <BorderedSection>
  119. <EmptyState withIcon={false}>
  120. <IconSad legacySize="54px" />
  121. <p>{t('Unable to retrieve traces')}</p>
  122. </EmptyState>
  123. </BorderedSection>
  124. );
  125. }
  126. if (!replayRecord || !didInit || (isFetching && !traces?.length) || !eventView) {
  127. // Show the blank screen until we start fetching, thats when you get a spinner
  128. return (
  129. <StyledPlaceholder height="100%">
  130. {isFetching ? <Loading /> : null}
  131. </StyledPlaceholder>
  132. );
  133. }
  134. const project = projects.find(p => p.id === replayRecord.project_id);
  135. const hasPerformance = project?.firstTransactionEvent === true;
  136. const performanceActive =
  137. organization.features.includes('performance-view') && hasPerformance;
  138. if (!hasTraceData(traces, orphanErrors)) {
  139. return <TracesNotFound performanceActive={performanceActive} />;
  140. }
  141. if (organization.features.includes('replay-trace-view-v1')) {
  142. return (
  143. <TraceStateProvider
  144. initialPreferences={preferences}
  145. preferencesStorageKey="replay-trace-view-preferences"
  146. >
  147. <TraceViewWaterfallWrapper>
  148. <TraceViewWaterfall
  149. traceSlug="Replay"
  150. status={errors.length > 0 ? 'error' : isFetching ? 'loading' : 'success'}
  151. trace={traceSplitResults}
  152. organization={organization}
  153. traceEventView={eventView}
  154. metaResults={metaResults}
  155. rootEvent={rootEvent}
  156. source="replay"
  157. replayRecord={replayRecord}
  158. />
  159. </TraceViewWaterfallWrapper>
  160. </TraceStateProvider>
  161. );
  162. }
  163. return (
  164. <TraceFound
  165. performanceActive={performanceActive}
  166. organization={organization}
  167. eventView={eventView}
  168. traces={traces ?? []}
  169. orphanErrors={orphanErrors}
  170. />
  171. );
  172. }
  173. // This has the gray background, to match other loaders on Replay Details
  174. const StyledPlaceholder = styled(Placeholder)`
  175. border: 1px solid ${p => p.theme.border};
  176. border-radius: ${p => p.theme.borderRadius};
  177. `;
  178. // White background, to match the loaded component
  179. const BorderedSection = styled(FluidHeight)`
  180. border: 1px solid ${p => p.theme.border};
  181. border-radius: ${p => p.theme.borderRadius};
  182. `;
  183. const OverflowScrollBorderedSection = styled(BorderedSection)`
  184. overflow: scroll;
  185. ${StyledTracePanel} {
  186. border: none;
  187. }
  188. `;
  189. const TraceViewWaterfallWrapper = styled('div')`
  190. display: flex;
  191. flex-direction: column;
  192. height: 100%;
  193. `;
  194. export default Trace;