content.tsx 13 KB

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