content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. projectIds={projects.map(p => parseInt(p.id, 10))}
  176. searchSource="profile_landing"
  177. supportedTags={profileFilters}
  178. query={query}
  179. onSearch={handleSearch}
  180. maxQueryLength={MAX_QUERY_LENGTH}
  181. />
  182. )}
  183. </ActionBar>
  184. {shouldShowProfilingOnboardingPanel ? (
  185. <Fragment>
  186. <ProfilingUnsupportedAlert selectedProjects={selection.projects} />
  187. <ProfilingOnboardingPanel
  188. content={
  189. // If user is on m2, show default
  190. <ProfilingAM1OrMMXUpgrade
  191. organization={organization}
  192. fallback={
  193. <Fragment>
  194. <h3>{t('Function level insights')}</h3>
  195. <p>
  196. {t(
  197. 'Discover slow-to-execute or resource intensive functions within your application'
  198. )}
  199. </p>
  200. </Fragment>
  201. }
  202. />
  203. }
  204. >
  205. <ProfilingUpgradeButton
  206. data-test-id="profiling-upgrade"
  207. organization={organization}
  208. priority="primary"
  209. onClick={onSetupProfilingClick}
  210. fallback={
  211. <Button onClick={onSetupProfilingClick} priority="primary">
  212. {t('Set Up Profiling')}
  213. </Button>
  214. }
  215. >
  216. {t('Set Up Profiling')}
  217. </ProfilingUpgradeButton>
  218. <Button href="https://docs.sentry.io/product/profiling/" external>
  219. {t('Read Docs')}
  220. </Button>
  221. </ProfilingOnboardingPanel>
  222. </Fragment>
  223. ) : (
  224. <Fragment>
  225. {organization.features.includes(
  226. 'profiling-global-suspect-functions'
  227. ) ? (
  228. <Fragment>
  229. <ProfilesChartWidget
  230. chartHeight={150}
  231. referrer="api.profiling.landing-chart"
  232. userQuery={query}
  233. selection={selection}
  234. />
  235. <WidgetsContainer>
  236. <LandingWidgetSelector
  237. cursorName={LEFT_WIDGET_CURSOR}
  238. widgetHeight="340px"
  239. defaultWidget="slowest functions"
  240. query={query}
  241. storageKey="profiling-landing-widget-0"
  242. />
  243. <LandingWidgetSelector
  244. cursorName={RIGHT_WIDGET_CURSOR}
  245. widgetHeight="340px"
  246. defaultWidget="regressed functions"
  247. query={query}
  248. storageKey="profiling-landing-widget-1"
  249. />
  250. </WidgetsContainer>
  251. </Fragment>
  252. ) : (
  253. <PanelsGrid>
  254. <ProfilingSlowestTransactionsPanel />
  255. <ProfilesChart
  256. referrer="api.profiling.landing-chart"
  257. query={query}
  258. selection={selection}
  259. hideCount
  260. />
  261. </PanelsGrid>
  262. )}
  263. <ProfileEventsTable
  264. columns={fields.slice()}
  265. data={transactions.status === 'success' ? transactions.data : null}
  266. error={
  267. transactions.status === 'error'
  268. ? t('Unable to load profiles')
  269. : null
  270. }
  271. isLoading={transactions.status === 'loading'}
  272. sort={sort}
  273. sortableColumns={new Set(fields)}
  274. />
  275. <Pagination
  276. pageLinks={
  277. transactions.status === 'success'
  278. ? transactions.getResponseHeader?.('Link') ?? null
  279. : null
  280. }
  281. />
  282. </Fragment>
  283. )}
  284. </Layout.Main>
  285. </Layout.Body>
  286. </Layout.Page>
  287. </PageFiltersContainer>
  288. </SentryDocumentTitle>
  289. );
  290. }
  291. const ALL_FIELDS = [
  292. 'transaction',
  293. 'project.id',
  294. 'last_seen()',
  295. 'p50()',
  296. 'p75()',
  297. 'p95()',
  298. 'p99()',
  299. 'count()',
  300. ] as const;
  301. type FieldType = (typeof ALL_FIELDS)[number];
  302. const StyledHeaderContent = styled(Layout.HeaderContent)`
  303. display: flex;
  304. align-items: center;
  305. justify-content: space-between;
  306. flex-direction: row;
  307. `;
  308. const ActionBar = styled('div')`
  309. display: grid;
  310. gap: ${space(2)};
  311. grid-template-columns: min-content auto;
  312. margin-bottom: ${space(2)};
  313. `;
  314. // TODO: another simple primitive that can easily be <Grid columns={2} />
  315. const PanelsGrid = styled('div')`
  316. display: grid;
  317. grid-template-columns: minmax(0, 1fr) 1fr;
  318. gap: ${space(2)};
  319. @media (max-width: ${p => p.theme.breakpoints.small}) {
  320. grid-template-columns: minmax(0, 1fr);
  321. }
  322. `;
  323. const WidgetsContainer = styled('div')`
  324. display: grid;
  325. grid-template-columns: 1fr 1fr;
  326. gap: ${space(2)};
  327. @media (max-width: ${p => p.theme.breakpoints.small}) {
  328. grid-template-columns: 1fr;
  329. }
  330. `;
  331. export default ProfilingContent;