slowestProfileFunctions.tsx 9.1 KB

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