content.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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, LinkButton} 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 {TransactionSearchQueryBuilder} from 'sentry/components/performance/transactionSearchQueryBuilder';
  17. import {
  18. ProfilingAM1OrMMXUpgrade,
  19. ProfilingBetaAlertBanner,
  20. ProfilingUpgradeButton,
  21. } from 'sentry/components/profiling/billing/alerts';
  22. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  23. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  24. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  25. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  26. import {TabList, Tabs} from 'sentry/components/tabs';
  27. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  28. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  29. import {t} from 'sentry/locale';
  30. import SidebarPanelStore from 'sentry/stores/sidebarPanelStore';
  31. import {space} from 'sentry/styles/space';
  32. import {trackAnalytics} from 'sentry/utils/analytics';
  33. import {browserHistory} from 'sentry/utils/browserHistory';
  34. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  35. import {formatError, formatSort} from 'sentry/utils/profiling/hooks/utils';
  36. import {decodeScalar} from 'sentry/utils/queryString';
  37. import {useLocation} from 'sentry/utils/useLocation';
  38. import useOrganization from 'sentry/utils/useOrganization';
  39. import usePageFilters from 'sentry/utils/usePageFilters';
  40. import useProjects from 'sentry/utils/useProjects';
  41. import {
  42. TableHeader,
  43. TableHeaderActions,
  44. TableHeaderTitle,
  45. } from 'sentry/views/explore/components/table';
  46. import {LandingAggregateFlamegraph} from 'sentry/views/profiling/landingAggregateFlamegraph';
  47. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  48. import {LandingWidgetSelector} from './landing/landingWidgetSelector';
  49. import {ProfilesChart} from './landing/profileCharts';
  50. import {ProfilesChartWidget} from './landing/profilesChartWidget';
  51. import {ProfilingSlowestTransactionsPanel} from './landing/profilingSlowestTransactionsPanel';
  52. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  53. const LEFT_WIDGET_CURSOR = 'leftCursor';
  54. const RIGHT_WIDGET_CURSOR = 'rightCursor';
  55. const CURSOR_PARAMS = [LEFT_WIDGET_CURSOR, RIGHT_WIDGET_CURSOR];
  56. interface ProfilingContentProps {
  57. location: Location;
  58. }
  59. function validateTab(tab: unknown): tab is 'flamegraph' | 'transactions' {
  60. return tab === 'flamegraph' || tab === 'transactions';
  61. }
  62. function decodeTab(tab: unknown): 'flamegraph' | 'transactions' {
  63. // Fallback to transactions if tab is invalid. We default to transactions
  64. // because that is going to be the most common perf setup when we release.
  65. return validateTab(tab) ? tab : 'transactions';
  66. }
  67. export default function ProfilingContent({location}: ProfilingContentProps) {
  68. const organization = useOrganization();
  69. const {selection} = usePageFilters();
  70. const {projects} = useProjects();
  71. const tab = decodeTab(location.query.tab);
  72. useEffect(() => {
  73. trackAnalytics('profiling_views.landing', {
  74. organization,
  75. });
  76. }, [organization]);
  77. const onTabChange = useCallback(
  78. (newTab: 'flamegraph' | 'transactions') => {
  79. browserHistory.push({
  80. ...location,
  81. query: {
  82. ...location.query,
  83. tab: newTab,
  84. },
  85. });
  86. },
  87. [location]
  88. );
  89. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  90. // if it's My Projects or All projects, only show onboarding if we can't
  91. // find any projects with profiles
  92. if (
  93. selection.projects.length === 0 ||
  94. selection.projects[0] === ALL_ACCESS_PROJECTS
  95. ) {
  96. return projects.every(project => !project.hasProfiles);
  97. }
  98. // otherwise, only show onboarding if we can't find any projects with profiles
  99. // from those that were selected
  100. const projectsWithProfiles = new Set(
  101. projects.filter(project => project.hasProfiles).map(project => project.id)
  102. );
  103. return selection.projects.every(
  104. project => !projectsWithProfiles.has(String(project))
  105. );
  106. }, [selection.projects, projects]);
  107. return (
  108. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  109. <PageFiltersContainer
  110. defaultSelection={{datetime: DEFAULT_PROFILING_DATETIME_SELECTION}}
  111. >
  112. <Layout.Page>
  113. <ProfilingBetaAlertBanner organization={organization} />
  114. <ProfilingContentPageHeader tab={tab} onTabChange={onTabChange} />
  115. {tab === 'flamegraph' ? (
  116. <FlamegraphBody>
  117. <ProfilingFlamegraphTabContent
  118. tab={tab}
  119. shouldShowProfilingOnboardingPanel={shouldShowProfilingOnboardingPanel}
  120. />
  121. </FlamegraphBody>
  122. ) : tab === 'transactions' ? (
  123. <Layout.Body>
  124. <ProfilingTransactionsContent
  125. tab={tab}
  126. shouldShowProfilingOnboardingPanel={shouldShowProfilingOnboardingPanel}
  127. />
  128. </Layout.Body>
  129. ) : null}
  130. </Layout.Page>
  131. </PageFiltersContainer>
  132. </SentryDocumentTitle>
  133. );
  134. }
  135. interface ProfilingTabContentProps {
  136. shouldShowProfilingOnboardingPanel: boolean;
  137. tab: 'flamegraph' | 'transactions';
  138. }
  139. function ProfilingFlamegraphTabContent(props: ProfilingTabContentProps) {
  140. return (
  141. <FlamegraphMainLayout>
  142. <FlamegraphActionBar>
  143. <PageFilterBar condensed>
  144. <ProjectPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  145. <EnvironmentPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  146. <DatePageFilter resetParamsOnChange={CURSOR_PARAMS} />
  147. </PageFilterBar>
  148. </FlamegraphActionBar>
  149. <FlamegraphLayout>
  150. {props.shouldShowProfilingOnboardingPanel ? (
  151. <ProfilingOnboardingCTA />
  152. ) : (
  153. <LandingAggregateFlamegraphContainer>
  154. <LandingAggregateFlamegraph />
  155. </LandingAggregateFlamegraphContainer>
  156. )}
  157. <FlamegraphSidebar />
  158. </FlamegraphLayout>
  159. </FlamegraphMainLayout>
  160. );
  161. }
  162. function ProfilingTransactionsContent(props: ProfilingTabContentProps) {
  163. const organization = useOrganization();
  164. const location = useLocation();
  165. const {selection} = usePageFilters();
  166. const fields = ALL_FIELDS;
  167. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), fields, {
  168. key: 'count()',
  169. order: 'desc',
  170. });
  171. const cursor = decodeScalar(location.query.cursor);
  172. const query = decodeScalar(location.query.query, '');
  173. const transactions = useProfileEvents<FieldType>({
  174. cursor,
  175. fields,
  176. query,
  177. sort,
  178. referrer: 'api.profiling.landing-table',
  179. });
  180. const transactionsError =
  181. transactions.status === 'error' ? formatError(transactions.error) : null;
  182. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  183. (searchQuery: string) => {
  184. browserHistory.push({
  185. ...location,
  186. query: {
  187. ...location.query,
  188. cursor: undefined,
  189. query: searchQuery || undefined,
  190. },
  191. });
  192. },
  193. [location]
  194. );
  195. return (
  196. <Layout.Main fullWidth>
  197. {transactionsError && (
  198. <Alert type="error" showIcon>
  199. {transactionsError}
  200. </Alert>
  201. )}
  202. <ActionBar>
  203. <PageFilterBar condensed>
  204. <ProjectPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  205. <EnvironmentPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  206. <DatePageFilter resetParamsOnChange={CURSOR_PARAMS} />
  207. </PageFilterBar>
  208. {organization.features.includes('search-query-builder-performance') ? (
  209. <TransactionSearchQueryBuilder
  210. projects={selection.projects}
  211. initialQuery={query}
  212. onSearch={handleSearch}
  213. searchSource="profile_landing"
  214. />
  215. ) : (
  216. <SearchBar
  217. searchSource="profile_landing"
  218. organization={organization}
  219. projectIds={selection.projects}
  220. query={query}
  221. onSearch={handleSearch}
  222. maxQueryLength={MAX_QUERY_LENGTH}
  223. />
  224. )}
  225. </ActionBar>
  226. {props.shouldShowProfilingOnboardingPanel ? (
  227. <ProfilingOnboardingCTA />
  228. ) : (
  229. <Fragment>
  230. {organization.features.includes('profiling-global-suspect-functions') ? (
  231. <Fragment>
  232. <ProfilesChartWidget
  233. chartHeight={150}
  234. referrer="api.profiling.landing-chart"
  235. userQuery={query}
  236. selection={selection}
  237. />
  238. <WidgetsContainer>
  239. <LandingWidgetSelector
  240. cursorName={LEFT_WIDGET_CURSOR}
  241. widgetHeight="340px"
  242. defaultWidget="slowest functions"
  243. query={query}
  244. storageKey="profiling-landing-widget-0"
  245. />
  246. <LandingWidgetSelector
  247. cursorName={RIGHT_WIDGET_CURSOR}
  248. widgetHeight="340px"
  249. defaultWidget="regressed functions"
  250. query={query}
  251. storageKey="profiling-landing-widget-1"
  252. />
  253. </WidgetsContainer>
  254. </Fragment>
  255. ) : (
  256. <PanelsGrid>
  257. <ProfilingSlowestTransactionsPanel />
  258. <ProfilesChart
  259. referrer="api.profiling.landing-chart"
  260. query={query}
  261. selection={selection}
  262. hideCount
  263. />
  264. </PanelsGrid>
  265. )}
  266. <Fragment>
  267. <TableHeader>
  268. <TableHeaderTitle>{t('Transactions')}</TableHeaderTitle>
  269. <TableHeaderActions>
  270. <StyledPagination
  271. pageLinks={
  272. transactions.status === 'success'
  273. ? transactions.getResponseHeader?.('Link') ?? null
  274. : null
  275. }
  276. />
  277. </TableHeaderActions>
  278. </TableHeader>
  279. <ProfileEventsTable
  280. columns={fields.slice()}
  281. data={transactions.status === 'success' ? transactions.data : null}
  282. error={
  283. transactions.status === 'error' ? t('Unable to load profiles') : null
  284. }
  285. isLoading={transactions.status === 'pending'}
  286. sort={sort}
  287. sortableColumns={new Set(fields)}
  288. />
  289. </Fragment>
  290. </Fragment>
  291. )}
  292. </Layout.Main>
  293. );
  294. }
  295. function ProfilingOnboardingCTA() {
  296. const organization = useOrganization();
  297. // Open the modal on demand
  298. const onSetupProfilingClick = useCallback(() => {
  299. trackAnalytics('profiling_views.onboarding', {
  300. organization,
  301. });
  302. SidebarPanelStore.activatePanel(SidebarPanelKey.PROFILING_ONBOARDING);
  303. }, [organization]);
  304. return (
  305. <Fragment>
  306. <ProfilingOnboardingPanel
  307. content={
  308. // If user is on m2, show default
  309. <ProfilingAM1OrMMXUpgrade
  310. organization={organization}
  311. fallback={
  312. <Fragment>
  313. <h3>{t('Function level insights')}</h3>
  314. <p>
  315. {t(
  316. 'Discover slow-to-execute or resource intensive functions within your application'
  317. )}
  318. </p>
  319. </Fragment>
  320. }
  321. />
  322. }
  323. >
  324. <ProfilingUpgradeButton
  325. data-test-id="profiling-upgrade"
  326. organization={organization}
  327. priority="primary"
  328. onClick={onSetupProfilingClick}
  329. fallback={
  330. <Button onClick={onSetupProfilingClick} priority="primary">
  331. {t('Set Up Profiling')}
  332. </Button>
  333. }
  334. >
  335. {t('Set Up Profiling')}
  336. </ProfilingUpgradeButton>
  337. <LinkButton href="https://docs.sentry.io/product/profiling/" external>
  338. {t('Read Docs')}
  339. </LinkButton>
  340. </ProfilingOnboardingPanel>
  341. </Fragment>
  342. );
  343. }
  344. interface ProfilingContentPageHeaderProps {
  345. onTabChange: (newTab: 'flamegraph' | 'transactions') => void;
  346. tab: 'flamegraph' | 'transactions';
  347. }
  348. function ProfilingContentPageHeader(props: ProfilingContentPageHeaderProps) {
  349. return (
  350. <StyledLayoutHeader>
  351. <StyledHeaderContent>
  352. <Layout.Title>
  353. {t('Profiling')}
  354. <PageHeadingQuestionTooltip
  355. docsUrl="https://docs.sentry.io/product/profiling/"
  356. title={t(
  357. '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.'
  358. )}
  359. />
  360. </Layout.Title>
  361. <FeedbackWidgetButton />
  362. </StyledHeaderContent>
  363. <div>
  364. <Tabs value={props.tab} onChange={props.onTabChange}>
  365. <TabList hideBorder>
  366. <TabList.Item key="transactions">{t('Transactions')}</TabList.Item>
  367. <TabList.Item key="flamegraph">{t('Flamegraph')}</TabList.Item>
  368. </TabList>
  369. </Tabs>
  370. </div>
  371. </StyledLayoutHeader>
  372. );
  373. }
  374. const ALL_FIELDS = [
  375. 'transaction',
  376. 'project.id',
  377. 'last_seen()',
  378. 'p50()',
  379. 'p75()',
  380. 'p95()',
  381. 'p99()',
  382. 'count()',
  383. ] as const;
  384. type FieldType = (typeof ALL_FIELDS)[number];
  385. const FlamegraphBody = styled(Layout.Body)`
  386. display: grid;
  387. grid-template-rows: 1fr;
  388. `;
  389. const FlamegraphMainLayout = styled(Layout.Main)`
  390. display: grid;
  391. grid-column: 1 / -1;
  392. grid-template-rows: min-content 1fr;
  393. `;
  394. const FlamegraphLayout = styled('div')`
  395. display: grid;
  396. grid-template-areas: 'flamegraph sidebar';
  397. grid-template-columns: 1fr min-content;
  398. margin-top: ${space(2)};
  399. `;
  400. const FlamegraphActionBar = styled('div')``;
  401. const FlamegraphSidebar = styled('div')`
  402. grid-area: sidebar;
  403. `;
  404. const LandingAggregateFlamegraphContainer = styled('div')`
  405. height: 100%;
  406. min-height: 300px;
  407. position: relative;
  408. border: 1px solid ${p => p.theme.border};
  409. border-radius: ${p => p.theme.borderRadius};
  410. margin-bottom: ${space(2)};
  411. grid-area: flamegraph;
  412. `;
  413. const StyledLayoutHeader = styled(Layout.Header)`
  414. display: block;
  415. `;
  416. const StyledHeaderContent = styled(Layout.HeaderContent)`
  417. display: flex;
  418. align-items: center;
  419. justify-content: space-between;
  420. flex-direction: row;
  421. `;
  422. const ActionBar = styled('div')`
  423. display: grid;
  424. gap: ${space(2)};
  425. grid-template-columns: min-content auto;
  426. margin-bottom: ${space(2)};
  427. `;
  428. // TODO: another simple primitive that can easily be <Grid columns={2} />
  429. const PanelsGrid = styled('div')`
  430. display: grid;
  431. grid-template-columns: minmax(0, 1fr) 1fr;
  432. gap: ${space(2)};
  433. @media (max-width: ${p => p.theme.breakpoints.small}) {
  434. grid-template-columns: minmax(0, 1fr);
  435. }
  436. `;
  437. const WidgetsContainer = styled('div')`
  438. display: grid;
  439. grid-template-columns: 1fr 1fr;
  440. gap: ${space(2)};
  441. @media (max-width: ${p => p.theme.breakpoints.small}) {
  442. grid-template-columns: 1fr;
  443. }
  444. `;
  445. const StyledPagination = styled(Pagination)`
  446. margin: 0;
  447. `;