content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. onClick={onSetupProfilingClick}
  220. fallback={
  221. <Button onClick={onSetupProfilingClick} priority="primary">
  222. {t('Set Up Profiling')}
  223. </Button>
  224. }
  225. >
  226. {t('Set Up Profiling')}
  227. </ProfilingUpgradeButton>
  228. <Button href="https://docs.sentry.io/product/profiling/" external>
  229. {t('Read Docs')}
  230. </Button>
  231. </ProfilingOnboardingPanel>
  232. ) : (
  233. <Fragment>
  234. {organization.features.includes(
  235. 'profiling-global-suspect-functions'
  236. ) ? (
  237. <Fragment>
  238. <ProfilesChartWidget
  239. chartHeight={150}
  240. referrer="api.profiling.landing-chart"
  241. userQuery={query}
  242. selection={selection}
  243. />
  244. <WidgetsContainer>
  245. <LandingWidgetSelector
  246. cursorName={LEFT_WIDGET_CURSOR}
  247. widgetHeight="340px"
  248. defaultWidget="slowest functions"
  249. query={query}
  250. storageKey="profiling-landing-widget-0"
  251. />
  252. <LandingWidgetSelector
  253. cursorName={RIGHT_WIDGET_CURSOR}
  254. widgetHeight="340px"
  255. defaultWidget="regressed functions"
  256. query={query}
  257. storageKey="profiling-landing-widget-1"
  258. />
  259. </WidgetsContainer>
  260. </Fragment>
  261. ) : (
  262. <PanelsGrid>
  263. <ProfilingSlowestTransactionsPanel />
  264. <ProfilesChart
  265. referrer="api.profiling.landing-chart"
  266. query={query}
  267. selection={selection}
  268. hideCount
  269. />
  270. </PanelsGrid>
  271. )}
  272. <ProfileEventsTable
  273. columns={fields.slice()}
  274. data={transactions.status === 'success' ? transactions.data : null}
  275. error={
  276. transactions.status === 'error'
  277. ? t('Unable to load profiles')
  278. : null
  279. }
  280. isLoading={transactions.status === 'loading'}
  281. sort={sort}
  282. sortableColumns={new Set(fields)}
  283. />
  284. <Pagination
  285. pageLinks={
  286. transactions.status === 'success'
  287. ? transactions.getResponseHeader?.('Link') ?? null
  288. : null
  289. }
  290. />
  291. </Fragment>
  292. )}
  293. </Layout.Main>
  294. </Layout.Body>
  295. </Layout.Page>
  296. </PageFiltersContainer>
  297. </SentryDocumentTitle>
  298. );
  299. }
  300. const BASE_FIELDS = [
  301. 'transaction',
  302. 'project.id',
  303. 'last_seen()',
  304. 'p75()',
  305. 'p95()',
  306. 'p99()',
  307. 'count()',
  308. ] as const;
  309. // user misery is only available with the profiling-using-transactions feature
  310. const ALL_FIELDS = [...BASE_FIELDS, 'user_misery()'] as const;
  311. type FieldType = (typeof ALL_FIELDS)[number];
  312. const StyledHeaderContent = styled(Layout.HeaderContent)`
  313. display: flex;
  314. align-items: center;
  315. justify-content: space-between;
  316. flex-direction: row;
  317. `;
  318. const ActionBar = styled('div')`
  319. display: grid;
  320. gap: ${space(2)};
  321. grid-template-columns: min-content auto;
  322. margin-bottom: ${space(2)};
  323. `;
  324. // TODO: another simple primitive that can easily be <Grid columns={2} />
  325. const PanelsGrid = styled('div')`
  326. display: grid;
  327. grid-template-columns: minmax(0, 1fr) 1fr;
  328. gap: ${space(2)};
  329. @media (max-width: ${p => p.theme.breakpoints.small}) {
  330. grid-template-columns: minmax(0, 1fr);
  331. }
  332. `;
  333. const WidgetsContainer = styled('div')`
  334. display: grid;
  335. grid-template-columns: 1fr 1fr;
  336. gap: ${space(2)};
  337. @media (max-width: ${p => p.theme.breakpoints.small}) {
  338. grid-template-columns: 1fr;
  339. }
  340. `;
  341. export default ProfilingContent;