content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. projectIds={projects.map(p => parseInt(p.id, 10))}
  175. searchSource="profile_landing"
  176. supportedTags={profileFilters}
  177. query={query}
  178. onSearch={handleSearch}
  179. maxQueryLength={MAX_QUERY_LENGTH}
  180. />
  181. )}
  182. </ActionBar>
  183. {shouldShowProfilingOnboardingPanel ? (
  184. <Fragment>
  185. <ProfilingOnboardingPanel
  186. content={
  187. // If user is on m2, show default
  188. <ProfilingAM1OrMMXUpgrade
  189. organization={organization}
  190. fallback={
  191. <Fragment>
  192. <h3>{t('Function level insights')}</h3>
  193. <p>
  194. {t(
  195. 'Discover slow-to-execute or resource intensive functions within your application'
  196. )}
  197. </p>
  198. </Fragment>
  199. }
  200. />
  201. }
  202. >
  203. <ProfilingUpgradeButton
  204. data-test-id="profiling-upgrade"
  205. organization={organization}
  206. priority="primary"
  207. onClick={onSetupProfilingClick}
  208. fallback={
  209. <Button onClick={onSetupProfilingClick} priority="primary">
  210. {t('Set Up Profiling')}
  211. </Button>
  212. }
  213. >
  214. {t('Set Up Profiling')}
  215. </ProfilingUpgradeButton>
  216. <Button href="https://docs.sentry.io/product/profiling/" external>
  217. {t('Read Docs')}
  218. </Button>
  219. </ProfilingOnboardingPanel>
  220. </Fragment>
  221. ) : (
  222. <Fragment>
  223. {organization.features.includes(
  224. 'profiling-global-suspect-functions'
  225. ) ? (
  226. <Fragment>
  227. <ProfilesChartWidget
  228. chartHeight={150}
  229. referrer="api.profiling.landing-chart"
  230. userQuery={query}
  231. selection={selection}
  232. />
  233. <WidgetsContainer>
  234. <LandingWidgetSelector
  235. cursorName={LEFT_WIDGET_CURSOR}
  236. widgetHeight="340px"
  237. defaultWidget="slowest functions"
  238. query={query}
  239. storageKey="profiling-landing-widget-0"
  240. />
  241. <LandingWidgetSelector
  242. cursorName={RIGHT_WIDGET_CURSOR}
  243. widgetHeight="340px"
  244. defaultWidget="regressed functions"
  245. query={query}
  246. storageKey="profiling-landing-widget-1"
  247. />
  248. </WidgetsContainer>
  249. </Fragment>
  250. ) : (
  251. <PanelsGrid>
  252. <ProfilingSlowestTransactionsPanel />
  253. <ProfilesChart
  254. referrer="api.profiling.landing-chart"
  255. query={query}
  256. selection={selection}
  257. hideCount
  258. />
  259. </PanelsGrid>
  260. )}
  261. <ProfileEventsTable
  262. columns={fields.slice()}
  263. data={transactions.status === 'success' ? transactions.data : null}
  264. error={
  265. transactions.status === 'error'
  266. ? t('Unable to load profiles')
  267. : null
  268. }
  269. isLoading={transactions.status === 'loading'}
  270. sort={sort}
  271. sortableColumns={new Set(fields)}
  272. />
  273. <Pagination
  274. pageLinks={
  275. transactions.status === 'success'
  276. ? transactions.getResponseHeader?.('Link') ?? null
  277. : null
  278. }
  279. />
  280. </Fragment>
  281. )}
  282. </Layout.Main>
  283. </Layout.Body>
  284. </Layout.Page>
  285. </PageFiltersContainer>
  286. </SentryDocumentTitle>
  287. );
  288. }
  289. const ALL_FIELDS = [
  290. 'transaction',
  291. 'project.id',
  292. 'last_seen()',
  293. 'p50()',
  294. 'p75()',
  295. 'p95()',
  296. 'p99()',
  297. 'count()',
  298. ] as const;
  299. type FieldType = (typeof ALL_FIELDS)[number];
  300. const StyledHeaderContent = styled(Layout.HeaderContent)`
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-between;
  304. flex-direction: row;
  305. `;
  306. const ActionBar = styled('div')`
  307. display: grid;
  308. gap: ${space(2)};
  309. grid-template-columns: min-content auto;
  310. margin-bottom: ${space(2)};
  311. `;
  312. // TODO: another simple primitive that can easily be <Grid columns={2} />
  313. const PanelsGrid = styled('div')`
  314. display: grid;
  315. grid-template-columns: minmax(0, 1fr) 1fr;
  316. gap: ${space(2)};
  317. @media (max-width: ${p => p.theme.breakpoints.small}) {
  318. grid-template-columns: minmax(0, 1fr);
  319. }
  320. `;
  321. const WidgetsContainer = styled('div')`
  322. display: grid;
  323. grid-template-columns: 1fr 1fr;
  324. gap: ${space(2)};
  325. @media (max-width: ${p => p.theme.breakpoints.small}) {
  326. grid-template-columns: 1fr;
  327. }
  328. `;
  329. export default ProfilingContent;