content.tsx 13 KB

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