samplingModeField.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. addSuccessMessage,
  7. } from 'sentry/actionCreators/indicator';
  8. import {Button} from 'sentry/components/button';
  9. import Confirm from 'sentry/components/confirm';
  10. import FieldGroup from 'sentry/components/forms/fieldGroup';
  11. import ExternalLink from 'sentry/components/links/externalLink';
  12. import {t, tct} from 'sentry/locale';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import {useUpdateOrganization} from 'sentry/views/settings/dynamicSampling/utils/useUpdateOrganization';
  15. import {useAccess} from 'sentry/views/settings/projectMetrics/access';
  16. const switchToManualMessage = tct(
  17. 'Switching to manual mode will disable automatic adjustments for your projects. You will be able to set individual sample rates for each project. Those rates will be initially set to their current automatic value. [link:Learn more about sampling]',
  18. // TODO(aknaus): Add link to documentation
  19. {link: <ExternalLink href="https://docs.sentry.io" />}
  20. );
  21. const switchToAutoMessage = tct(
  22. 'Switching to automatic mode will enable automatic adjustments for your projects based on a global rate. By switching [strong:you will lose your manually defined sample rates]. [link:Learn more about sampling]',
  23. // TODO(aknaus): Add link to documentation
  24. {link: <ExternalLink href="https://docs.sentry.io" />, strong: <strong />}
  25. );
  26. export function SamplingModeField() {
  27. const {samplingMode} = useOrganization();
  28. const hasAccess = useAccess({access: ['org:write']});
  29. const {mutate: updateOrganization, isPending} = useUpdateOrganization({
  30. onMutate: () => {
  31. addLoadingMessage(t('Switching sampling mode...'));
  32. },
  33. onSuccess: () => {
  34. addSuccessMessage(t('Changes applied.'));
  35. },
  36. onError: () => {
  37. addErrorMessage(t('Unable to save changes. Please try again.'));
  38. },
  39. });
  40. const handleSwitchMode = () => {
  41. updateOrganization({
  42. samplingMode: samplingMode === 'organization' ? 'project' : 'organization',
  43. });
  44. };
  45. return (
  46. <FieldGroup
  47. disabled={!hasAccess}
  48. label={t('Switch Mode')}
  49. help={
  50. samplingMode === 'organization'
  51. ? t(
  52. 'Take control over the individual sample rates in your projects. This disables automatic adjustments.'
  53. )
  54. : t(
  55. 'Let Sentry monitor span volume and adjust sample rates automatically. This resets the custom rates below.'
  56. )
  57. }
  58. >
  59. <Confirm
  60. disabled={!hasAccess || isPending}
  61. message={
  62. <Fragment>
  63. <strong>{t('Are you sure?')}</strong>
  64. <p>
  65. {samplingMode === 'organization'
  66. ? switchToManualMessage
  67. : switchToAutoMessage}
  68. </p>
  69. </Fragment>
  70. }
  71. header={
  72. <h5>
  73. {samplingMode === 'organization'
  74. ? t('Switch to Manual Mode')
  75. : t('Switch to Automatic Mode')}
  76. </h5>
  77. }
  78. confirmText={t('Switch Mode')}
  79. cancelText={t('Cancel')}
  80. onConfirm={handleSwitchMode}
  81. >
  82. <Button
  83. css={css`
  84. width: max-content;
  85. `}
  86. >
  87. {samplingMode === 'organization' ? t('Switch to Manual') : t('Switch to Auto')}
  88. </Button>
  89. </Confirm>
  90. </FieldGroup>
  91. );
  92. }