content.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 type {SmartSearchBarProps} from 'sentry/components/deprecatedSmartSearchBar';
  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 QuestionTooltip from 'sentry/components/questionTooltip';
  24. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  25. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  26. import {TabList, Tabs} from 'sentry/components/tabs';
  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 type {PageFilters} from 'sentry/types/core';
  32. import type {Project} from 'sentry/types/project';
  33. import {trackAnalytics} from 'sentry/utils/analytics';
  34. import {browserHistory} from 'sentry/utils/browserHistory';
  35. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  36. import {formatError, formatSort} from 'sentry/utils/profiling/hooks/utils';
  37. import {decodeScalar} from 'sentry/utils/queryString';
  38. import useOrganization from 'sentry/utils/useOrganization';
  39. import usePageFilters from 'sentry/utils/usePageFilters';
  40. import useProjects from 'sentry/utils/useProjects';
  41. import {LandingAggregateFlamegraph} from 'sentry/views/profiling/landingAggregateFlamegraph';
  42. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  43. import {LandingWidgetSelector} from './landing/landingWidgetSelector';
  44. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  45. const LEFT_WIDGET_CURSOR = 'leftCursor';
  46. const RIGHT_WIDGET_CURSOR = 'rightCursor';
  47. const CURSOR_PARAMS = [LEFT_WIDGET_CURSOR, RIGHT_WIDGET_CURSOR];
  48. interface ProfilingContentProps {
  49. location: Location;
  50. }
  51. function validateTab(tab: unknown): tab is 'flamegraph' | 'transactions' {
  52. return tab === 'flamegraph' || tab === 'transactions';
  53. }
  54. function decodeTab(tab: unknown): 'flamegraph' | 'transactions' {
  55. // Fallback to transactions if tab is invalid. We default to transactions
  56. // because that is going to be the most common perf setup when we release.
  57. return validateTab(tab) ? tab : 'transactions';
  58. }
  59. export default function ProfilingContent({location}: ProfilingContentProps) {
  60. const {selection} = usePageFilters();
  61. const organization = useOrganization();
  62. const {projects} = useProjects();
  63. useEffect(() => {
  64. trackAnalytics('profiling_views.landing', {
  65. organization,
  66. });
  67. }, [organization]);
  68. const showOnboardingPanel = useMemo(() => {
  69. return shouldShowProfilingOnboardingPanel(selection, projects);
  70. }, [selection, projects]);
  71. const tab = decodeTab(location.query.tab);
  72. const onTabChange = useCallback(
  73. (newTab: 'flamegraph' | 'transactions') => {
  74. browserHistory.push({
  75. ...location,
  76. query: {
  77. ...location.query,
  78. tab: newTab,
  79. },
  80. });
  81. },
  82. [location]
  83. );
  84. return (
  85. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  86. <PageFiltersContainer
  87. defaultSelection={{datetime: DEFAULT_PROFILING_DATETIME_SELECTION}}
  88. >
  89. <Layout.Page>
  90. <ProfilingBetaAlertBanner organization={organization} />
  91. <ProfilingContentPageHeader />
  92. <LayoutBody>
  93. <LayoutMain fullWidth>
  94. <ActionBar>
  95. <PageFilterBar condensed>
  96. <ProjectPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  97. <EnvironmentPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  98. <DatePageFilter resetParamsOnChange={CURSOR_PARAMS} />
  99. </PageFilterBar>
  100. </ActionBar>
  101. {showOnboardingPanel ? (
  102. <ProfilingOnboardingCTA />
  103. ) : (
  104. <Fragment>
  105. {organization.features.includes(
  106. 'profiling-global-suspect-functions'
  107. ) && (
  108. <WidgetsContainer>
  109. <LandingWidgetSelector
  110. cursorName={LEFT_WIDGET_CURSOR}
  111. widgetHeight="410px"
  112. defaultWidget="slowest functions"
  113. storageKey="profiling-landing-widget-0"
  114. />
  115. <LandingWidgetSelector
  116. cursorName={RIGHT_WIDGET_CURSOR}
  117. widgetHeight="410px"
  118. defaultWidget="regressed functions"
  119. storageKey="profiling-landing-widget-1"
  120. />
  121. </WidgetsContainer>
  122. )}
  123. <div>
  124. <Tabs value={tab} onChange={onTabChange}>
  125. <TabList hideBorder>
  126. <TabList.Item key="transactions">
  127. {t('Transactions')}
  128. <StyledQuestionTooltip
  129. position="top"
  130. size="sm"
  131. title={t(
  132. 'Transactions breakdown the profiling data by transactions to provide a more focused view of the data. It allows you to view an aggregate flamegraph for just that transaction and find specific examples.'
  133. )}
  134. />
  135. </TabList.Item>
  136. <TabList.Item key="flamegraph">
  137. {t('Aggregate Flamegraph')}
  138. <StyledQuestionTooltip
  139. position="top"
  140. size="sm"
  141. title={t(
  142. 'Aggregate flamegraphs are a visual representation of stacktraces that helps identify where a program spends its time. Look for the widest stacktraces as they indicate where your application is spending more time.'
  143. )}
  144. />
  145. </TabList.Item>
  146. </TabList>
  147. </Tabs>
  148. </div>
  149. {tab === 'flamegraph' ? (
  150. <FlamegraphTab />
  151. ) : (
  152. <TransactionsTab location={location} selection={selection} />
  153. )}
  154. </Fragment>
  155. )}
  156. </LayoutMain>
  157. </LayoutBody>
  158. </Layout.Page>
  159. </PageFiltersContainer>
  160. </SentryDocumentTitle>
  161. );
  162. }
  163. interface TabbedContentProps {
  164. location: Location;
  165. selection: PageFilters;
  166. }
  167. function TransactionsTab({location, selection}: TabbedContentProps) {
  168. const query = decodeScalar(location.query.query, '');
  169. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  170. (searchQuery: string) => {
  171. browserHistory.push({
  172. ...location,
  173. query: {
  174. ...location.query,
  175. cursor: undefined,
  176. query: searchQuery || undefined,
  177. },
  178. });
  179. },
  180. [location]
  181. );
  182. const fields = ALL_FIELDS;
  183. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), fields, {
  184. key: 'count()',
  185. order: 'desc',
  186. });
  187. const cursor = decodeScalar(location.query.cursor);
  188. const transactions = useProfileEvents<FieldType>({
  189. cursor,
  190. fields,
  191. query,
  192. sort,
  193. limit: 50,
  194. referrer: 'api.profiling.landing-table',
  195. });
  196. const transactionsError =
  197. transactions.status === 'error' ? formatError(transactions.error) : '';
  198. return (
  199. <Fragment>
  200. <SearchbarContainer>
  201. <TransactionSearchQueryBuilder
  202. projects={selection.projects}
  203. initialQuery={query}
  204. onSearch={handleSearch}
  205. searchSource="profile_landing"
  206. />
  207. </SearchbarContainer>
  208. {transactionsError && (
  209. <Alert type="error" showIcon>
  210. {transactionsError}
  211. </Alert>
  212. )}
  213. <ProfileEventsTable
  214. columns={fields.slice()}
  215. data={transactions.status === 'success' ? transactions.data : null}
  216. error={transactions.status === 'error' ? t('Unable to load profiles') : null}
  217. isLoading={transactions.status === 'pending'}
  218. sort={sort}
  219. sortableColumns={new Set(fields)}
  220. />
  221. <StyledPagination
  222. pageLinks={
  223. transactions.status === 'success'
  224. ? transactions.getResponseHeader?.('Link') ?? null
  225. : null
  226. }
  227. />
  228. </Fragment>
  229. );
  230. }
  231. function FlamegraphTab() {
  232. return (
  233. <LandingAggregateFlamegraphSizer>
  234. <LandingAggregateFlamegraphContainer>
  235. <LandingAggregateFlamegraph />
  236. </LandingAggregateFlamegraphContainer>
  237. </LandingAggregateFlamegraphSizer>
  238. );
  239. }
  240. function shouldShowProfilingOnboardingPanel(selection: PageFilters, projects: Project[]) {
  241. // if it's My Projects or All projects, only show onboarding if we can't
  242. // find any projects with profiles
  243. if (selection.projects.length === 0 || selection.projects[0] === ALL_ACCESS_PROJECTS) {
  244. return projects.every(project => !project.hasProfiles);
  245. }
  246. // otherwise, only show onboarding if we can't find any projects with profiles
  247. // from those that were selected
  248. const projectsWithProfiles = new Set(
  249. projects.filter(project => project.hasProfiles).map(project => project.id)
  250. );
  251. return selection.projects.every(project => !projectsWithProfiles.has(String(project)));
  252. }
  253. function ProfilingOnboardingCTA() {
  254. const organization = useOrganization();
  255. // Open the modal on demand
  256. const onSetupProfilingClick = useCallback(() => {
  257. trackAnalytics('profiling_views.onboarding', {
  258. organization,
  259. });
  260. SidebarPanelStore.activatePanel(SidebarPanelKey.PROFILING_ONBOARDING);
  261. }, [organization]);
  262. return (
  263. <Fragment>
  264. <ProfilingOnboardingPanel
  265. content={
  266. // If user is on m2, show default
  267. <ProfilingAM1OrMMXUpgrade
  268. organization={organization}
  269. fallback={
  270. <Fragment>
  271. <h3>{t('Function level insights')}</h3>
  272. <p>
  273. {t(
  274. 'Discover slow-to-execute or resource intensive functions within your application'
  275. )}
  276. </p>
  277. </Fragment>
  278. }
  279. />
  280. }
  281. >
  282. <ProfilingUpgradeButton
  283. data-test-id="profiling-upgrade"
  284. organization={organization}
  285. priority="primary"
  286. onClick={onSetupProfilingClick}
  287. fallback={
  288. <Button onClick={onSetupProfilingClick} priority="primary">
  289. {t('Set Up Profiling')}
  290. </Button>
  291. }
  292. >
  293. {t('Set Up Profiling')}
  294. </ProfilingUpgradeButton>
  295. <LinkButton href="https://docs.sentry.io/product/profiling/" external>
  296. {t('Read Docs')}
  297. </LinkButton>
  298. </ProfilingOnboardingPanel>
  299. </Fragment>
  300. );
  301. }
  302. function ProfilingContentPageHeader() {
  303. return (
  304. <StyledLayoutHeader>
  305. <StyledHeaderContent>
  306. <Layout.Title>
  307. {t('Profiling')}
  308. <PageHeadingQuestionTooltip
  309. docsUrl="https://docs.sentry.io/product/profiling/"
  310. title={t(
  311. '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.'
  312. )}
  313. />
  314. </Layout.Title>
  315. <FeedbackWidgetButton />
  316. </StyledHeaderContent>
  317. </StyledLayoutHeader>
  318. );
  319. }
  320. const ALL_FIELDS = [
  321. 'transaction',
  322. 'project.id',
  323. 'last_seen()',
  324. 'p50()',
  325. 'p75()',
  326. 'p95()',
  327. 'p99()',
  328. 'count()',
  329. ] as const;
  330. type FieldType = (typeof ALL_FIELDS)[number];
  331. const LayoutBody = styled(Layout.Body)`
  332. display: grid;
  333. align-content: stretch;
  334. @media (min-width: ${p => p.theme.breakpoints.large}) {
  335. align-content: stretch;
  336. }
  337. `;
  338. const LayoutMain = styled(Layout.Main)`
  339. display: flex;
  340. flex-direction: column;
  341. `;
  342. const LandingAggregateFlamegraphSizer = styled('div')`
  343. height: 100%;
  344. min-height: max(80vh, 300px);
  345. margin-bottom: ${space(2)};
  346. margin-top: ${space(2)};
  347. `;
  348. const LandingAggregateFlamegraphContainer = styled('div')`
  349. height: 100%;
  350. position: relative;
  351. border: 1px solid ${p => p.theme.border};
  352. border-radius: ${p => p.theme.borderRadius};
  353. `;
  354. const StyledLayoutHeader = styled(Layout.Header)`
  355. display: block;
  356. `;
  357. const StyledHeaderContent = styled(Layout.HeaderContent)`
  358. display: flex;
  359. align-items: center;
  360. justify-content: space-between;
  361. flex-direction: row;
  362. `;
  363. const ActionBar = styled('div')`
  364. display: grid;
  365. gap: ${space(2)};
  366. grid-template-columns: min-content auto;
  367. margin-bottom: ${space(2)};
  368. `;
  369. const WidgetsContainer = styled('div')`
  370. display: grid;
  371. grid-template-columns: 1fr 1fr;
  372. gap: ${space(2)};
  373. @media (max-width: ${p => p.theme.breakpoints.small}) {
  374. grid-template-columns: 1fr;
  375. }
  376. `;
  377. const SearchbarContainer = styled('div')`
  378. margin-top: ${space(3)};
  379. margin-bottom: ${space(2)};
  380. `;
  381. const StyledPagination = styled(Pagination)`
  382. margin: 0;
  383. `;
  384. const StyledQuestionTooltip = styled(QuestionTooltip)`
  385. margin-left: ${space(0.5)};
  386. `;