content.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 FeatureBadge from 'sentry/components/featureBadge';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  12. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  13. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  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 ProjectPageFilter from 'sentry/components/projectPageFilter';
  23. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  24. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  25. import SmartSearchBar, {SmartSearchBarProps} 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 EventView from 'sentry/utils/discover/eventView';
  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 {ProfileCharts} from './landing/profileCharts';
  42. import {ProfilingSlowestTransactionsPanel} from './landing/profilingSlowestTransactionsPanel';
  43. import {SlowestFunctionsWidget} from './landing/slowestFunctionsWidget';
  44. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  45. interface ProfilingContentProps {
  46. location: Location;
  47. }
  48. function ProfilingContent({location}: ProfilingContentProps) {
  49. const organization = useOrganization();
  50. const {selection} = usePageFilters();
  51. const cursor = decodeScalar(location.query.cursor);
  52. const query = decodeScalar(location.query.query, '');
  53. const profilingUsingTransactions = organization.features.includes(
  54. 'profiling-using-transactions'
  55. );
  56. const fields = profilingUsingTransactions ? ALL_FIELDS : BASE_FIELDS;
  57. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), fields, {
  58. key: 'p95()',
  59. order: 'desc',
  60. });
  61. const profileFilters = useProfileFilters({
  62. query: '',
  63. selection,
  64. disabled: profilingUsingTransactions,
  65. });
  66. const {projects} = useProjects();
  67. const transactions = useProfileEvents<FieldType>({
  68. cursor,
  69. fields,
  70. query,
  71. sort,
  72. referrer: 'api.profiling.landing-table',
  73. });
  74. const transactionsError =
  75. transactions.status === 'error' ? formatError(transactions.error) : null;
  76. useEffect(() => {
  77. trackAnalytics('profiling_views.landing', {
  78. organization,
  79. });
  80. }, [organization]);
  81. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  82. (searchQuery: string) => {
  83. browserHistory.push({
  84. ...location,
  85. query: {
  86. ...location.query,
  87. cursor: undefined,
  88. query: searchQuery || undefined,
  89. },
  90. });
  91. },
  92. [location]
  93. );
  94. // Open the modal on demand
  95. const onSetupProfilingClick = useCallback(() => {
  96. trackAnalytics('profiling_views.onboarding', {
  97. organization,
  98. });
  99. SidebarPanelStore.activatePanel(SidebarPanelKey.PROFILING_ONBOARDING);
  100. }, [organization]);
  101. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  102. // if it's My Projects or All projects, only show onboarding if we can't
  103. // find any projects with profiles
  104. if (
  105. selection.projects.length === 0 ||
  106. selection.projects[0] === ALL_ACCESS_PROJECTS
  107. ) {
  108. return projects.every(project => !project.hasProfiles);
  109. }
  110. // otherwise, only show onboarding if we can't find any projects with profiles
  111. // from those that were selected
  112. const projectsWithProfiles = new Set(
  113. projects.filter(project => project.hasProfiles).map(project => project.id)
  114. );
  115. return selection.projects.every(
  116. project => !projectsWithProfiles.has(String(project))
  117. );
  118. }, [selection.projects, projects]);
  119. const eventView = useMemo(() => {
  120. const _eventView = EventView.fromNewQueryWithLocation(
  121. {
  122. id: undefined,
  123. version: 2,
  124. name: t('Profiling'),
  125. fields: [],
  126. query,
  127. projects: selection.projects,
  128. },
  129. location
  130. );
  131. _eventView.additionalConditions.setFilterValues('has', ['profile.id']);
  132. return _eventView;
  133. }, [location, query, selection.projects]);
  134. const isProfilingGA = organization.features.includes('profiling-ga');
  135. return (
  136. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  137. <PageFiltersContainer
  138. defaultSelection={
  139. profilingUsingTransactions
  140. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  141. : undefined
  142. }
  143. >
  144. <Layout.Page>
  145. {isProfilingGA && <ProfilingBetaAlertBanner organization={organization} />}
  146. <Layout.Header>
  147. <Layout.HeaderContent>
  148. <Layout.Title>
  149. {t('Profiling')}
  150. <PageHeadingQuestionTooltip
  151. docsUrl="https://docs.sentry.io/product/profiling/"
  152. title={t(
  153. '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.'
  154. )}
  155. />
  156. {isProfilingGA ? (
  157. <FeatureBadge type="new" />
  158. ) : (
  159. <FeatureBadge type="beta" />
  160. )}
  161. </Layout.Title>
  162. </Layout.HeaderContent>
  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 />
  174. <EnvironmentPageFilter />
  175. <DatePageFilter alignDropdown="left" />
  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. isProfilingGA ? (
  200. // If user is on m2, show default
  201. <ProfilingOnboardingPanel
  202. content={
  203. <ProfilingAM1OrMMXUpgrade
  204. organization={organization}
  205. fallback={
  206. <Fragment>
  207. <h3>{t('Function level insights')}</h3>
  208. <p>
  209. {t(
  210. 'Discover slow-to-execute or resource intensive functions within your application'
  211. )}
  212. </p>
  213. </Fragment>
  214. }
  215. />
  216. }
  217. >
  218. <ProfilingUpgradeButton
  219. organization={organization}
  220. priority="primary"
  221. fallback={
  222. <Button onClick={onSetupProfilingClick} priority="primary">
  223. {t('Set Up Profiling')}
  224. </Button>
  225. }
  226. >
  227. {t('Update plan')}
  228. </ProfilingUpgradeButton>
  229. <Button href="https://docs.sentry.io/product/profiling/" external>
  230. {t('Read Docs')}
  231. </Button>
  232. </ProfilingOnboardingPanel>
  233. ) : (
  234. // show previous state
  235. <ProfilingOnboardingPanel>
  236. <Button onClick={onSetupProfilingClick} priority="primary">
  237. {t('Set Up Profiling')}
  238. </Button>
  239. <Button href="https://docs.sentry.io/product/profiling/" external>
  240. {t('Read Docs')}
  241. </Button>
  242. </ProfilingOnboardingPanel>
  243. )
  244. ) : (
  245. <Fragment>
  246. <PanelsGrid>
  247. {organization.features.includes(
  248. 'profiling-global-suspect-functions'
  249. ) ? (
  250. <SlowestFunctionsWidget />
  251. ) : (
  252. <ProfilingSlowestTransactionsPanel />
  253. )}
  254. <ProfileCharts
  255. referrer="api.profiling.landing-chart"
  256. query={query}
  257. selection={selection}
  258. hideCount
  259. />
  260. </PanelsGrid>
  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 BASE_FIELDS = [
  290. 'transaction',
  291. 'project.id',
  292. 'last_seen()',
  293. 'p75()',
  294. 'p95()',
  295. 'p99()',
  296. 'count()',
  297. ] as const;
  298. // user misery is only available with the profiling-using-transactions feature
  299. const ALL_FIELDS = [...BASE_FIELDS, 'user_misery()'] as const;
  300. type FieldType = (typeof ALL_FIELDS)[number];
  301. const ActionBar = styled('div')`
  302. display: grid;
  303. gap: ${space(2)};
  304. grid-template-columns: min-content auto;
  305. margin-bottom: ${space(2)};
  306. `;
  307. // TODO: another simple primitive that can easily be <Grid columns={2} />
  308. const PanelsGrid = styled('div')`
  309. display: grid;
  310. grid-template-columns: minmax(0, 1fr) 1fr;
  311. gap: ${space(2)};
  312. @media (max-width: ${p => p.theme.breakpoints.small}) {
  313. grid-template-columns: minmax(0, 1fr);
  314. }
  315. `;
  316. export default ProfilingContent;