slowestProfileFunctions.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {useCallback, useEffect, useMemo, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Sentry from '@sentry/react';
  5. import {CompactSelect, SelectOption} 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} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  16. import {useProfileFunctions} from 'sentry/utils/profiling/hooks/useProfileFunctions';
  17. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  18. import {generateProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
  19. import {decodeScalar} from 'sentry/utils/queryString';
  20. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. const SLOWEST_FUNCTIONS_LIMIT = 15;
  24. const SLOWEST_FUNCTIONS_CURSOR = 'functionsCursor';
  25. const functionsFields = [
  26. 'package',
  27. 'function',
  28. 'count()',
  29. 'p75()',
  30. 'sum()',
  31. 'examples()',
  32. ] as const;
  33. type FunctionsField = (typeof functionsFields)[number];
  34. interface SlowestProfileFunctionsProps {
  35. transaction: string;
  36. }
  37. export function SlowestProfileFunctions(props: SlowestProfileFunctionsProps) {
  38. const organization = useOrganization();
  39. const project = useCurrentProjectFromRouteParam();
  40. const [functionType, setFunctionType] = useState<'application' | 'system' | 'all'>(
  41. 'application'
  42. );
  43. const location = useLocation();
  44. const functionsCursor = useMemo(
  45. () => decodeScalar(location.query[SLOWEST_FUNCTIONS_CURSOR]),
  46. [location.query]
  47. );
  48. const functionsSort = useMemo(
  49. () =>
  50. formatSort<FunctionsField>(
  51. decodeScalar(location.query.functionsSort),
  52. functionsFields,
  53. {
  54. key: 'sum()',
  55. order: 'desc',
  56. }
  57. ),
  58. [location.query.functionsSort]
  59. );
  60. const handleFunctionsCursor = useCallback((cursor, pathname, query) => {
  61. browserHistory.push({
  62. pathname,
  63. query: {...query, [SLOWEST_FUNCTIONS_CURSOR]: cursor},
  64. });
  65. }, []);
  66. const query = useMemo(() => {
  67. const conditions = new MutableSearch('');
  68. conditions.setFilterValues('transaction', [props.transaction]);
  69. if (functionType === 'application') {
  70. conditions.setFilterValues('is_application', ['1']);
  71. } else if (functionType === 'system') {
  72. conditions.setFilterValues('is_application', ['0']);
  73. }
  74. return conditions.formatString();
  75. }, [functionType, props.transaction]);
  76. const functionsQuery = useProfileFunctions<FunctionsField>({
  77. fields: functionsFields,
  78. referrer: 'api.profiling.profile-summary-functions-table',
  79. sort: functionsSort,
  80. query,
  81. limit: SLOWEST_FUNCTIONS_LIMIT,
  82. cursor: functionsCursor,
  83. });
  84. useEffect(() => {
  85. if (
  86. functionsQuery.isLoading ||
  87. functionsQuery.isError ||
  88. functionsQuery.data?.data?.length > 0
  89. ) {
  90. return;
  91. }
  92. Sentry.captureMessage('No regressed functions detected for flamegraph');
  93. }, [functionsQuery.data, functionsQuery.isLoading, functionsQuery.isError]);
  94. const onChangeFunctionType = useCallback(v => setFunctionType(v.value), []);
  95. const functions = functionsQuery.data?.data ?? [];
  96. const onSlowestFunctionClick = useCallback(() => {
  97. trackAnalytics('profiling_views.go_to_flamegraph', {
  98. organization,
  99. source: `profiling_transaction.slowest_functions_table`,
  100. });
  101. }, [organization]);
  102. return (
  103. <SlowestFunctionsContainer>
  104. <SlowestFunctionsTitleContainer>
  105. <SlowestFunctionsTypeSelect
  106. value={functionType}
  107. options={SLOWEST_FUNCTION_OPTIONS}
  108. onChange={onChangeFunctionType}
  109. triggerProps={TRIGGER_PROPS}
  110. offset={4}
  111. />
  112. <SlowestFunctionsPagination
  113. pageLinks={functionsQuery.getResponseHeader?.('Link')}
  114. onCursor={handleFunctionsCursor}
  115. size="xs"
  116. />
  117. </SlowestFunctionsTitleContainer>
  118. <SlowestFunctionsList>
  119. {functionsQuery.isLoading ? (
  120. <SlowestFunctionsQueryState>
  121. <LoadingIndicator size={36} />
  122. </SlowestFunctionsQueryState>
  123. ) : functionsQuery.isError ? (
  124. <SlowestFunctionsQueryState>
  125. {t('Failed to fetch slowest functions')}
  126. </SlowestFunctionsQueryState>
  127. ) : !functions.length ? (
  128. <SlowestFunctionsQueryState>
  129. {t('Yikes, you have no slow functions? This should not happen.')}
  130. </SlowestFunctionsQueryState>
  131. ) : (
  132. functions.map((fn, i) => {
  133. return (
  134. <SlowestFunctionRow key={i}>
  135. <SlowestFunctionMainRow>
  136. <div>
  137. <Link
  138. onClick={onSlowestFunctionClick}
  139. to={generateProfileFlamechartRouteWithQuery({
  140. orgSlug: organization.slug,
  141. projectSlug: project?.slug ?? '',
  142. profileId: (fn['examples()']?.[0] as string) ?? '',
  143. query: {
  144. // specify the frame to focus, the flamegraph will switch
  145. // to the appropriate thread when these are specified
  146. frameName: fn.function as string,
  147. framePackage: fn.package as string,
  148. },
  149. })}
  150. >
  151. <TextTruncateOverflow>{fn.function}</TextTruncateOverflow>
  152. </Link>
  153. </div>
  154. <div>
  155. <PerformanceDuration
  156. nanoseconds={fn['sum()'] as number}
  157. abbreviation
  158. />
  159. </div>
  160. </SlowestFunctionMainRow>
  161. <SlowestFunctionMetricsRow>
  162. <div>
  163. <TextTruncateOverflow>{fn.package}</TextTruncateOverflow>
  164. </div>
  165. <div>
  166. <Count value={fn['count()'] as number} />
  167. {', '}
  168. <PerformanceDuration
  169. nanoseconds={fn['p75()'] as number}
  170. abbreviation
  171. />
  172. </div>
  173. </SlowestFunctionMetricsRow>
  174. </SlowestFunctionRow>
  175. );
  176. })
  177. )}
  178. </SlowestFunctionsList>
  179. </SlowestFunctionsContainer>
  180. );
  181. }
  182. const SlowestFunctionsList = styled('div')`
  183. flex-basis: 100%;
  184. overflow: auto;
  185. min-height: 0;
  186. `;
  187. const SlowestFunctionsContainer = styled('div')`
  188. margin-top: ${space(0.5)};
  189. min-height: 0;
  190. display: flex;
  191. flex-direction: column;
  192. `;
  193. const SlowestFunctionsPagination = styled(Pagination)`
  194. margin: 0;
  195. `;
  196. const SlowestFunctionsTitleContainer = styled('div')`
  197. display: flex;
  198. align-items: center;
  199. justify-content: space-between;
  200. margin-bottom: ${space(1)};
  201. `;
  202. const SlowestFunctionsTypeSelect = styled(CompactSelect)`
  203. button {
  204. margin: 0;
  205. padding: 0;
  206. }
  207. `;
  208. const SlowestFunctionsQueryState = styled('div')`
  209. text-align: center;
  210. padding: ${space(2)} ${space(0.5)};
  211. color: ${p => p.theme.subText};
  212. `;
  213. const SlowestFunctionRow = styled('div')`
  214. margin-bottom: ${space(1)};
  215. `;
  216. const SlowestFunctionMainRow = styled('div')`
  217. display: flex;
  218. align-items: center;
  219. justify-content: space-between;
  220. `;
  221. const SlowestFunctionMetricsRow = styled('div')`
  222. display: flex;
  223. align-items: center;
  224. justify-content: space-between;
  225. font-size: ${p => p.theme.fontSizeSmall};
  226. color: ${p => p.theme.subText};
  227. margin-top: ${space(0.25)};
  228. `;
  229. const TRIGGER_PROPS = {borderless: true, size: 'zero' as const};
  230. const SLOWEST_FUNCTION_OPTIONS: SelectOption<'application' | 'system' | 'all'>[] = [
  231. {
  232. label: t('Slowest Application Functions'),
  233. value: 'application' as const,
  234. },
  235. {
  236. label: t('Slowest System Functions'),
  237. value: 'system' as const,
  238. },
  239. {
  240. label: t('Slowest Functions'),
  241. value: 'all' as const,
  242. },
  243. ];