content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import {Fragment, useCallback, useEffect, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {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 FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  11. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  12. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  13. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  14. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  15. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  16. import Pagination from 'sentry/components/pagination';
  17. import {
  18. ProfilingAM1OrMMXUpgrade,
  19. ProfilingBetaAlertBanner,
  20. ProfilingUpgradeButton,
  21. } from 'sentry/components/profiling/billing/alerts';
  22. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  23. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  24. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  25. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  26. import SmartSearchBar from 'sentry/components/smartSearchBar';
  27. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  28. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  29. import {t} from 'sentry/locale';
  30. import SidebarPanelStore from 'sentry/stores/sidebarPanelStore';
  31. import {space} from 'sentry/styles/space';
  32. import {trackAnalytics} from 'sentry/utils/analytics';
  33. import EventView from 'sentry/utils/discover/eventView';
  34. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  35. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  36. import {formatError, formatSort} from 'sentry/utils/profiling/hooks/utils';
  37. import {decodeScalar} from 'sentry/utils/queryString';
  38. import useOrganization from 'sentry/utils/useOrganization';
  39. import usePageFilters from 'sentry/utils/usePageFilters';
  40. import useProjects from 'sentry/utils/useProjects';
  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 = profilingUsingTransactions ? ALL_FIELDS : BASE_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. const eventView = useMemo(() => {
  125. const _eventView = EventView.fromNewQueryWithLocation(
  126. {
  127. id: undefined,
  128. version: 2,
  129. name: t('Profiling'),
  130. fields: [],
  131. query,
  132. projects: selection.projects,
  133. },
  134. location
  135. );
  136. _eventView.additionalConditions.setFilterValues('has', ['profile.id']);
  137. return _eventView;
  138. }, [location, query, selection.projects]);
  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. <ProfilingBetaAlertBanner organization={organization} />
  150. <Layout.Header>
  151. <StyledHeaderContent>
  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. </Layout.Title>
  161. <FeedbackWidgetButton />
  162. </StyledHeaderContent>
  163. </Layout.Header>
  164. <Layout.Body>
  165. <Layout.Main fullWidth>
  166. {transactionsError && (
  167. <Alert type="error" showIcon>
  168. {transactionsError}
  169. </Alert>
  170. )}
  171. <ActionBar>
  172. <PageFilterBar condensed>
  173. <ProjectPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  174. <EnvironmentPageFilter resetParamsOnChange={CURSOR_PARAMS} />
  175. <DatePageFilter resetParamsOnChange={CURSOR_PARAMS} />
  176. </PageFilterBar>
  177. {profilingUsingTransactions ? (
  178. <SearchBar
  179. searchSource="profile_summary"
  180. organization={organization}
  181. projectIds={eventView.project}
  182. query={query}
  183. onSearch={handleSearch}
  184. maxQueryLength={MAX_QUERY_LENGTH}
  185. />
  186. ) : (
  187. <SmartSearchBar
  188. organization={organization}
  189. hasRecentSearches
  190. searchSource="profile_landing"
  191. supportedTags={profileFilters}
  192. query={query}
  193. onSearch={handleSearch}
  194. maxQueryLength={MAX_QUERY_LENGTH}
  195. />
  196. )}
  197. </ActionBar>
  198. {shouldShowProfilingOnboardingPanel ? (
  199. // If user is on m2, show default
  200. <ProfilingOnboardingPanel
  201. content={
  202. <ProfilingAM1OrMMXUpgrade
  203. organization={organization}
  204. fallback={
  205. <Fragment>
  206. <h3>{t('Function level insights')}</h3>
  207. <p>
  208. {t(
  209. 'Discover slow-to-execute or resource intensive functions within your application'
  210. )}
  211. </p>
  212. </Fragment>
  213. }
  214. />
  215. }
  216. >
  217. <ProfilingUpgradeButton
  218. organization={organization}
  219. priority="primary"
  220. onClick={onSetupProfilingClick}
  221. fallback={
  222. <Button onClick={onSetupProfilingClick} priority="primary">
  223. {t('Set Up Profiling')}
  224. </Button>
  225. }
  226. >
  227. {t('Set Up Profiling')}
  228. </ProfilingUpgradeButton>
  229. <Button href="https://docs.sentry.io/product/profiling/" external>
  230. {t('Read Docs')}
  231. </Button>
  232. </ProfilingOnboardingPanel>
  233. ) : (
  234. <Fragment>
  235. {organization.features.includes(
  236. 'profiling-global-suspect-functions'
  237. ) ? (
  238. <Fragment>
  239. <ProfilesChartWidget
  240. chartHeight={150}
  241. referrer="api.profiling.landing-chart"
  242. userQuery={query}
  243. selection={selection}
  244. />
  245. <WidgetsContainer>
  246. <LandingWidgetSelector
  247. cursorName={LEFT_WIDGET_CURSOR}
  248. widgetHeight="340px"
  249. defaultWidget="slowest functions"
  250. query={query}
  251. storageKey="profiling-landing-widget-0"
  252. />
  253. <LandingWidgetSelector
  254. cursorName={RIGHT_WIDGET_CURSOR}
  255. widgetHeight="340px"
  256. defaultWidget="regressed functions"
  257. query={query}
  258. storageKey="profiling-landing-widget-1"
  259. />
  260. </WidgetsContainer>
  261. </Fragment>
  262. ) : (
  263. <PanelsGrid>
  264. <ProfilingSlowestTransactionsPanel />
  265. <ProfilesChart
  266. referrer="api.profiling.landing-chart"
  267. query={query}
  268. selection={selection}
  269. hideCount
  270. />
  271. </PanelsGrid>
  272. )}
  273. <ProfileEventsTable
  274. columns={fields.slice()}
  275. data={transactions.status === 'success' ? transactions.data : null}
  276. error={
  277. transactions.status === 'error'
  278. ? t('Unable to load profiles')
  279. : null
  280. }
  281. isLoading={transactions.status === 'loading'}
  282. sort={sort}
  283. sortableColumns={new Set(fields)}
  284. />
  285. <Pagination
  286. pageLinks={
  287. transactions.status === 'success'
  288. ? transactions.getResponseHeader?.('Link') ?? null
  289. : null
  290. }
  291. />
  292. </Fragment>
  293. )}
  294. </Layout.Main>
  295. </Layout.Body>
  296. </Layout.Page>
  297. </PageFiltersContainer>
  298. </SentryDocumentTitle>
  299. );
  300. }
  301. const BASE_FIELDS = [
  302. 'transaction',
  303. 'project.id',
  304. 'last_seen()',
  305. 'p50()',
  306. 'p75()',
  307. 'p95()',
  308. 'p99()',
  309. 'count()',
  310. ] as const;
  311. // user misery is only available with the profiling-using-transactions feature
  312. const ALL_FIELDS = [...BASE_FIELDS, 'user_misery()'] as const;
  313. type FieldType = (typeof ALL_FIELDS)[number];
  314. const StyledHeaderContent = styled(Layout.HeaderContent)`
  315. display: flex;
  316. align-items: center;
  317. justify-content: space-between;
  318. flex-direction: row;
  319. `;
  320. const ActionBar = styled('div')`
  321. display: grid;
  322. gap: ${space(2)};
  323. grid-template-columns: min-content auto;
  324. margin-bottom: ${space(2)};
  325. `;
  326. // TODO: another simple primitive that can easily be <Grid columns={2} />
  327. const PanelsGrid = styled('div')`
  328. display: grid;
  329. grid-template-columns: minmax(0, 1fr) 1fr;
  330. gap: ${space(2)};
  331. @media (max-width: ${p => p.theme.breakpoints.small}) {
  332. grid-template-columns: minmax(0, 1fr);
  333. }
  334. `;
  335. const WidgetsContainer = styled('div')`
  336. display: grid;
  337. grid-template-columns: 1fr 1fr;
  338. gap: ${space(2)};
  339. @media (max-width: ${p => p.theme.breakpoints.small}) {
  340. grid-template-columns: 1fr;
  341. }
  342. `;
  343. export default ProfilingContent;