projectPerformance.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Access from 'sentry/components/acl/access';
  5. import Feature from 'sentry/components/acl/feature';
  6. import {Button} from 'sentry/components/button';
  7. import Form from 'sentry/components/forms/form';
  8. import JsonForm from 'sentry/components/forms/jsonForm';
  9. import {Field} from 'sentry/components/forms/types';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import LoadingIndicator from 'sentry/components/loadingIndicator';
  12. import {PanelItem} from 'sentry/components/panels';
  13. import {t, tct} from 'sentry/locale';
  14. import ProjectsStore from 'sentry/stores/projectsStore';
  15. import {Organization, Project, Scope} from 'sentry/types';
  16. import {DynamicSamplingBiasType} from 'sentry/types/sampling';
  17. import {trackAnalytics} from 'sentry/utils/analytics';
  18. import routeTitleGen from 'sentry/utils/routeTitle';
  19. import AsyncView from 'sentry/views/asyncView';
  20. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  21. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  22. // These labels need to be exported so that they can be used in audit logs
  23. export const retentionPrioritiesLabels = {
  24. boostLatestRelease: t('Prioritize new releases'),
  25. boostEnvironments: t('Prioritize dev environments'),
  26. boostLowVolumeTransactions: t('Prioritize low-volume transactions'),
  27. ignoreHealthChecks: t('Deprioritize health checks'),
  28. };
  29. type RouteParams = {orgId: string; projectId: string};
  30. type Props = RouteComponentProps<{projectId: string}, {}> & {
  31. organization: Organization;
  32. project: Project;
  33. };
  34. type ProjectThreshold = {
  35. metric: string;
  36. threshold: string;
  37. editedBy?: string;
  38. id?: string;
  39. };
  40. type State = AsyncView['state'] & {
  41. threshold: ProjectThreshold;
  42. };
  43. class ProjectPerformance extends AsyncView<Props, State> {
  44. getTitle() {
  45. const {projectId} = this.props.params;
  46. return routeTitleGen(t('Performance'), projectId, false);
  47. }
  48. getProjectEndpoint({orgId, projectId}: RouteParams) {
  49. return `/projects/${orgId}/${projectId}/`;
  50. }
  51. getPerformanceIssuesEndpoint({orgId, projectId}: RouteParams) {
  52. return `/projects/${orgId}/${projectId}/performance-issues/configure/`;
  53. }
  54. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  55. const {params, organization} = this.props;
  56. const {projectId} = params;
  57. const endpoints: ReturnType<AsyncView['getEndpoints']> = [
  58. [
  59. 'threshold',
  60. `/projects/${organization.slug}/${projectId}/transaction-threshold/configure/`,
  61. ],
  62. ['project', `/projects/${organization.slug}/${projectId}/`],
  63. ];
  64. if (organization.features.includes('performance-issues-dev')) {
  65. const performanceIssuesEndpoint = [
  66. 'performance_issue_settings',
  67. `/projects/${organization.slug}/${projectId}/performance-issues/configure/`,
  68. ] as [string, string];
  69. endpoints.push(performanceIssuesEndpoint);
  70. }
  71. return endpoints;
  72. }
  73. getRetentionPrioritiesData(...data) {
  74. return {
  75. dynamicSamplingBiases: Object.entries(data[1].form).map(([key, value]) => ({
  76. id: key,
  77. active: value,
  78. })),
  79. };
  80. }
  81. handleDelete = () => {
  82. const {projectId} = this.props.params;
  83. const {organization} = this.props;
  84. this.setState({
  85. loading: true,
  86. });
  87. this.api.request(
  88. `/projects/${organization.slug}/${projectId}/transaction-threshold/configure/`,
  89. {
  90. method: 'DELETE',
  91. success: () => {
  92. trackAnalytics('performance_views.project_transaction_threshold.clear', {
  93. organization,
  94. });
  95. },
  96. complete: () => this.fetchData(),
  97. }
  98. );
  99. };
  100. getEmptyMessage() {
  101. return t('There is no threshold set for this project.');
  102. }
  103. renderLoading() {
  104. return (
  105. <LoadingIndicatorContainer>
  106. <LoadingIndicator />
  107. </LoadingIndicatorContainer>
  108. );
  109. }
  110. get formFields(): Field[] {
  111. const fields: Field[] = [
  112. {
  113. name: 'metric',
  114. type: 'select',
  115. label: t('Calculation Method'),
  116. options: [
  117. {value: 'duration', label: t('Transaction Duration')},
  118. {value: 'lcp', label: t('Largest Contentful Paint')},
  119. ],
  120. help: tct(
  121. 'This determines which duration is used to set your thresholds. By default, we use transaction duration which measures the entire length of the transaction. You can also set this to use a [link:Web Vital].',
  122. {
  123. link: (
  124. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/" />
  125. ),
  126. }
  127. ),
  128. },
  129. {
  130. name: 'threshold',
  131. type: 'string',
  132. label: t('Response Time Threshold (ms)'),
  133. placeholder: t('300'),
  134. help: tct(
  135. 'Define what a satisfactory response time is based on the calculation method above. This will affect how your [link1:Apdex] and [link2:User Misery] thresholds are calculated. For example, misery will be 4x your satisfactory response time.',
  136. {
  137. link1: (
  138. <ExternalLink href="https://docs.sentry.io/performance-monitoring/performance/metrics/#apdex" />
  139. ),
  140. link2: (
  141. <ExternalLink href="https://docs.sentry.io/product/performance/metrics/#user-misery" />
  142. ),
  143. }
  144. ),
  145. },
  146. ];
  147. return fields;
  148. }
  149. get performanceIssueFormFields(): Field[] {
  150. return [
  151. {
  152. name: 'performanceIssueCreationRate',
  153. type: 'range',
  154. label: t('Performance Issue Creation Rate'),
  155. min: 0.0,
  156. max: 1.0,
  157. step: 0.01,
  158. defaultValue: 0,
  159. help: t(
  160. 'This determines the rate at which performance issues are created. A rate of 0.0 will disable performance issue creation.'
  161. ),
  162. },
  163. {
  164. name: 'performanceIssueSendToPlatform',
  165. type: 'boolean',
  166. label: t('Send Occurrences To Platform'),
  167. defaultValue: false,
  168. help: t(
  169. 'This determines whether performance issue occurrences are sent to the issues platform.'
  170. ),
  171. },
  172. {
  173. name: 'performanceIssueCreationThroughPlatform',
  174. type: 'boolean',
  175. label: t('Create Issues Through Issues Platform'),
  176. defaultValue: false,
  177. help: t(
  178. 'This determines whether performance issues are created through the issues platform.'
  179. ),
  180. },
  181. ];
  182. }
  183. get performanceIssueDetectorsFormFields(): Field[] {
  184. return [
  185. {
  186. name: 'n_plus_one_db_detection_rate',
  187. type: 'range',
  188. label: t('N+1 (DB) Detection Rate'),
  189. min: 0.0,
  190. max: 1.0,
  191. step: 0.01,
  192. defaultValue: 0,
  193. },
  194. {
  195. name: 'n_plus_one_db_count',
  196. type: 'number',
  197. label: t('N+1 (DB) Minimum Count'),
  198. min: 0,
  199. max: 1000,
  200. defaultValue: 5,
  201. },
  202. {
  203. name: 'n_plus_one_db_duration_threshold',
  204. type: 'number',
  205. label: t('N+1 (DB) Duration Threshold'),
  206. min: 0,
  207. max: 1000000.0,
  208. defaultValue: 500,
  209. },
  210. {
  211. name: 'n_plus_one_api_calls_detection_rate',
  212. type: 'range',
  213. label: t('N+1 API Calls Detection Rate'),
  214. min: 0.0,
  215. max: 1.0,
  216. step: 0.01,
  217. defaultValue: 0,
  218. },
  219. {
  220. name: 'consecutive_db_queries_detection_rate',
  221. type: 'range',
  222. label: t('Consecutive DB Detection rate'),
  223. min: 0.0,
  224. max: 1.0,
  225. step: 0.01,
  226. defaultValue: 0,
  227. },
  228. {
  229. name: 'consecutive_http_spans_detection_enabled',
  230. type: 'boolean',
  231. label: t('Consecutive HTTP Spans Detection Enabled'),
  232. defaultValue: true,
  233. },
  234. ];
  235. }
  236. get retentionPrioritiesFormFields(): Field[] {
  237. return [
  238. {
  239. name: 'boostLatestRelease',
  240. type: 'boolean',
  241. label: retentionPrioritiesLabels.boostLatestRelease,
  242. help: t(
  243. 'Captures more transactions for your new releases as they are being adopted'
  244. ),
  245. getData: this.getRetentionPrioritiesData,
  246. },
  247. {
  248. name: 'boostEnvironments',
  249. type: 'boolean',
  250. label: retentionPrioritiesLabels.boostEnvironments,
  251. help: t(
  252. 'Captures more traces from environments that contain "debug", "dev", "local", "qa", and "test"'
  253. ),
  254. getData: this.getRetentionPrioritiesData,
  255. },
  256. {
  257. name: 'boostLowVolumeTransactions',
  258. type: 'boolean',
  259. label: retentionPrioritiesLabels.boostLowVolumeTransactions,
  260. help: t("Balance high-volume endpoints so they don't drown out low-volume ones"),
  261. visible: this.props.organization.features.includes(
  262. 'dynamic-sampling-transaction-name-priority'
  263. ),
  264. getData: this.getRetentionPrioritiesData,
  265. },
  266. {
  267. name: 'ignoreHealthChecks',
  268. type: 'boolean',
  269. label: retentionPrioritiesLabels.ignoreHealthChecks,
  270. help: t('Captures fewer of your health checks transactions'),
  271. getData: this.getRetentionPrioritiesData,
  272. },
  273. ];
  274. }
  275. get initialData() {
  276. const {threshold} = this.state;
  277. return {
  278. threshold: threshold.threshold,
  279. metric: threshold.metric,
  280. };
  281. }
  282. renderBody() {
  283. const {organization, project} = this.props;
  284. const endpoint = `/projects/${organization.slug}/${project.slug}/transaction-threshold/configure/`;
  285. const requiredScopes: Scope[] = ['project:write'];
  286. const params = {orgId: organization.slug, projectId: project.slug};
  287. const projectEndpoint = this.getProjectEndpoint(params);
  288. const performanceIssuesEndpoint = this.getPerformanceIssuesEndpoint(params);
  289. return (
  290. <Fragment>
  291. <SettingsPageHeader title={t('Performance')} />
  292. <PermissionAlert project={project} />
  293. <Form
  294. saveOnBlur
  295. allowUndo
  296. initialData={this.initialData}
  297. apiMethod="POST"
  298. apiEndpoint={endpoint}
  299. onSubmitSuccess={resp => {
  300. const initial = this.initialData;
  301. const changedThreshold = initial.metric === resp.metric;
  302. trackAnalytics('performance_views.project_transaction_threshold.change', {
  303. organization,
  304. from: changedThreshold ? initial.threshold : initial.metric,
  305. to: changedThreshold ? resp.threshold : resp.metric,
  306. key: changedThreshold ? 'threshold' : 'metric',
  307. });
  308. this.setState({threshold: resp});
  309. }}
  310. >
  311. <Access access={requiredScopes} project={project}>
  312. {({hasAccess}) => (
  313. <JsonForm
  314. title={t('General')}
  315. fields={this.formFields}
  316. disabled={!hasAccess}
  317. renderFooter={() => (
  318. <Actions>
  319. <Button onClick={() => this.handleDelete()}>{t('Reset All')}</Button>
  320. </Actions>
  321. )}
  322. />
  323. )}
  324. </Access>
  325. </Form>
  326. <Feature features={['organizations:dynamic-sampling']}>
  327. <Form
  328. saveOnBlur
  329. allowUndo
  330. initialData={
  331. project.dynamicSamplingBiases?.reduce((acc, bias) => {
  332. acc[bias.id] = bias.active;
  333. return acc;
  334. }, {}) ?? {}
  335. }
  336. onSubmitSuccess={(response, _instance, id, change) => {
  337. ProjectsStore.onUpdateSuccess(response);
  338. trackAnalytics(
  339. change?.new === true
  340. ? 'dynamic_sampling_settings.priority_enabled'
  341. : 'dynamic_sampling_settings.priority_disabled',
  342. {
  343. organization,
  344. project_id: project.id,
  345. id: id as DynamicSamplingBiasType,
  346. }
  347. );
  348. }}
  349. apiMethod="PUT"
  350. apiEndpoint={projectEndpoint}
  351. >
  352. <Access access={requiredScopes} project={project}>
  353. {({hasAccess}) => (
  354. <JsonForm
  355. title={t('Retention Priorities')}
  356. fields={this.retentionPrioritiesFormFields}
  357. disabled={!hasAccess}
  358. renderFooter={() => (
  359. <Actions>
  360. <Button
  361. external
  362. href="https://docs.sentry.io/product/performance/performance-at-scale/"
  363. >
  364. {t('Read docs')}
  365. </Button>
  366. </Actions>
  367. )}
  368. />
  369. )}
  370. </Access>
  371. </Form>
  372. </Feature>
  373. <Feature features={['organizations:performance-issues-dev']}>
  374. <Fragment>
  375. <Form
  376. saveOnBlur
  377. allowUndo
  378. initialData={{
  379. performanceIssueCreationRate:
  380. this.state.project.performanceIssueCreationRate,
  381. performanceIssueSendToPlatform:
  382. this.state.project.performanceIssueSendToPlatform,
  383. performanceIssueCreationThroughPlatform:
  384. this.state.project.performanceIssueCreationThroughPlatform,
  385. }}
  386. apiMethod="PUT"
  387. apiEndpoint={projectEndpoint}
  388. >
  389. <Access access={requiredScopes} project={project}>
  390. {({hasAccess}) => (
  391. <JsonForm
  392. title={t('Performance Issues - All')}
  393. fields={this.performanceIssueFormFields}
  394. disabled={!hasAccess}
  395. />
  396. )}
  397. </Access>
  398. </Form>
  399. <Form
  400. saveOnBlur
  401. allowUndo
  402. initialData={this.state.performance_issue_settings}
  403. apiMethod="PUT"
  404. apiEndpoint={performanceIssuesEndpoint}
  405. >
  406. <Access access={requiredScopes} project={project}>
  407. {({hasAccess}) => (
  408. <JsonForm
  409. title={t('Performance Issues - Detector Settings')}
  410. fields={this.performanceIssueDetectorsFormFields}
  411. disabled={!hasAccess}
  412. />
  413. )}
  414. </Access>
  415. </Form>
  416. </Fragment>
  417. </Feature>
  418. </Fragment>
  419. );
  420. }
  421. }
  422. const Actions = styled(PanelItem)`
  423. justify-content: flex-end;
  424. `;
  425. const LoadingIndicatorContainer = styled('div')`
  426. margin: 18px 18px 0;
  427. `;
  428. export default ProjectPerformance;