slowestProfileFunctions.tsx 9.2 KB

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