content.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import {Fragment, useCallback, useEffect, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Alert} from 'sentry/components/alert';
  5. import {Button} from 'sentry/components/button';
  6. import SearchBar from 'sentry/components/events/searchBar';
  7. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  10. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  13. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  14. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  15. import Pagination from 'sentry/components/pagination';
  16. import {
  17. ProfilingAM1OrMMXUpgrade,
  18. ProfilingBetaAlertBanner,
  19. ProfilingUpgradeButton,
  20. } from 'sentry/components/profiling/billing/alerts';
  21. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  22. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  23. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  24. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  25. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  26. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  27. import {t} from 'sentry/locale';
  28. import SidebarPanelStore from 'sentry/stores/sidebarPanelStore';
  29. import {space} from 'sentry/styles/space';
  30. import {trackAnalytics} from 'sentry/utils/analytics';
  31. import {browserHistory} from 'sentry/utils/browserHistory';
  32. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  33. import {formatError, formatSort} from 'sentry/utils/profiling/hooks/utils';
  34. import {decodeScalar} from 'sentry/utils/queryString';
  35. import useOrganization from 'sentry/utils/useOrganization';
  36. import usePageFilters from 'sentry/utils/usePageFilters';
  37. import useProjects from 'sentry/utils/useProjects';
  38. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  39. import {LandingWidgetSelector} from './landing/landingWidgetSelector';
  40. import {ProfilesChart} from './landing/profileCharts';
  41. import {ProfilesChartWidget} from './landing/profilesChartWidget';
  42. import {ProfilingSlowestTransactionsPanel} from './landing/profilingSlowestTransactionsPanel';
  43. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  44. const LEFT_WIDGET_CURSOR = 'leftCursor';
  45. const RIGHT_WIDGET_CURSOR = 'rightCursor';
  46. const CURSOR_PARAMS = [LEFT_WIDGET_CURSOR, RIGHT_WIDGET_CURSOR];
  47. interface ProfilingContentProps {
  48. location: Location;
  49. }
  50. function ProfilingContent({location}: ProfilingContentProps) {
  51. const organization = useOrganization();
  52. const {selection} = usePageFilters();
  53. const cursor = decodeScalar(location.query.cursor);
  54. const query = decodeScalar(location.query.query, '');
  55. const fields = ALL_FIELDS;
  56. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), fields, {
  57. key: 'count()',
  58. order: 'desc',
  59. });
  60. const {projects} = useProjects();
  61. const transactions = useProfileEvents<FieldType>({
  62. cursor,
  63. fields,
  64. query,
  65. sort,
  66. referrer: 'api.profiling.landing-table',
  67. });
  68. const transactionsError =
  69. transactions.status === 'error' ? formatError(transactions.error) : null;
  70. useEffect(() => {
  71. trackAnalytics('profiling_views.landing', {
  72. organization,
  73. });
  74. }, [organization]);
  75. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  76. (searchQuery: string) => {
  77. browserHistory.push({
  78. ...location,
  79. query: {
  80. ...location.query,
  81. cursor: undefined,
  82. query: searchQuery || undefined,
  83. },
  84. });
  85. },
  86. [location]
  87. );
  88. // Open the modal on demand
  89. const onSetupProfilingClick = useCallback(() => {
  90. trackAnalytics('profiling_views.onboarding', {
  91. organization,
  92. });
  93. SidebarPanelStore.activatePanel(SidebarPanelKey.PROFILING_ONBOARDING);
  94. }, [organization]);
  95. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  96. // if it's My Projects or All projects, only show onboarding if we can't
  97. // find any projects with profiles
  98. if (
  99. selection.projects.length === 0 ||
  100. selection.projects[0] === ALL_ACCESS_PROJECTS
  101. ) {
  102. return projects.every(project => !project.hasProfiles);
  103. }
  104. // otherwise, only show onboarding if we can't find any projects with profiles
  105. // from those that were selected
  106. const projectsWithProfiles = new Set(
  107. projects.filter(project => project.hasProfiles).map(project => project.id)
  108. );
  109. return selection.projects.every(
  110. project => !projectsWithProfiles.has(String(project))
  111. );
  112. }, [selection.projects, projects]);
  113. return (
  114. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  115. <PageFiltersContainer
  116. defaultSelection={{datetime: DEFAULT_PROFILING_DATETIME_SELECTION}}
  117. >
  118. <Layout.Page>
  119. <ProfilingBetaAlertBanner organization={organization} />
  120. <Layout.Header>
  121. <StyledHeaderContent>
  122. <Layout.Title>
  123. {t('Profiling')}
  124. <PageHeadingQuestionTooltip
  125. docsUrl="https://docs.sentry.io/product/profiling/"
  126. title={t(
  127. 'Profiling collects detailed information in production about the functions executing in your application and how long they take to run, giving you code-level visibility into your hot paths.'
  128. )}
  129. />
  130. </Layout.Title>
  131. <FeedbackWidgetButton />
  132. </StyledHeaderContent>
  133. </Layout.Header>
  134. <Layout.Body>
  135. <Layout.Main fullWidth>
  136. {transactionsError && (
  137. <Alert type="error" showIcon>
  138. {transactionsError}
  139. </Alert>
  140. )}
  141. <ActionBar>
  142. <PageFilterBar condensed>
  143. <ProjectPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  144. <EnvironmentPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  145. <DatePageFilter resetParamsOnChange={CURSOR_PARAMS} />
  146. </PageFilterBar>
  147. <SearchBar
  148. searchSource="profile_landing"
  149. organization={organization}
  150. projectIds={selection.projects}
  151. query={query}
  152. onSearch={handleSearch}
  153. maxQueryLength={MAX_QUERY_LENGTH}
  154. />
  155. </ActionBar>
  156. {shouldShowProfilingOnboardingPanel ? (
  157. <Fragment>
  158. <ProfilingOnboardingPanel
  159. content={
  160. // If user is on m2, show default
  161. <ProfilingAM1OrMMXUpgrade
  162. organization={organization}
  163. fallback={
  164. <Fragment>
  165. <h3>{t('Function level insights')}</h3>
  166. <p>
  167. {t(
  168. 'Discover slow-to-execute or resource intensive functions within your application'
  169. )}
  170. </p>
  171. </Fragment>
  172. }
  173. />
  174. }
  175. >
  176. <ProfilingUpgradeButton
  177. data-test-id="profiling-upgrade"
  178. organization={organization}
  179. priority="primary"
  180. onClick={onSetupProfilingClick}
  181. fallback={
  182. <Button onClick={onSetupProfilingClick} priority="primary">
  183. {t('Set Up Profiling')}
  184. </Button>
  185. }
  186. >
  187. {t('Set Up Profiling')}
  188. </ProfilingUpgradeButton>
  189. <Button href="https://docs.sentry.io/product/profiling/" external>
  190. {t('Read Docs')}
  191. </Button>
  192. </ProfilingOnboardingPanel>
  193. </Fragment>
  194. ) : (
  195. <Fragment>
  196. {organization.features.includes(
  197. 'profiling-global-suspect-functions'
  198. ) ? (
  199. <Fragment>
  200. {organization.features.includes(
  201. 'continuous-profiling-ui'
  202. ) ? null : (
  203. <ProfilesChartWidget
  204. chartHeight={150}
  205. referrer="api.profiling.landing-chart"
  206. userQuery={query}
  207. selection={selection}
  208. />
  209. )}
  210. <WidgetsContainer>
  211. <LandingWidgetSelector
  212. cursorName={LEFT_WIDGET_CURSOR}
  213. widgetHeight="340px"
  214. defaultWidget="slowest functions"
  215. query={query}
  216. storageKey="profiling-landing-widget-0"
  217. />
  218. <LandingWidgetSelector
  219. cursorName={RIGHT_WIDGET_CURSOR}
  220. widgetHeight="340px"
  221. defaultWidget="regressed functions"
  222. query={query}
  223. storageKey="profiling-landing-widget-1"
  224. />
  225. </WidgetsContainer>
  226. </Fragment>
  227. ) : (
  228. <PanelsGrid>
  229. <ProfilingSlowestTransactionsPanel />
  230. <ProfilesChart
  231. referrer="api.profiling.landing-chart"
  232. query={query}
  233. selection={selection}
  234. hideCount
  235. />
  236. </PanelsGrid>
  237. )}
  238. <ProfileEventsTable
  239. columns={fields.slice()}
  240. data={transactions.status === 'success' ? transactions.data : null}
  241. error={
  242. transactions.status === 'error'
  243. ? t('Unable to load profiles')
  244. : null
  245. }
  246. isLoading={transactions.status === 'loading'}
  247. sort={sort}
  248. sortableColumns={new Set(fields)}
  249. />
  250. <Pagination
  251. pageLinks={
  252. transactions.status === 'success'
  253. ? transactions.getResponseHeader?.('Link') ?? null
  254. : null
  255. }
  256. />
  257. </Fragment>
  258. )}
  259. </Layout.Main>
  260. </Layout.Body>
  261. </Layout.Page>
  262. </PageFiltersContainer>
  263. </SentryDocumentTitle>
  264. );
  265. }
  266. const ALL_FIELDS = [
  267. 'transaction',
  268. 'project.id',
  269. 'last_seen()',
  270. 'p50()',
  271. 'p75()',
  272. 'p95()',
  273. 'p99()',
  274. 'count()',
  275. ] as const;
  276. type FieldType = (typeof ALL_FIELDS)[number];
  277. const StyledHeaderContent = styled(Layout.HeaderContent)`
  278. display: flex;
  279. align-items: center;
  280. justify-content: space-between;
  281. flex-direction: row;
  282. `;
  283. const ActionBar = styled('div')`
  284. display: grid;
  285. gap: ${space(2)};
  286. grid-template-columns: min-content auto;
  287. margin-bottom: ${space(2)};
  288. `;
  289. // TODO: another simple primitive that can easily be <Grid columns={2} />
  290. const PanelsGrid = styled('div')`
  291. display: grid;
  292. grid-template-columns: minmax(0, 1fr) 1fr;
  293. gap: ${space(2)};
  294. @media (max-width: ${p => p.theme.breakpoints.small}) {
  295. grid-template-columns: minmax(0, 1fr);
  296. }
  297. `;
  298. const WidgetsContainer = styled('div')`
  299. display: grid;
  300. grid-template-columns: 1fr 1fr;
  301. gap: ${space(2)};
  302. @media (max-width: ${p => p.theme.breakpoints.small}) {
  303. grid-template-columns: 1fr;
  304. }
  305. `;
  306. export default ProfilingContent;