content.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 {MutableSearch} from 'sentry/utils/tokenizeSearch';
  38. import useOrganization from 'sentry/utils/useOrganization';
  39. import usePageFilters from 'sentry/utils/usePageFilters';
  40. import useProjects from 'sentry/utils/useProjects';
  41. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  42. import {FunctionTrendsWidget} from './landing/functionTrendsWidget';
  43. import {ProfileCharts} from './landing/profileCharts';
  44. import {ProfilingSlowestTransactionsPanel} from './landing/profilingSlowestTransactionsPanel';
  45. import {SlowestFunctionsWidget} from './landing/slowestFunctionsWidget';
  46. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  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 profilingUsingTransactions = organization.features.includes(
  56. 'profiling-using-transactions'
  57. );
  58. const fields = profilingUsingTransactions ? ALL_FIELDS : BASE_FIELDS;
  59. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), fields, {
  60. key: 'p95()',
  61. order: 'desc',
  62. });
  63. const profileFilters = useProfileFilters({
  64. query: '',
  65. selection,
  66. disabled: profilingUsingTransactions,
  67. });
  68. const {projects} = useProjects();
  69. const transactions = useProfileEvents<FieldType>({
  70. cursor,
  71. fields,
  72. query,
  73. sort,
  74. referrer: 'api.profiling.landing-table',
  75. });
  76. const transactionsError =
  77. transactions.status === 'error' ? formatError(transactions.error) : null;
  78. useEffect(() => {
  79. trackAnalytics('profiling_views.landing', {
  80. organization,
  81. });
  82. }, [organization]);
  83. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  84. (searchQuery: string) => {
  85. browserHistory.push({
  86. ...location,
  87. query: {
  88. ...location.query,
  89. cursor: undefined,
  90. query: searchQuery || undefined,
  91. },
  92. });
  93. },
  94. [location]
  95. );
  96. // Open the modal on demand
  97. const onSetupProfilingClick = useCallback(() => {
  98. trackAnalytics('profiling_views.onboarding', {
  99. organization,
  100. });
  101. SidebarPanelStore.activatePanel(SidebarPanelKey.PROFILING_ONBOARDING);
  102. }, [organization]);
  103. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  104. // if it's My Projects or All projects, only show onboarding if we can't
  105. // find any projects with profiles
  106. if (
  107. selection.projects.length === 0 ||
  108. selection.projects[0] === ALL_ACCESS_PROJECTS
  109. ) {
  110. return projects.every(project => !project.hasProfiles);
  111. }
  112. // otherwise, only show onboarding if we can't find any projects with profiles
  113. // from those that were selected
  114. const projectsWithProfiles = new Set(
  115. projects.filter(project => project.hasProfiles).map(project => project.id)
  116. );
  117. return selection.projects.every(
  118. project => !projectsWithProfiles.has(String(project))
  119. );
  120. }, [selection.projects, projects]);
  121. const eventView = useMemo(() => {
  122. const _eventView = EventView.fromNewQueryWithLocation(
  123. {
  124. id: undefined,
  125. version: 2,
  126. name: t('Profiling'),
  127. fields: [],
  128. query,
  129. projects: selection.projects,
  130. },
  131. location
  132. );
  133. _eventView.additionalConditions.setFilterValues('has', ['profile.id']);
  134. return _eventView;
  135. }, [location, query, selection.projects]);
  136. const isProfilingGA = organization.features.includes('profiling-ga');
  137. const functionQuery = useMemo(() => {
  138. const conditions = new MutableSearch('');
  139. conditions.setFilterValues('is_application', ['1']);
  140. return conditions.formatString();
  141. }, []);
  142. return (
  143. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  144. <PageFiltersContainer
  145. defaultSelection={
  146. profilingUsingTransactions
  147. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  148. : undefined
  149. }
  150. >
  151. <Layout.Page>
  152. {isProfilingGA && <ProfilingBetaAlertBanner organization={organization} />}
  153. <Layout.Header>
  154. <Layout.HeaderContent>
  155. <Layout.Title>
  156. {t('Profiling')}
  157. <PageHeadingQuestionTooltip
  158. docsUrl="https://docs.sentry.io/product/profiling/"
  159. title={t(
  160. '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.'
  161. )}
  162. />
  163. {isProfilingGA ? (
  164. <FeatureBadge type="new" />
  165. ) : (
  166. <FeatureBadge type="beta" />
  167. )}
  168. </Layout.Title>
  169. </Layout.HeaderContent>
  170. </Layout.Header>
  171. <Layout.Body>
  172. <Layout.Main fullWidth>
  173. {transactionsError && (
  174. <Alert type="error" showIcon>
  175. {transactionsError}
  176. </Alert>
  177. )}
  178. <ActionBar>
  179. <PageFilterBar condensed>
  180. <ProjectPageFilter />
  181. <EnvironmentPageFilter />
  182. <DatePageFilter alignDropdown="left" />
  183. </PageFilterBar>
  184. {profilingUsingTransactions ? (
  185. <SearchBar
  186. searchSource="profile_summary"
  187. organization={organization}
  188. projectIds={eventView.project}
  189. query={query}
  190. onSearch={handleSearch}
  191. maxQueryLength={MAX_QUERY_LENGTH}
  192. />
  193. ) : (
  194. <SmartSearchBar
  195. organization={organization}
  196. hasRecentSearches
  197. searchSource="profile_landing"
  198. supportedTags={profileFilters}
  199. query={query}
  200. onSearch={handleSearch}
  201. maxQueryLength={MAX_QUERY_LENGTH}
  202. />
  203. )}
  204. </ActionBar>
  205. {shouldShowProfilingOnboardingPanel ? (
  206. isProfilingGA ? (
  207. // If user is on m2, show default
  208. <ProfilingOnboardingPanel
  209. content={
  210. <ProfilingAM1OrMMXUpgrade
  211. organization={organization}
  212. fallback={
  213. <Fragment>
  214. <h3>{t('Function level insights')}</h3>
  215. <p>
  216. {t(
  217. 'Discover slow-to-execute or resource intensive functions within your application'
  218. )}
  219. </p>
  220. </Fragment>
  221. }
  222. />
  223. }
  224. >
  225. <ProfilingUpgradeButton
  226. organization={organization}
  227. priority="primary"
  228. fallback={
  229. <Button onClick={onSetupProfilingClick} priority="primary">
  230. {t('Set Up Profiling')}
  231. </Button>
  232. }
  233. >
  234. {t('Update plan')}
  235. </ProfilingUpgradeButton>
  236. <Button href="https://docs.sentry.io/product/profiling/" external>
  237. {t('Read Docs')}
  238. </Button>
  239. </ProfilingOnboardingPanel>
  240. ) : (
  241. // show previous state
  242. <ProfilingOnboardingPanel>
  243. <Button onClick={onSetupProfilingClick} priority="primary">
  244. {t('Set Up Profiling')}
  245. </Button>
  246. <Button href="https://docs.sentry.io/product/profiling/" external>
  247. {t('Read Docs')}
  248. </Button>
  249. </ProfilingOnboardingPanel>
  250. )
  251. ) : (
  252. <Fragment>
  253. <PanelsGrid>
  254. {organization.features.includes(
  255. 'profiling-global-suspect-functions'
  256. ) ? (
  257. <Fragment>
  258. <SlowestFunctionsWidget userQuery={functionQuery} />
  259. <FunctionTrendsWidget
  260. trendFunction="p95()"
  261. trendType="regression"
  262. userQuery={functionQuery}
  263. />
  264. </Fragment>
  265. ) : (
  266. <Fragment>
  267. <ProfilingSlowestTransactionsPanel />
  268. <ProfileCharts
  269. referrer="api.profiling.landing-chart"
  270. query={query}
  271. selection={selection}
  272. hideCount
  273. />
  274. </Fragment>
  275. )}
  276. </PanelsGrid>
  277. <ProfileEventsTable
  278. columns={fields.slice()}
  279. data={transactions.status === 'success' ? transactions.data : null}
  280. error={
  281. transactions.status === 'error'
  282. ? t('Unable to load profiles')
  283. : null
  284. }
  285. isLoading={transactions.status === 'loading'}
  286. sort={sort}
  287. sortableColumns={new Set(fields)}
  288. />
  289. <Pagination
  290. pageLinks={
  291. transactions.status === 'success'
  292. ? transactions.getResponseHeader?.('Link') ?? null
  293. : null
  294. }
  295. />
  296. </Fragment>
  297. )}
  298. </Layout.Main>
  299. </Layout.Body>
  300. </Layout.Page>
  301. </PageFiltersContainer>
  302. </SentryDocumentTitle>
  303. );
  304. }
  305. const BASE_FIELDS = [
  306. 'transaction',
  307. 'project.id',
  308. 'last_seen()',
  309. 'p75()',
  310. 'p95()',
  311. 'p99()',
  312. 'count()',
  313. ] as const;
  314. // user misery is only available with the profiling-using-transactions feature
  315. const ALL_FIELDS = [...BASE_FIELDS, 'user_misery()'] as const;
  316. type FieldType = (typeof ALL_FIELDS)[number];
  317. const ActionBar = styled('div')`
  318. display: grid;
  319. gap: ${space(2)};
  320. grid-template-columns: min-content auto;
  321. margin-bottom: ${space(2)};
  322. `;
  323. // TODO: another simple primitive that can easily be <Grid columns={2} />
  324. const PanelsGrid = styled('div')`
  325. display: grid;
  326. grid-template-columns: minmax(0, 1fr) 1fr;
  327. gap: ${space(2)};
  328. @media (max-width: ${p => p.theme.breakpoints.small}) {
  329. grid-template-columns: minmax(0, 1fr);
  330. }
  331. `;
  332. export default ProfilingContent;