content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 SearchBar from 'sentry/components/events/searchBar';
  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 ProfilingFeedbackButton from 'sentry/components/profiling/profilingFeedbackButton';
  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 {ProfilesChart} 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: 'count()',
  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. return (
  139. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  140. <PageFiltersContainer
  141. defaultSelection={
  142. profilingUsingTransactions
  143. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  144. : undefined
  145. }
  146. >
  147. <Layout.Page>
  148. <ProfilingBetaAlertBanner organization={organization} />
  149. <Layout.Header>
  150. <StyledHeaderContent>
  151. <Layout.Title>
  152. {t('Profiling')}
  153. <PageHeadingQuestionTooltip
  154. docsUrl="https://docs.sentry.io/product/profiling/"
  155. title={t(
  156. '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.'
  157. )}
  158. />
  159. </Layout.Title>
  160. <ProfilingFeedbackButton />
  161. </StyledHeaderContent>
  162. </Layout.Header>
  163. <Layout.Body>
  164. <Layout.Main fullWidth>
  165. {transactionsError && (
  166. <Alert type="error" showIcon>
  167. {transactionsError}
  168. </Alert>
  169. )}
  170. <ActionBar>
  171. <PageFilterBar condensed>
  172. <ProjectPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  173. <EnvironmentPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  174. <DatePageFilter resetParamsOnChange={CURSOR_PARAMS} />
  175. </PageFilterBar>
  176. {profilingUsingTransactions ? (
  177. <SearchBar
  178. searchSource="profile_summary"
  179. organization={organization}
  180. projectIds={eventView.project}
  181. query={query}
  182. onSearch={handleSearch}
  183. maxQueryLength={MAX_QUERY_LENGTH}
  184. />
  185. ) : (
  186. <SmartSearchBar
  187. organization={organization}
  188. hasRecentSearches
  189. searchSource="profile_landing"
  190. supportedTags={profileFilters}
  191. query={query}
  192. onSearch={handleSearch}
  193. maxQueryLength={MAX_QUERY_LENGTH}
  194. />
  195. )}
  196. </ActionBar>
  197. {shouldShowProfilingOnboardingPanel ? (
  198. // If user is on m2, show default
  199. <ProfilingOnboardingPanel
  200. content={
  201. <ProfilingAM1OrMMXUpgrade
  202. organization={organization}
  203. fallback={
  204. <Fragment>
  205. <h3>{t('Function level insights')}</h3>
  206. <p>
  207. {t(
  208. 'Discover slow-to-execute or resource intensive functions within your application'
  209. )}
  210. </p>
  211. </Fragment>
  212. }
  213. />
  214. }
  215. >
  216. <ProfilingUpgradeButton
  217. organization={organization}
  218. priority="primary"
  219. fallback={
  220. <Button onClick={onSetupProfilingClick} priority="primary">
  221. {t('Set Up Profiling')}
  222. </Button>
  223. }
  224. >
  225. {t('Update plan')}
  226. </ProfilingUpgradeButton>
  227. <Button href="https://docs.sentry.io/product/profiling/" external>
  228. {t('Read Docs')}
  229. </Button>
  230. </ProfilingOnboardingPanel>
  231. ) : (
  232. <Fragment>
  233. {organization.features.includes(
  234. 'profiling-global-suspect-functions'
  235. ) ? (
  236. <Fragment>
  237. <ProfilesChartWidget
  238. chartHeight={150}
  239. referrer="api.profiling.landing-chart"
  240. userQuery={query}
  241. selection={selection}
  242. />
  243. <WidgetsContainer>
  244. <LandingWidgetSelector
  245. cursorName={LEFT_WIDGET_CURSOR}
  246. widgetHeight="340px"
  247. defaultWidget="slowest functions"
  248. query={query}
  249. storageKey="profiling-landing-widget-0"
  250. />
  251. <LandingWidgetSelector
  252. cursorName={RIGHT_WIDGET_CURSOR}
  253. widgetHeight="340px"
  254. defaultWidget="regressed functions"
  255. query={query}
  256. storageKey="profiling-landing-widget-1"
  257. />
  258. </WidgetsContainer>
  259. </Fragment>
  260. ) : (
  261. <PanelsGrid>
  262. <ProfilingSlowestTransactionsPanel />
  263. <ProfilesChart
  264. referrer="api.profiling.landing-chart"
  265. query={query}
  266. selection={selection}
  267. hideCount
  268. />
  269. </PanelsGrid>
  270. )}
  271. <ProfileEventsTable
  272. columns={fields.slice()}
  273. data={transactions.status === 'success' ? transactions.data : null}
  274. error={
  275. transactions.status === 'error'
  276. ? t('Unable to load profiles')
  277. : null
  278. }
  279. isLoading={transactions.status === 'loading'}
  280. sort={sort}
  281. sortableColumns={new Set(fields)}
  282. />
  283. <Pagination
  284. pageLinks={
  285. transactions.status === 'success'
  286. ? transactions.getResponseHeader?.('Link') ?? null
  287. : null
  288. }
  289. />
  290. </Fragment>
  291. )}
  292. </Layout.Main>
  293. </Layout.Body>
  294. </Layout.Page>
  295. </PageFiltersContainer>
  296. </SentryDocumentTitle>
  297. );
  298. }
  299. const BASE_FIELDS = [
  300. 'transaction',
  301. 'project.id',
  302. 'last_seen()',
  303. 'p75()',
  304. 'p95()',
  305. 'p99()',
  306. 'count()',
  307. ] as const;
  308. // user misery is only available with the profiling-using-transactions feature
  309. const ALL_FIELDS = [...BASE_FIELDS, 'user_misery()'] as const;
  310. type FieldType = (typeof ALL_FIELDS)[number];
  311. const StyledHeaderContent = styled(Layout.HeaderContent)`
  312. display: flex;
  313. align-items: center;
  314. justify-content: space-between;
  315. flex-direction: row;
  316. `;
  317. const ActionBar = styled('div')`
  318. display: grid;
  319. gap: ${space(2)};
  320. grid-template-columns: min-content auto;
  321. margin-bottom: ${space(2)};
  322. `;
  323. // TODO: another simple primitive that can easily be <Grid columns={2} />
  324. const PanelsGrid = styled('div')`
  325. display: grid;
  326. grid-template-columns: minmax(0, 1fr) 1fr;
  327. gap: ${space(2)};
  328. @media (max-width: ${p => p.theme.breakpoints.small}) {
  329. grid-template-columns: minmax(0, 1fr);
  330. }
  331. `;
  332. const WidgetsContainer = styled('div')`
  333. display: grid;
  334. grid-template-columns: 1fr 1fr;
  335. gap: ${space(2)};
  336. @media (max-width: ${p => p.theme.breakpoints.small}) {
  337. grid-template-columns: 1fr;
  338. }
  339. `;
  340. export default ProfilingContent;