content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 * as Layout from 'sentry/components/layouts/thirds';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  13. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  14. import Pagination from 'sentry/components/pagination';
  15. import {
  16. ProfilingAM1OrMMXUpgrade,
  17. ProfilingBetaAlertBanner,
  18. ProfilingUpgradeButton,
  19. } from 'sentry/components/profiling/billing/alerts';
  20. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  21. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  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 {ProfileCharts} 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: 'p95()',
  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
  173. alignDropdown="left"
  174. resetParamsOnChange={CURSOR_PARAMS}
  175. />
  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. fallback={
  221. <Button onClick={onSetupProfilingClick} priority="primary">
  222. {t('Set Up Profiling')}
  223. </Button>
  224. }
  225. >
  226. {t('Update plan')}
  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. <ProfileCharts
  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 ActionBar = styled('div')`
  313. display: grid;
  314. gap: ${space(2)};
  315. grid-template-columns: min-content auto;
  316. margin-bottom: ${space(2)};
  317. `;
  318. // TODO: another simple primitive that can easily be <Grid columns={2} />
  319. const PanelsGrid = styled('div')`
  320. display: grid;
  321. grid-template-columns: minmax(0, 1fr) 1fr;
  322. gap: ${space(2)};
  323. @media (max-width: ${p => p.theme.breakpoints.small}) {
  324. grid-template-columns: minmax(0, 1fr);
  325. }
  326. `;
  327. const WidgetsContainer = styled('div')`
  328. display: grid;
  329. grid-template-columns: 1fr 1fr;
  330. gap: ${space(2)};
  331. @media (max-width: ${p => p.theme.breakpoints.small}) {
  332. grid-template-columns: 1fr;
  333. }
  334. `;
  335. export default ProfilingContent;