|
5 | 5 | import pytest
|
6 | 6 |
|
7 | 7 | from sentry.constants import DataCategory
|
8 |
| -from sentry.quotas.base import QuotaConfig, QuotaScope |
| 8 | +from sentry.quotas.base import QuotaConfig, QuotaScope, build_metric_abuse_quotas |
9 | 9 | from sentry.quotas.redis import RedisQuota, is_rate_limited
|
| 10 | +from sentry.sentry_metrics.use_case_id_registry import ( |
| 11 | + USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS, |
| 12 | + UseCaseID, |
| 13 | +) |
10 | 14 | from sentry.testutils.cases import TestCase
|
11 | 15 | from sentry.utils.redis import clusters
|
12 | 16 |
|
@@ -118,11 +122,12 @@ def test_abuse_quotas(self):
|
118 | 122 | self.organization.update_option("project-abuse-quota.session-limit", 602)
|
119 | 123 | self.organization.update_option("organization-abuse-quota.metric-bucket-limit", 603)
|
120 | 124 | self.organization.update_option("organization-abuse-quota.custom-metric-bucket-limit", 604)
|
121 |
| - self.organization.update_option("global-abuse-quota.metric-bucket-limit", 605) |
122 |
| - self.organization.update_option("global-abuse-quota.sessions-metric-bucket-limit", 606) |
123 |
| - self.organization.update_option("global-abuse-quota.transactions-metric-bucket-limit", 607) |
124 |
| - self.organization.update_option("global-abuse-quota.spans-metric-bucket-limit", 608) |
125 |
| - self.organization.update_option("global-abuse-quota.custom-metric-bucket-limit", 609) |
| 125 | + |
| 126 | + metric_abuse_limit_by_id = dict() |
| 127 | + for i, mabq in enumerate(build_metric_abuse_quotas()): |
| 128 | + self.organization.update_option(mabq.option, 700 + i) |
| 129 | + metric_abuse_limit_by_id[mabq.id] = 700 + i |
| 130 | + |
126 | 131 | with self.feature("organizations:transaction-metrics-extraction"):
|
127 | 132 | quotas = self.quota.get_quotas(self.project)
|
128 | 133 |
|
@@ -150,66 +155,38 @@ def test_abuse_quotas(self):
|
150 | 155 | assert quotas[3].window == 10
|
151 | 156 | assert quotas[3].reason_code == "project_abuse_limit"
|
152 | 157 |
|
153 |
| - assert quotas[4].id == "oam" |
154 |
| - assert quotas[4].scope == QuotaScope.ORGANIZATION |
155 |
| - assert quotas[4].scope_id is None |
156 |
| - assert quotas[4].categories == {DataCategory.METRIC_BUCKET} |
157 |
| - assert quotas[4].limit == 6030 |
158 |
| - assert quotas[4].window == 10 |
159 |
| - assert quotas[4].reason_code == "org_abuse_limit" |
160 |
| - |
161 |
| - assert quotas[5].id == "oacm" |
162 |
| - assert quotas[5].scope == QuotaScope.ORGANIZATION |
163 |
| - assert quotas[5].scope_id is None |
164 |
| - assert quotas[5].categories == {DataCategory.METRIC_BUCKET} |
165 |
| - assert quotas[5].limit == 6040 |
166 |
| - assert quotas[5].window == 10 |
167 |
| - assert quotas[5].reason_code == "org_abuse_limit" |
168 |
| - |
169 |
| - assert quotas[6].id == "gam" |
170 |
| - assert quotas[6].scope == QuotaScope.GLOBAL |
171 |
| - assert quotas[6].scope_id is None |
172 |
| - assert quotas[6].categories == {DataCategory.METRIC_BUCKET} |
173 |
| - assert quotas[6].limit == 6050 |
174 |
| - assert quotas[6].window == 10 |
175 |
| - assert quotas[6].reason_code == "global_abuse_limit" |
176 |
| - assert quotas[6].namespace is None |
177 |
| - |
178 |
| - assert quotas[7].id == "gams" |
179 |
| - assert quotas[7].scope == QuotaScope.GLOBAL |
180 |
| - assert quotas[7].scope_id is None |
181 |
| - assert quotas[7].categories == {DataCategory.METRIC_BUCKET} |
182 |
| - assert quotas[7].limit == 6060 |
183 |
| - assert quotas[7].window == 10 |
184 |
| - assert quotas[7].reason_code == "global_abuse_limit" |
185 |
| - assert quotas[7].namespace == "sessions" |
186 |
| - |
187 |
| - assert quotas[8].id == "gamt" |
188 |
| - assert quotas[8].scope == QuotaScope.GLOBAL |
189 |
| - assert quotas[8].scope_id is None |
190 |
| - assert quotas[8].categories == {DataCategory.METRIC_BUCKET} |
191 |
| - assert quotas[8].limit == 6070 |
192 |
| - assert quotas[8].window == 10 |
193 |
| - assert quotas[8].reason_code == "global_abuse_limit" |
194 |
| - assert quotas[8].namespace == "transactions" |
195 |
| - |
196 |
| - assert quotas[9].id == "gamp" |
197 |
| - assert quotas[9].scope == QuotaScope.GLOBAL |
198 |
| - assert quotas[9].scope_id is None |
199 |
| - assert quotas[9].categories == {DataCategory.METRIC_BUCKET} |
200 |
| - assert quotas[9].limit == 6080 |
201 |
| - assert quotas[9].window == 10 |
202 |
| - assert quotas[9].reason_code == "global_abuse_limit" |
203 |
| - assert quotas[9].namespace == "spans" |
204 |
| - |
205 |
| - assert quotas[10].id == "gamc" |
206 |
| - assert quotas[10].scope == QuotaScope.GLOBAL |
207 |
| - assert quotas[10].scope_id is None |
208 |
| - assert quotas[10].categories == {DataCategory.METRIC_BUCKET} |
209 |
| - assert quotas[10].limit == 6090 |
210 |
| - assert quotas[10].window == 10 |
211 |
| - assert quotas[10].reason_code == "global_abuse_limit" |
212 |
| - assert quotas[10].namespace == "custom" |
| 158 | + expected_quotas: dict[tuple[QuotaScope, UseCaseID | None], str] = dict() |
| 159 | + for scope, prefix in [ |
| 160 | + (QuotaScope.PROJECT, "p"), |
| 161 | + (QuotaScope.ORGANIZATION, "o"), |
| 162 | + (QuotaScope.GLOBAL, "g"), |
| 163 | + ]: |
| 164 | + expected_quotas[(scope, None)] = f"{prefix}amb" |
| 165 | + for use_case in USE_CASE_ID_CARDINALITY_LIMIT_QUOTA_OPTIONS: |
| 166 | + expected_quotas[(scope, use_case)] = f"{prefix}amb_{use_case.value}" |
| 167 | + |
| 168 | + for ((expected_scope, expected_use_case), id) in expected_quotas.items(): |
| 169 | + quota = next(x for x in quotas if x.id == id) |
| 170 | + assert quota is not None |
| 171 | + |
| 172 | + assert quota.id == id |
| 173 | + assert quota.scope == expected_scope |
| 174 | + assert quota.scope_id is None |
| 175 | + assert quota.categories == {DataCategory.METRIC_BUCKET} |
| 176 | + assert quota.limit == metric_abuse_limit_by_id[id] * 10 |
| 177 | + if expected_use_case is None: |
| 178 | + assert quota.namespace is None |
| 179 | + else: |
| 180 | + assert quota.namespace == expected_use_case.value |
| 181 | + assert quota.window == 10 |
| 182 | + if expected_scope == QuotaScope.GLOBAL: |
| 183 | + assert quota.reason_code == "global_abuse_limit" |
| 184 | + elif expected_scope == QuotaScope.ORGANIZATION: |
| 185 | + assert quota.reason_code == "org_abuse_limit" |
| 186 | + elif expected_scope == QuotaScope.PROJECT: |
| 187 | + assert quota.reason_code == "project_abuse_limit" |
| 188 | + else: |
| 189 | + assert False, "invalid quota scope" |
213 | 190 |
|
214 | 191 | # Let's set the global option for error limits.
|
215 | 192 | # Since we already have an org override for it, it shouldn't change anything.
|
|
0 commit comments