slowestProfileFunctions.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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: any, pathname: any, query: any) =>
  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: any) => 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 organization = useOrganization();
  154. const frame = useMemo(() => {
  155. return new Frame(
  156. {
  157. key: 0,
  158. name: props.func.function as string,
  159. package: props.func.package as string,
  160. },
  161. // Ensures that the frame runs through the normalization code path
  162. props.project?.platform && /node|javascript/.test(props.project.platform)
  163. ? props.project.platform
  164. : undefined,
  165. 'aggregate'
  166. );
  167. }, [props.func, props.project]);
  168. let rendered = <TextTruncateOverflow>{frame.name}</TextTruncateOverflow>;
  169. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  170. const example = props.func['all_examples()']?.[0];
  171. if (defined(example)) {
  172. const target = generateProfileRouteFromProfileReference({
  173. organization,
  174. projectSlug: props.project?.slug ?? '',
  175. frameName: frame.name,
  176. framePackage: frame.package as string,
  177. reference: example,
  178. });
  179. rendered = (
  180. <Link onClick={props.onSlowestFunctionClick} to={target}>
  181. {rendered}
  182. </Link>
  183. );
  184. }
  185. return (
  186. <SlowestFunctionRow>
  187. <SlowestFunctionMainRow>
  188. <div>{rendered}</div>
  189. <div>
  190. <PerformanceDuration nanoseconds={props.func['sum()'] as number} abbreviation />
  191. </div>
  192. </SlowestFunctionMainRow>
  193. <SlowestFunctionMetricsRow>
  194. <div>
  195. <TextTruncateOverflow>{frame.package}</TextTruncateOverflow>
  196. </div>
  197. <div>
  198. <Count value={props.func['count()'] as number} />{' '}
  199. {tn('time', 'times', props.func['count()'])}
  200. {', '}
  201. <PerformanceDuration nanoseconds={props.func['p75()'] as number} abbreviation />
  202. </div>
  203. </SlowestFunctionMetricsRow>
  204. </SlowestFunctionRow>
  205. );
  206. }
  207. const SlowestFunctionsList = styled('div')`
  208. flex-basis: 100%;
  209. overflow: auto;
  210. min-height: 0;
  211. `;
  212. const SlowestFunctionsContainer = styled('div')`
  213. margin-top: ${space(0.5)};
  214. min-height: 0;
  215. display: flex;
  216. flex-direction: column;
  217. padding: 0 ${space(1)};
  218. border-bottom: 1px solid ${p => p.theme.border};
  219. `;
  220. const SlowestFunctionsPagination = styled(Pagination)`
  221. margin: 0;
  222. button {
  223. height: 16px;
  224. width: 16px;
  225. min-width: 16px;
  226. min-height: 16px;
  227. svg {
  228. width: 10px;
  229. height: 10px;
  230. }
  231. }
  232. `;
  233. const SlowestFunctionsTitleContainer = styled('div')`
  234. display: flex;
  235. align-items: center;
  236. justify-content: space-between;
  237. margin-bottom: ${space(1)};
  238. `;
  239. const SlowestFunctionsTypeSelect = styled(CompactSelect)`
  240. button {
  241. margin: 0;
  242. padding: 0;
  243. }
  244. `;
  245. const SlowestFunctionsQueryState = styled('div')`
  246. text-align: center;
  247. padding: ${space(2)} ${space(0.5)};
  248. color: ${p => p.theme.subText};
  249. `;
  250. const SlowestFunctionRow = styled('div')`
  251. margin-bottom: ${space(1)};
  252. `;
  253. const SlowestFunctionMainRow = styled('div')`
  254. display: flex;
  255. align-items: center;
  256. justify-content: space-between;
  257. > div:first-child {
  258. min-width: 0;
  259. }
  260. > div:last-child {
  261. white-space: nowrap;
  262. }
  263. `;
  264. const SlowestFunctionMetricsRow = styled('div')`
  265. display: flex;
  266. align-items: center;
  267. justify-content: space-between;
  268. font-size: ${p => p.theme.fontSizeSmall};
  269. color: ${p => p.theme.subText};
  270. margin-top: ${space(0.25)};
  271. `;
  272. const TRIGGER_PROPS = {borderless: true, size: 'zero' as const};
  273. const SLOWEST_FUNCTION_OPTIONS: Array<SelectOption<'application' | 'system' | 'all'>> = [
  274. {
  275. label: t('Slowest Application Functions'),
  276. value: 'application' as const,
  277. },
  278. {
  279. label: t('Slowest System Functions'),
  280. value: 'system' as const,
  281. },
  282. {
  283. label: t('Slowest Functions'),
  284. value: 'all' as const,
  285. },
  286. ];