slowestProfileFunctions.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import {useCallback, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {SelectOption} from 'sentry/components/compactSelect';
  4. import {CompactSelect} from 'sentry/components/compactSelect';
  5. import Count from 'sentry/components/count';
  6. import Link from 'sentry/components/links/link';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import Pagination from 'sentry/components/pagination';
  9. import PerformanceDuration from 'sentry/components/performanceDuration';
  10. import {TextTruncateOverflow} from 'sentry/components/profiling/textTruncateOverflow';
  11. import {t, tn} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization} from 'sentry/types/organization';
  14. import type {Project} from 'sentry/types/project';
  15. import {defined} from 'sentry/utils';
  16. import {trackAnalytics} from 'sentry/utils/analytics';
  17. import {Frame} from 'sentry/utils/profiling/frame';
  18. import type {EventsResultsDataRow} from 'sentry/utils/profiling/hooks/types';
  19. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  20. import {useProfileFunctions} from 'sentry/utils/profiling/hooks/useProfileFunctions';
  21. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  22. import {generateProfileRouteFromProfileReference} from 'sentry/utils/profiling/routes';
  23. import {decodeScalar} from 'sentry/utils/queryString';
  24. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import {useNavigate} from 'sentry/utils/useNavigate';
  27. import useOrganization from 'sentry/utils/useOrganization';
  28. const SLOWEST_FUNCTIONS_LIMIT = 15;
  29. const SLOWEST_FUNCTIONS_CURSOR = 'functionsCursor';
  30. const functionsFields = [
  31. 'package',
  32. 'function',
  33. 'count()',
  34. 'p75()',
  35. 'sum()',
  36. 'all_examples()',
  37. ] as const;
  38. type FunctionsField = (typeof functionsFields)[number];
  39. interface SlowestProfileFunctionsProps {
  40. transaction: string;
  41. }
  42. export function SlowestProfileFunctions(props: SlowestProfileFunctionsProps) {
  43. const navigate = useNavigate();
  44. const location = useLocation();
  45. const organization = useOrganization();
  46. const project = useCurrentProjectFromRouteParam();
  47. const [functionType, setFunctionType] = useState<'application' | 'system' | 'all'>(
  48. 'application'
  49. );
  50. const functionsCursor = useMemo(
  51. () => decodeScalar(location.query[SLOWEST_FUNCTIONS_CURSOR]),
  52. [location.query]
  53. );
  54. const functionsSort = useMemo(
  55. () =>
  56. formatSort<FunctionsField>(
  57. decodeScalar(location.query.functionsSort),
  58. functionsFields,
  59. {
  60. key: 'sum()',
  61. order: 'desc',
  62. }
  63. ),
  64. [location.query.functionsSort]
  65. );
  66. const handleFunctionsCursor = useCallback(
  67. (cursor, pathname, query) =>
  68. navigate({
  69. pathname,
  70. query: {...query, [SLOWEST_FUNCTIONS_CURSOR]: cursor},
  71. }),
  72. [navigate]
  73. );
  74. const query = useMemo(() => {
  75. const conditions = new MutableSearch('');
  76. conditions.setFilterValues('transaction', [props.transaction]);
  77. if (functionType === 'application') {
  78. conditions.setFilterValues('is_application', ['1']);
  79. } else if (functionType === 'system') {
  80. conditions.setFilterValues('is_application', ['0']);
  81. }
  82. return conditions.formatString();
  83. }, [functionType, props.transaction]);
  84. const functionsQuery = useProfileFunctions<FunctionsField>({
  85. fields: functionsFields,
  86. referrer: 'api.profiling.profile-summary-functions-table',
  87. sort: functionsSort,
  88. query,
  89. limit: SLOWEST_FUNCTIONS_LIMIT,
  90. cursor: functionsCursor,
  91. });
  92. const onChangeFunctionType = useCallback(v => setFunctionType(v.value), []);
  93. const functions = functionsQuery.data?.data ?? [];
  94. const onSlowestFunctionClick = useCallback(() => {
  95. trackAnalytics('profiling_views.go_to_flamegraph', {
  96. organization,
  97. source: `profiling_transaction.slowest_functions_table`,
  98. });
  99. }, [organization]);
  100. return (
  101. <SlowestFunctionsContainer>
  102. <SlowestFunctionsTitleContainer>
  103. <SlowestFunctionsTypeSelect
  104. value={functionType}
  105. options={SLOWEST_FUNCTION_OPTIONS}
  106. onChange={onChangeFunctionType}
  107. triggerProps={TRIGGER_PROPS}
  108. offset={4}
  109. />
  110. <SlowestFunctionsPagination
  111. pageLinks={functionsQuery.getResponseHeader?.('Link')}
  112. onCursor={handleFunctionsCursor}
  113. size="xs"
  114. />
  115. </SlowestFunctionsTitleContainer>
  116. <SlowestFunctionsList>
  117. {functionsQuery.isPending ? (
  118. <SlowestFunctionsQueryState>
  119. <LoadingIndicator size={36} />
  120. </SlowestFunctionsQueryState>
  121. ) : functionsQuery.isError ? (
  122. <SlowestFunctionsQueryState>
  123. {t('Failed to fetch slowest functions')}
  124. </SlowestFunctionsQueryState>
  125. ) : !functions.length ? (
  126. <SlowestFunctionsQueryState>
  127. {t('The fastest code is one that never runs.')}
  128. </SlowestFunctionsQueryState>
  129. ) : (
  130. functions.map((fn, i) => {
  131. return (
  132. <SlowestFunctionEntry
  133. key={i}
  134. func={fn}
  135. organization={organization}
  136. project={project}
  137. onSlowestFunctionClick={onSlowestFunctionClick}
  138. />
  139. );
  140. })
  141. )}
  142. </SlowestFunctionsList>
  143. </SlowestFunctionsContainer>
  144. );
  145. }
  146. interface SlowestFunctionEntryProps {
  147. func: EventsResultsDataRow<'function' | 'package' | 'count()' | 'p75()' | 'sum()'>;
  148. onSlowestFunctionClick: () => void;
  149. organization: Organization;
  150. project: Project | null;
  151. }
  152. function SlowestFunctionEntry(props: SlowestFunctionEntryProps) {
  153. const frame = useMemo(() => {
  154. return new Frame(
  155. {
  156. key: 0,
  157. name: props.func.function as string,
  158. package: props.func.package as string,
  159. },
  160. // Ensures that the frame runs through the normalization code path
  161. props.project?.platform && /node|javascript/.test(props.project.platform)
  162. ? props.project.platform
  163. : undefined,
  164. 'aggregate'
  165. );
  166. }, [props.func, props.project]);
  167. let rendered = <TextTruncateOverflow>{frame.name}</TextTruncateOverflow>;
  168. const example = props.func['all_examples()']?.[0];
  169. if (defined(example)) {
  170. const target = generateProfileRouteFromProfileReference({
  171. orgSlug: props.organization.slug,
  172. projectSlug: props.project?.slug ?? '',
  173. frameName: frame.name as string,
  174. framePackage: frame.package as string,
  175. reference: example,
  176. });
  177. rendered = (
  178. <Link onClick={props.onSlowestFunctionClick} to={target}>
  179. {rendered}
  180. </Link>
  181. );
  182. }
  183. return (
  184. <SlowestFunctionRow>
  185. <SlowestFunctionMainRow>
  186. <div>{rendered}</div>
  187. <div>
  188. <PerformanceDuration nanoseconds={props.func['sum()'] as number} abbreviation />
  189. </div>
  190. </SlowestFunctionMainRow>
  191. <SlowestFunctionMetricsRow>
  192. <div>
  193. <TextTruncateOverflow>{frame.package}</TextTruncateOverflow>
  194. </div>
  195. <div>
  196. <Count value={props.func['count()'] as number} />{' '}
  197. {tn('time', 'times', props.func['count()'])}
  198. {', '}
  199. <PerformanceDuration nanoseconds={props.func['p75()'] as number} abbreviation />
  200. </div>
  201. </SlowestFunctionMetricsRow>
  202. </SlowestFunctionRow>
  203. );
  204. }
  205. const SlowestFunctionsList = styled('div')`
  206. flex-basis: 100%;
  207. overflow: auto;
  208. min-height: 0;
  209. `;
  210. const SlowestFunctionsContainer = styled('div')`
  211. margin-top: ${space(0.5)};
  212. min-height: 0;
  213. display: flex;
  214. flex-direction: column;
  215. padding: 0 ${space(1)};
  216. border-bottom: 1px solid ${p => p.theme.border};
  217. `;
  218. const SlowestFunctionsPagination = styled(Pagination)`
  219. margin: 0;
  220. button {
  221. height: 16px;
  222. width: 16px;
  223. min-width: 16px;
  224. min-height: 16px;
  225. svg {
  226. width: 10px;
  227. height: 10px;
  228. }
  229. }
  230. `;
  231. const SlowestFunctionsTitleContainer = styled('div')`
  232. display: flex;
  233. align-items: center;
  234. justify-content: space-between;
  235. margin-bottom: ${space(1)};
  236. `;
  237. const SlowestFunctionsTypeSelect = styled(CompactSelect)`
  238. button {
  239. margin: 0;
  240. padding: 0;
  241. }
  242. `;
  243. const SlowestFunctionsQueryState = styled('div')`
  244. text-align: center;
  245. padding: ${space(2)} ${space(0.5)};
  246. color: ${p => p.theme.subText};
  247. `;
  248. const SlowestFunctionRow = styled('div')`
  249. margin-bottom: ${space(1)};
  250. `;
  251. const SlowestFunctionMainRow = styled('div')`
  252. display: flex;
  253. align-items: center;
  254. justify-content: space-between;
  255. > div:first-child {
  256. min-width: 0;
  257. }
  258. > div:last-child {
  259. white-space: nowrap;
  260. }
  261. `;
  262. const SlowestFunctionMetricsRow = styled('div')`
  263. display: flex;
  264. align-items: center;
  265. justify-content: space-between;
  266. font-size: ${p => p.theme.fontSizeSmall};
  267. color: ${p => p.theme.subText};
  268. margin-top: ${space(0.25)};
  269. `;
  270. const TRIGGER_PROPS = {borderless: true, size: 'zero' as const};
  271. const SLOWEST_FUNCTION_OPTIONS: SelectOption<'application' | 'system' | 'all'>[] = [
  272. {
  273. label: t('Slowest Application Functions'),
  274. value: 'application' as const,
  275. },
  276. {
  277. label: t('Slowest System Functions'),
  278. value: 'system' as const,
  279. },
  280. {
  281. label: t('Slowest Functions'),
  282. value: 'all' as const,
  283. },
  284. ];