Просмотр исходного кода

ref(cardinality-limit): Dynamically build option names (#69567)

Dynamically build cardinality limit related option names in preparation
for additional project scoped option.
ArthurKnaus 10 месяцев назад
Родитель
Сommit
a1048dfbb5

+ 3 - 3
src/sentry/quotas/base.py

@@ -9,7 +9,7 @@ from django.core.cache import cache
 
 from sentry import features, options
 from sentry.constants import DataCategory
-from sentry.sentry_metrics.use_case_id_registry import USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS
+from sentry.sentry_metrics.use_case_id_registry import CARDINALITY_LIMIT_USE_CASES
 from sentry.utils.json import prune_empty_keys
 from sentry.utils.services import Service
 
@@ -61,7 +61,7 @@ def build_metric_abuse_quotas() -> list[AbuseQuota]:
         (QuotaScope.GLOBAL, "g"),
     ]
 
-    for (scope, prefix) in scopes:
+    for scope, prefix in scopes:
         quotas.append(
             AbuseQuota(
                 id=f"{prefix}amb",
@@ -71,7 +71,7 @@ def build_metric_abuse_quotas() -> list[AbuseQuota]:
             )
         )
 
-        for use_case in USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS:
+        for use_case in CARDINALITY_LIMIT_USE_CASES:
             quotas.append(
                 AbuseQuota(
                     id=f"{prefix}amb_{use_case.value}",

+ 5 - 3
src/sentry/relay/config/__init__.py

@@ -44,7 +44,7 @@ from sentry.relay.config.metric_extraction import (
     get_metric_extraction_config,
 )
 from sentry.relay.utils import to_camel_case_name
-from sentry.sentry_metrics.use_case_id_registry import USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS
+from sentry.sentry_metrics.use_case_id_registry import CARDINALITY_LIMIT_USE_CASES
 from sentry.sentry_metrics.visibility import get_metrics_blocking_state_for_relay_config
 from sentry.utils import metrics
 from sentry.utils.http import get_origins
@@ -268,9 +268,11 @@ def get_metrics_config(timeout: TimeChecker, project: Project) -> Mapping[str, A
         )
 
         cardinality_limits: list[CardinalityLimit] = []
-        for namespace, option_name in USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS.items():
+        for namespace in CARDINALITY_LIMIT_USE_CASES:
             timeout.check()
-            option = options.get(option_name)
+            option = options.get(
+                f"sentry-metrics.cardinality-limiter.limits.{namespace.value}.per-org"
+            )
             if not option or not len(option) == 1:
                 # Multiple quotas are not supported
                 continue

+ 8 - 7
src/sentry/sentry_metrics/use_case_id_registry.py

@@ -54,13 +54,14 @@ REVERSE_METRIC_PATH_MAPPING: Mapping[UseCaseKey, UseCaseID] = {
     UseCaseKey.PERFORMANCE: UseCaseID.TRANSACTIONS,
 }
 
-USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS = {
-    UseCaseID.TRANSACTIONS: "sentry-metrics.cardinality-limiter.limits.transactions.per-org",
-    UseCaseID.SESSIONS: "sentry-metrics.cardinality-limiter.limits.sessions.per-org",
-    UseCaseID.SPANS: "sentry-metrics.cardinality-limiter.limits.spans.per-org",
-    UseCaseID.CUSTOM: "sentry-metrics.cardinality-limiter.limits.custom.per-org",
-    UseCaseID.PROFILES: "sentry-metrics.cardinality-limiter.limits.profiles.per-org",
-}
+# Temporary allowlist until all use cases have cardinality limit options
+CARDINALITY_LIMIT_USE_CASES = (
+    UseCaseID.TRANSACTIONS,
+    UseCaseID.SESSIONS,
+    UseCaseID.SPANS,
+    UseCaseID.CUSTOM,
+    UseCaseID.PROFILES,
+)
 
 USE_CASE_ID_WRITES_LIMIT_QUOTA_OPTIONS = {
     UseCaseID.SPANS: "sentry-metrics.writes-limiter.limits.spans",

+ 3 - 6
tests/sentry/quotas/test_redis.py

@@ -7,10 +7,7 @@ import pytest
 from sentry.constants import DataCategory
 from sentry.quotas.base import QuotaConfig, QuotaScope, build_metric_abuse_quotas
 from sentry.quotas.redis import RedisQuota, is_rate_limited
-from sentry.sentry_metrics.use_case_id_registry import (
-    USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS,
-    UseCaseID,
-)
+from sentry.sentry_metrics.use_case_id_registry import CARDINALITY_LIMIT_USE_CASES, UseCaseID
 from sentry.testutils.cases import TestCase
 from sentry.utils.redis import clusters
 
@@ -162,10 +159,10 @@ class RedisQuotaTest(TestCase):
             (QuotaScope.GLOBAL, "g"),
         ]:
             expected_quotas[(scope, None)] = f"{prefix}amb"
-            for use_case in USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS:
+            for use_case in CARDINALITY_LIMIT_USE_CASES:
                 expected_quotas[(scope, use_case)] = f"{prefix}amb_{use_case.value}"
 
-        for ((expected_scope, expected_use_case), id) in expected_quotas.items():
+        for (expected_scope, expected_use_case), id in expected_quotas.items():
             quota = next(x for x in quotas if x.id == id)
             assert quota is not None