content.tsx 12 KB

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