16
16
from sentry .silo .base import SiloMode
17
17
from sentry .testutils .cases import APITestCase
18
18
from sentry .testutils .silo import all_silo_test , assume_test_silo_mode , control_silo_test
19
- from sentry .types .ratelimit import RateLimit , RateLimitCategory
19
+ from sentry .types .ratelimit import RateLimit , RateLimitCategory , RateLimitMeta , RateLimitType
20
20
from sentry .utils .snuba import RateLimitExceeded
21
21
22
22
@@ -38,6 +38,22 @@ class SnubaRateLimitedEndpoint(Endpoint):
38
38
permission_classes = (AllowAny ,)
39
39
40
40
def get (self , request ):
41
+
42
+ # Rate limit middleware will set metadata to indicate the request is not limited by the endpoint itself
43
+ request ._request .rate_limit_metadata = RateLimitMeta (
44
+ rate_limit_type = RateLimitType .NOT_LIMITED ,
45
+ concurrent_limit = 123 ,
46
+ concurrent_requests = 1 ,
47
+ reset_time = 123 ,
48
+ group = "test_group" ,
49
+ limit = 123 ,
50
+ window = 123 ,
51
+ current = 1 ,
52
+ remaining = 122 ,
53
+ )
54
+
55
+ # However, snuba's 429 will be caught by the custom handler and raise an exception
56
+ # with the snuba metadata
41
57
raise RateLimitExceeded (
42
58
"Query on could not be run due to allocation policies, ... 'rejection_threshold': 40, 'quota_used': 41, ..." ,
43
59
policy = "ConcurrentRateLimitAllocationPolicy" ,
@@ -190,12 +206,12 @@ def test_access_log_snuba_rate_limited(self) -> None:
190
206
assert self .captured_logs [0 ].rate_limit_type == "RateLimitType.SNUBA"
191
207
assert self .captured_logs [0 ].rate_limited == "True"
192
208
193
- # All the types from the standard rate limit metadata should not be set
194
- assert self .captured_logs [0 ].remaining == "None "
195
- assert self .captured_logs [0 ].concurrent_limit == "None "
196
- assert self .captured_logs [0 ].concurrent_requests == "None "
197
- assert self .captured_logs [0 ].limit == "None "
198
- assert self .captured_logs [0 ].reset_time == "None "
209
+ # All the types from the standard rate limit metadata should be set
210
+ assert self .captured_logs [0 ].remaining == "122 "
211
+ assert self .captured_logs [0 ].concurrent_limit == "123 "
212
+ assert self .captured_logs [0 ].concurrent_requests == "1 "
213
+ assert self .captured_logs [0 ].limit == "123 "
214
+ assert self .captured_logs [0 ].reset_time == "123 "
199
215
200
216
# Snuba rate limit specific fields should be set
201
217
assert self .captured_logs [0 ].snuba_policy == "ConcurrentRateLimitAllocationPolicy"
0 commit comments