content.tsx 13 KB

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