cardinalityLimit.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import styled from '@emotion/styled';
  2. import Access from 'sentry/components/acl/access';
  3. import NumberField from 'sentry/components/forms/fields/numberField';
  4. import Form from 'sentry/components/forms/form';
  5. import Panel from 'sentry/components/panels/panel';
  6. import PanelBody from 'sentry/components/panels/panelBody';
  7. import PanelHeader from 'sentry/components/panels/panelHeader';
  8. import {t} from 'sentry/locale';
  9. import type {Project} from 'sentry/types/project';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import {DEFAULT_METRICS_CARDINALITY_LIMIT} from '../../../utils/metrics/constants';
  12. type Props = {
  13. project: Project;
  14. };
  15. function transformData(data) {
  16. const limit = data.relayCustomMetricCardinalityLimit;
  17. return {
  18. relayCustomMetricCardinalityLimit:
  19. limit === '' || limit === DEFAULT_METRICS_CARDINALITY_LIMIT.toString()
  20. ? null
  21. : limit,
  22. };
  23. }
  24. export function CardinalityLimit({project}: Props) {
  25. const organization = useOrganization();
  26. const endpoint = `/projects/${organization.slug}/${project.slug}/`;
  27. return (
  28. <Form
  29. apiEndpoint={endpoint}
  30. apiMethod="PUT"
  31. saveOnBlur
  32. initialData={{
  33. relayCustomMetricCardinalityLimit:
  34. project.relayCustomMetricCardinalityLimit ??
  35. DEFAULT_METRICS_CARDINALITY_LIMIT.toString(),
  36. }}
  37. >
  38. <Panel>
  39. <PanelHeader>{t('Limits')}</PanelHeader>
  40. <PanelBody>
  41. <Access access={['project:write']} project={project}>
  42. {({hasAccess}) => (
  43. <StyledNumberField
  44. disabledReason={
  45. !hasAccess
  46. ? t('You do not have permission to edit the cardinality limit')
  47. : undefined
  48. }
  49. disabled={!hasAccess}
  50. name="relayCustomMetricCardinalityLimit"
  51. label={t('Cardinality Limit')}
  52. help={t(
  53. 'The cardinality limit defines the maximum number of unique tag combinations allowed for a metric (measured per hour). If the cardinality limit is exceeded, the metric will be blocked.'
  54. )}
  55. saveOnBlur
  56. placeholder={t('Enter a value')}
  57. flexibleControlStateSize
  58. multiple
  59. getData={transformData}
  60. />
  61. )}
  62. </Access>
  63. </PanelBody>
  64. </Panel>
  65. </Form>
  66. );
  67. }
  68. const StyledNumberField = styled(NumberField)`
  69. ${p => p.disabled && `cursor: not-allowed`}
  70. `;