index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import {useCallback, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {Button, LinkButton} from 'sentry/components/button';
  6. import {CompactSelect} from 'sentry/components/compactSelect';
  7. import type {SelectOption} from 'sentry/components/compactSelect/types';
  8. import DatePageFilter from 'sentry/components/datePageFilter';
  9. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  10. import ErrorBoundary from 'sentry/components/errorBoundary';
  11. import SearchBar from 'sentry/components/events/searchBar';
  12. import IdBadge from 'sentry/components/idBadge';
  13. import * as Layout from 'sentry/components/layouts/thirds';
  14. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  15. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  16. import {AggregateFlamegraph} from 'sentry/components/profiling/flamegraph/aggregateFlamegraph';
  17. import {FlamegraphSearch} from 'sentry/components/profiling/flamegraph/flamegraphToolbar/flamegraphSearch';
  18. import {
  19. ProfilingBreadcrumbs,
  20. ProfilingBreadcrumbsProps,
  21. } from 'sentry/components/profiling/profilingBreadcrumbs';
  22. import {SegmentedControl} from 'sentry/components/segmentedControl';
  23. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  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 {t} from 'sentry/locale';
  28. import {space} from 'sentry/styles/space';
  29. import type {Organization, PageFilters, Project} from 'sentry/types';
  30. import {defined} from 'sentry/utils';
  31. import EventView from 'sentry/utils/discover/eventView';
  32. import {isAggregateField} from 'sentry/utils/discover/fields';
  33. import {
  34. CanvasPoolManager,
  35. CanvasScheduler,
  36. useCanvasScheduler,
  37. } from 'sentry/utils/profiling/canvasScheduler';
  38. import {FlamegraphStateProvider} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphContextProvider';
  39. import {FlamegraphThemeProvider} from 'sentry/utils/profiling/flamegraph/flamegraphThemeProvider';
  40. import {Frame} from 'sentry/utils/profiling/frame';
  41. import {useAggregateFlamegraphQuery} from 'sentry/utils/profiling/hooks/useAggregateFlamegraphQuery';
  42. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  43. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  44. import {decodeScalar} from 'sentry/utils/queryString';
  45. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  46. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  47. import useOrganization from 'sentry/utils/useOrganization';
  48. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  49. import {
  50. FlamegraphProvider,
  51. useFlamegraph,
  52. } from 'sentry/views/profiling/flamegraphProvider';
  53. import {ProfilesSummaryChart} from 'sentry/views/profiling/landing/profilesSummaryChart';
  54. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  55. import {LegacySummaryPage} from 'sentry/views/profiling/profileSummary/legacySummaryPage';
  56. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  57. import {MostRegressedProfileFunctions} from './regressedProfileFunctions';
  58. import {SlowestProfileFunctions} from './slowestProfileFunctions';
  59. interface ProfileSummaryHeaderProps {
  60. location: Location;
  61. organization: Organization;
  62. project: Project | null;
  63. query: string;
  64. transaction: string;
  65. }
  66. function ProfileSummaryHeader(props: ProfileSummaryHeaderProps) {
  67. const breadcrumbTrails: ProfilingBreadcrumbsProps['trails'] = useMemo(() => {
  68. return [
  69. {
  70. type: 'landing',
  71. payload: {
  72. query: props.location.query,
  73. },
  74. },
  75. {
  76. type: 'profile summary',
  77. payload: {
  78. projectSlug: props.project?.slug ?? '',
  79. query: props.location.query,
  80. transaction: props.transaction,
  81. },
  82. },
  83. ];
  84. }, [props.location.query, props.project?.slug, props.transaction]);
  85. const transactionSummaryTarget =
  86. props.project &&
  87. props.transaction &&
  88. transactionSummaryRouteWithQuery({
  89. orgSlug: props.organization.slug,
  90. transaction: props.transaction,
  91. projectID: props.project.id,
  92. query: {query: props.query},
  93. });
  94. return (
  95. <Layout.Header>
  96. <Layout.HeaderContent>
  97. <ProfilingBreadcrumbs
  98. organization={props.organization}
  99. trails={breadcrumbTrails}
  100. />
  101. <Layout.Title>
  102. {props.project ? (
  103. <IdBadge
  104. hideName
  105. project={props.project}
  106. avatarSize={28}
  107. avatarProps={{hasTooltip: true, tooltip: props.project.slug}}
  108. />
  109. ) : null}
  110. {props.transaction}
  111. </Layout.Title>
  112. </Layout.HeaderContent>
  113. {transactionSummaryTarget && (
  114. <Layout.HeaderActions>
  115. <LinkButton to={transactionSummaryTarget} size="sm">
  116. {t('View Transaction Summary')}
  117. </LinkButton>
  118. </Layout.HeaderActions>
  119. )}
  120. </Layout.Header>
  121. );
  122. }
  123. interface ProfileFiltersProps {
  124. location: Location;
  125. organization: Organization;
  126. projectIds: EventView['project'];
  127. query: string;
  128. selection: PageFilters;
  129. transaction: string | undefined;
  130. usingTransactions: boolean;
  131. }
  132. function ProfileFilters(props: ProfileFiltersProps) {
  133. const filtersQuery = useMemo(() => {
  134. // To avoid querying for the filters each time the query changes,
  135. // do not pass the user query to get the filters.
  136. const search = new MutableSearch('');
  137. if (defined(props.transaction)) {
  138. search.setFilterValues('transaction_name', [props.transaction]);
  139. }
  140. return search.formatString();
  141. }, [props.transaction]);
  142. const profileFilters = useProfileFilters({
  143. query: filtersQuery,
  144. selection: props.selection,
  145. disabled: props.usingTransactions,
  146. });
  147. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  148. (searchQuery: string) => {
  149. browserHistory.push({
  150. ...props.location,
  151. query: {
  152. ...props.location.query,
  153. query: searchQuery || undefined,
  154. cursor: undefined,
  155. },
  156. });
  157. },
  158. [props.location]
  159. );
  160. return (
  161. <ActionBar>
  162. <PageFilterBar condensed>
  163. <EnvironmentPageFilter />
  164. <DatePageFilter alignDropdown="left" />
  165. </PageFilterBar>
  166. {props.usingTransactions ? (
  167. <SearchBar
  168. searchSource="profile_summary"
  169. organization={props.organization}
  170. projectIds={props.projectIds}
  171. query={props.query}
  172. onSearch={handleSearch}
  173. maxQueryLength={MAX_QUERY_LENGTH}
  174. />
  175. ) : (
  176. <SmartSearchBar
  177. organization={props.organization}
  178. hasRecentSearches
  179. searchSource="profile_summary"
  180. supportedTags={profileFilters}
  181. query={props.query}
  182. onSearch={handleSearch}
  183. maxQueryLength={MAX_QUERY_LENGTH}
  184. />
  185. )}
  186. </ActionBar>
  187. );
  188. }
  189. const ActionBar = styled('div')`
  190. display: grid;
  191. gap: ${space(1)};
  192. grid-template-columns: min-content auto;
  193. padding: ${space(1)} ${space(1)};
  194. background-color: ${p => p.theme.background};
  195. `;
  196. interface ProfileSummaryPageProps {
  197. location: Location;
  198. params: {
  199. projectId?: Project['slug'];
  200. };
  201. selection: PageFilters;
  202. }
  203. function ProfileSummaryPage(props: ProfileSummaryPageProps) {
  204. const organization = useOrganization();
  205. const project = useCurrentProjectFromRouteParam();
  206. const profilingUsingTransactions = organization.features.includes(
  207. 'profiling-using-transactions'
  208. );
  209. const transaction = decodeScalar(props.location.query.transaction);
  210. if (!transaction) {
  211. throw new TypeError(
  212. `Profile summary requires a transaction query params, got ${
  213. transaction?.toString() ?? transaction
  214. }`
  215. );
  216. }
  217. const rawQuery = decodeScalar(props.location?.query?.query, '');
  218. const projectIds: number[] = useMemo(() => {
  219. if (!defined(project)) {
  220. return [];
  221. }
  222. const projects = parseInt(project.id, 10);
  223. if (isNaN(projects)) {
  224. return [];
  225. }
  226. return [projects];
  227. }, [project]);
  228. const projectSlugs: string[] = useMemo(() => {
  229. return defined(project) ? [project.slug] : [];
  230. }, [project]);
  231. const query = useMemo(() => {
  232. const search = new MutableSearch(rawQuery);
  233. if (defined(transaction)) {
  234. search.setFilterValues('transaction', [transaction]);
  235. }
  236. // there are no aggregations happening on this page,
  237. // so remove any aggregate filters
  238. Object.keys(search.filters).forEach(field => {
  239. if (isAggregateField(field)) {
  240. search.removeFilter(field);
  241. }
  242. });
  243. return search.formatString();
  244. }, [rawQuery, transaction]);
  245. const {data} = useAggregateFlamegraphQuery({transaction});
  246. const [visualization, setVisualization] = useLocalStorageState<
  247. 'flamegraph' | 'call tree'
  248. >('flamegraph-visualization', 'flamegraph');
  249. const onVisualizationChange = useCallback(
  250. (value: 'flamegraph' | 'call tree') => {
  251. setVisualization(value);
  252. },
  253. [setVisualization]
  254. );
  255. const [frameFilter, setFrameFilter] = useLocalStorageState<
  256. 'system' | 'application' | 'all'
  257. >('flamegraph-frame-filter', 'application');
  258. const onFrameFilterChange = useCallback(
  259. (value: 'system' | 'application' | 'all') => {
  260. setFrameFilter(value);
  261. },
  262. [setFrameFilter]
  263. );
  264. const flamegraphFrameFilter: ((frame: Frame) => boolean) | undefined = useMemo(() => {
  265. if (frameFilter === 'all') {
  266. return () => true;
  267. }
  268. if (frameFilter === 'application') {
  269. return frame => frame.is_application;
  270. }
  271. return frame => !frame.is_application;
  272. }, [frameFilter]);
  273. const canvasPoolManager = useMemo(() => new CanvasPoolManager(), []);
  274. const scheduler = useCanvasScheduler(canvasPoolManager);
  275. return (
  276. <SentryDocumentTitle
  277. title={t('Profiling \u2014 Profile Summary')}
  278. orgSlug={organization.slug}
  279. >
  280. <ProfileSummaryContainer>
  281. <PageFiltersContainer
  282. shouldForceProject={defined(project)}
  283. forceProject={project}
  284. specificProjectSlugs={projectSlugs}
  285. defaultSelection={
  286. profilingUsingTransactions
  287. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  288. : undefined
  289. }
  290. >
  291. <ProfileSummaryHeader
  292. organization={organization}
  293. location={props.location}
  294. project={project}
  295. query={query}
  296. transaction={transaction}
  297. />
  298. <ProfileFilters
  299. projectIds={projectIds}
  300. organization={organization}
  301. location={props.location}
  302. query={rawQuery}
  303. selection={props.selection}
  304. transaction={transaction}
  305. usingTransactions={profilingUsingTransactions}
  306. />
  307. <ProfilesSummaryChart
  308. referrer="api.profiling.profile-summary-chart"
  309. query={query}
  310. hideCount
  311. />
  312. <ProfileVisualizationContainer>
  313. <ProfileVisualization>
  314. <ProfileGroupProvider
  315. type="flamegraph"
  316. input={data ?? null}
  317. traceID=""
  318. frameFilter={flamegraphFrameFilter}
  319. >
  320. <FlamegraphStateProvider
  321. initialState={{
  322. preferences: {
  323. sorting: 'alphabetical',
  324. },
  325. }}
  326. >
  327. <FlamegraphThemeProvider>
  328. <FlamegraphProvider>
  329. <AggregateFlamegraphToolbar
  330. scheduler={scheduler}
  331. canvasPoolManager={canvasPoolManager}
  332. visualization={visualization}
  333. onVisualizationChange={onVisualizationChange}
  334. frameFilter={frameFilter}
  335. onFrameFilterChange={onFrameFilterChange}
  336. hideSystemFrames={false}
  337. setHideSystemFrames={() => void 0}
  338. />
  339. {visualization === 'flamegraph' ? (
  340. <AggregateFlamegraph
  341. canvasPoolManager={canvasPoolManager}
  342. scheduler={scheduler}
  343. />
  344. ) : null}
  345. </FlamegraphProvider>
  346. </FlamegraphThemeProvider>
  347. </FlamegraphStateProvider>
  348. </ProfileGroupProvider>
  349. </ProfileVisualization>
  350. <ProfileDigest>
  351. <MostRegressedProfileFunctions transaction={transaction} />
  352. <SlowestProfileFunctions transaction={transaction} />
  353. </ProfileDigest>
  354. </ProfileVisualizationContainer>
  355. </PageFiltersContainer>
  356. </ProfileSummaryContainer>
  357. </SentryDocumentTitle>
  358. );
  359. }
  360. interface AggregateFlamegraphToolbarProps {
  361. canvasPoolManager: CanvasPoolManager;
  362. frameFilter: 'system' | 'application' | 'all';
  363. hideSystemFrames: boolean;
  364. onFrameFilterChange: (value: 'system' | 'application' | 'all') => void;
  365. onVisualizationChange: (value: 'flamegraph' | 'call tree') => void;
  366. scheduler: CanvasScheduler;
  367. setHideSystemFrames: (value: boolean) => void;
  368. visualization: 'flamegraph' | 'call tree';
  369. }
  370. function AggregateFlamegraphToolbar(props: AggregateFlamegraphToolbarProps) {
  371. const flamegraph = useFlamegraph();
  372. const flamegraphs = useMemo(() => [flamegraph], [flamegraph]);
  373. const spans = useMemo(() => [], []);
  374. const frameSelectOptions: SelectOption<'system' | 'application' | 'all'>[] =
  375. useMemo(() => {
  376. return [
  377. {value: 'system', label: t('System Frames')},
  378. {value: 'application', label: t('Application Frames')},
  379. {value: 'all', label: t('All Frames')},
  380. ];
  381. }, []);
  382. const onResetZoom = useCallback(() => {
  383. props.scheduler.dispatch('reset zoom');
  384. }, [props.scheduler]);
  385. const onFrameFilterChange = useCallback(
  386. (value: {value: 'application' | 'system' | 'all'}) => {
  387. props.onFrameFilterChange(value.value);
  388. },
  389. [props]
  390. );
  391. return (
  392. <AggregateFlamegraphToolbarContainer>
  393. <SegmentedControl
  394. aria-label={t('View')}
  395. size="xs"
  396. value={props.visualization}
  397. onChange={props.onVisualizationChange}
  398. >
  399. <SegmentedControl.Item key="flamegraph">{t('Flamegraph')}</SegmentedControl.Item>
  400. <SegmentedControl.Item key="call tree">{t('Call Tree')}</SegmentedControl.Item>
  401. </SegmentedControl>
  402. <AggregateFlamegraphSearch
  403. spans={spans}
  404. canvasPoolManager={props.canvasPoolManager}
  405. flamegraphs={flamegraphs}
  406. />
  407. <Button size="xs" onClick={onResetZoom}>
  408. {t('Reset Zoom')}
  409. </Button>
  410. <CompactSelect
  411. onChange={onFrameFilterChange}
  412. value={props.frameFilter}
  413. size="xs"
  414. options={frameSelectOptions}
  415. />
  416. </AggregateFlamegraphToolbarContainer>
  417. );
  418. }
  419. const AggregateFlamegraphToolbarContainer = styled('div')`
  420. display: flex;
  421. justify-content: space-between;
  422. gap: ${space(1)};
  423. padding: ${space(0.5)};
  424. background: ${p => p.theme.background};
  425. `;
  426. const AggregateFlamegraphSearch = styled(FlamegraphSearch)`
  427. max-width: 300px;
  428. `;
  429. const ProfileVisualization = styled('div')`
  430. grid-area: visualization;
  431. `;
  432. const ProfileDigest = styled('div')`
  433. grid-area: digest;
  434. `;
  435. const ProfileVisualizationContainer = styled('div')`
  436. display: grid;
  437. grid-template-areas: 'visualization digest';
  438. flex: 1 1 100%;
  439. `;
  440. const ProfileSummaryContainer = styled('div')`
  441. display: flex;
  442. flex-direction: column;
  443. flex: 1 1 100%;
  444. /*
  445. * The footer component is a sibling of this div.
  446. * Remove it so the flamegraph can take up the
  447. * entire screen.
  448. */
  449. ~ footer {
  450. display: none;
  451. }
  452. `;
  453. export default function ProfileSummaryPageToggle(props: ProfileSummaryPageProps) {
  454. const organization = useOrganization();
  455. if (organization.features.includes('profiling-summary-redesign')) {
  456. return (
  457. <ProfileSummaryContainer data-test-id="profile-summary-redesign">
  458. <ErrorBoundary>
  459. <ProfileSummaryPage {...props} />
  460. </ErrorBoundary>
  461. </ProfileSummaryContainer>
  462. );
  463. }
  464. return (
  465. <div data-test-id="profile-summary-legacy">
  466. <LegacySummaryPage {...props} />
  467. </div>
  468. );
  469. }