content.tsx 14 KB

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