Skip to content

Commit 6017dd3

Browse files
Clara Rullfacebook-github-bot
authored andcommitted
Log more details on which ratelimit was exceeded
Summary: Log more details on which ratelimit was exceeded I recently included the limit (D75079217) but I've recieved feedback that it's also difficult to trace back to the actual limit in the config. This should help. Reviewed By: lmvasquezg Differential Revision: D75143660 fbshipit-source-id: 7a601fb0a2311cd7c74bf334525cfc552463b266
1 parent 0033dfd commit 6017dd3

File tree

6 files changed

+45
-57
lines changed

6 files changed

+45
-57
lines changed

eden/mononoke/edenapi_service/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rust_library(
1313
}),
1414
test_deps = [
1515
"//common/rust/shed/fbinit:fbinit-tokio",
16+
"//configerator/structs/scm/mononoke/ratelimiting:rate_limiting_config-rust",
1617
"//eden/mononoke/mononoke_macros:mononoke_macros",
1718
],
1819
deps = ([

eden/mononoke/edenapi_service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ vec1 = { version = "1", features = ["serde"] }
8080

8181
[dev-dependencies]
8282
fbinit-tokio = { version = "0.1.2", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
83+
rate_limiting_config = { version = "0.1.0", path = "../../../configerator/structs/scm/mononoke/ratelimiting" }

eden/mononoke/edenapi_service/src/handlers/commit.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ async fn bump_counter_check_ratelimit(
236236
RateLimitStatus::Enforced => true,
237237
_ => panic!("Invalid limit status: {:?}", limit.body.raw_config.status),
238238
};
239-
let max_value = limit.body.raw_config.limit;
240-
let time_window = limit.fci_metric.window.as_secs() as u32;
241239

242240
let client_main_id = match &client_request_info.main_id {
243241
Some(client_main_id) => client_main_id,
@@ -252,9 +250,7 @@ async fn bump_counter_check_ratelimit(
252250
&ctx,
253251
counter,
254252
bump_value,
255-
rate_limit_name,
256-
max_value,
257-
time_window,
253+
limit,
258254
enforced,
259255
hashmap! {"client_main_id" => client_main_id.as_str() },
260256
)

eden/mononoke/edenapi_service/src/middleware/rate_limiter.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,12 @@ impl Middleware for ThrottleMiddleware {
8686

8787
let category = rate_limiter.category();
8888
let counter = build_counter(&ctx, category, EDENAPI_QPS_LIMIT, &client_main_id);
89-
let max_value = limit.body.raw_config.limit;
90-
let time_window = limit.fci_metric.window.as_secs() as u32;
9189

9290
match counter_check_and_bump(
9391
&ctx,
9492
counter,
9593
1.0,
96-
EDENAPI_QPS_LIMIT,
97-
max_value,
98-
time_window,
94+
limit,
9995
enforced,
10096
hashmap! {"client_main_id" => client_main_id.as_str() },
10197
)

eden/mononoke/edenapi_service/src/utils/rate_limit.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use anyhow::Error;
1212
use anyhow::Result;
1313
use anyhow::anyhow;
1414
use context::CoreContext;
15+
use rate_limiting::RateLimit;
1516
use sha2::Digest;
1617
use sha2::Sha256;
1718
use slog::debug;
@@ -50,12 +51,13 @@ pub async fn counter_check_and_bump<'a>(
5051
ctx: &'a CoreContext,
5152
counter: BoxGlobalTimeWindowCounter,
5253
bump_value: f64,
53-
rate_limit_name: &'a str,
54-
max_value: f64,
55-
time_window: u32,
54+
rate_limit: RateLimit,
5655
enforced: bool,
5756
scuba_extras: HashMap<&'a str, &'a str>,
5857
) -> Result<(), Error> {
58+
let max_value = rate_limit.body.raw_config.limit;
59+
let time_window = rate_limit.fci_metric.window.as_secs() as u32;
60+
5961
let mut scuba = ctx.scuba().clone();
6062
for (key, val) in scuba_extras {
6163
scuba.add(key, val);
@@ -65,43 +67,30 @@ pub async fn counter_check_and_bump<'a>(
6567
Ok(Ok(count)) => {
6668
let new_value = count + bump_value;
6769
if new_value <= max_value {
68-
debug!(
69-
ctx.logger(),
70-
"Rate-limiting counter {} ({}) does not exceed threshold {} if bumped",
71-
rate_limit_name,
72-
count,
73-
max_value,
74-
);
7570
counter.bump(bump_value);
7671
Ok(())
7772
} else if !enforced {
7873
debug!(
7974
ctx.logger(),
80-
"Rate-limiting counter {} ({}) exceeds threshold {} if bumped, but enforcement is disabled",
81-
rate_limit_name,
75+
"Rate-limiting counter {:?} (current value: {}) exceeds threshold {} if bumped, but enforcement is disabled",
76+
rate_limit,
8277
count,
8378
max_value,
8479
);
8580
let log_tag = "Request would have been rejected due to rate limiting, but enforcement is disabled";
86-
let msg = format!(
87-
"Rate limit exceeded: {}, limit: {} (log only)",
88-
rate_limit_name, max_value
89-
);
81+
let msg = format!("Rate limit exceeded: {:?} (log only)", rate_limit);
9082
scuba.log_with_msg(log_tag, msg);
9183
Ok(())
9284
} else {
9385
debug!(
9486
ctx.logger(),
95-
"Rate-limiting counter {} ({}) exceeds threshold {} if bumped. Blocking request",
96-
rate_limit_name,
87+
"Rate-limiting counter {:?} (current_value: {}) exceeds threshold {} if bumped. Blocking request",
88+
rate_limit,
9789
count,
9890
max_value,
9991
);
10092
let log_tag = "Request rejected due to rate limiting";
101-
let msg = format!(
102-
"Rate limit exceeded: {}, limit: {} (enforced)",
103-
rate_limit_name, max_value
104-
);
93+
let msg = format!("Rate limit exceeded: {:?} (enforced)", rate_limit);
10594
scuba.log_with_msg(log_tag, msg.clone());
10695
Err(anyhow!(msg))
10796
}
@@ -111,14 +100,14 @@ pub async fn counter_check_and_bump<'a>(
111100
// or it's not been long enough. Bump and continue.
112101
debug!(
113102
ctx.logger(),
114-
"Failed getting rate limiting counter {}: {:?}", rate_limit_name, e
103+
"Failed getting rate limiting counter {:?}: {}", rate_limit, e
115104
);
116105
counter.bump(bump_value);
117106
Ok(())
118107
}
119108
Err(_) => {
120109
let log_tag = "Rate limiting counter fetch timed out";
121-
let msg = format!("Rate limit {}: Timed out", rate_limit_name);
110+
let msg = format!("Rate limit {:?}: Timed out", rate_limit);
122111
scuba.log_with_msg(log_tag, Some(msg));
123112
// Fail open to prevent DoS as we can't check the rate limit
124113
Ok(())
@@ -134,6 +123,12 @@ mod test {
134123
use async_trait::async_trait;
135124
use fbinit::FacebookInit;
136125
use mononoke_macros::mononoke;
126+
use rate_limiting::FciMetric;
127+
use rate_limiting::Metric;
128+
use rate_limiting::RateLimitBody;
129+
use rate_limiting::RateLimitStatus;
130+
use rate_limiting::Scope;
131+
use rate_limiting::Target;
137132
use time_window_counter::GlobalTimeWindowCounter;
138133

139134
use super::*;
@@ -157,10 +152,24 @@ mod test {
157152
#[mononoke::fbinit_test]
158153
async fn test_counter_check_and_bump(fb: FacebookInit) {
159154
let ctx = CoreContext::test_mock(fb);
160-
let rate_limit_name = "test";
161155
let max_value = 10.0;
162156
let scuba_extras = HashMap::new();
163157

158+
let limit = RateLimit {
159+
body: RateLimitBody {
160+
raw_config: rate_limiting_config::RateLimitBody {
161+
limit: max_value,
162+
status: RateLimitStatus::Enforced,
163+
},
164+
},
165+
fci_metric: FciMetric {
166+
metric: Metric::Commits,
167+
window: Duration::from_secs(1),
168+
scope: Scope::Global,
169+
},
170+
target: Some(Target::MainClientId("test_target".to_string())),
171+
};
172+
164173
// Test case: Counter below maximum value
165174
let counter = Box::new(MockBoxGlobalTimeWindowCounter {
166175
count: Arc::new(Mutex::new(5.0)),
@@ -169,9 +178,7 @@ mod test {
169178
&ctx,
170179
counter,
171180
1.0,
172-
rate_limit_name,
173-
max_value,
174-
1,
181+
limit.clone(),
175182
true,
176183
scuba_extras.clone(),
177184
)
@@ -185,9 +192,7 @@ mod test {
185192
&ctx,
186193
counter,
187194
1.0,
188-
rate_limit_name,
189-
max_value,
190-
1,
195+
limit.clone(),
191196
true,
192197
scuba_extras.clone(),
193198
)
@@ -201,9 +206,7 @@ mod test {
201206
&ctx,
202207
counter,
203208
1.0,
204-
rate_limit_name,
205-
max_value,
206-
1,
209+
limit.clone(),
207210
true,
208211
scuba_extras.clone(),
209212
)
@@ -214,17 +217,8 @@ mod test {
214217
let counter = Box::new(MockBoxGlobalTimeWindowCounter {
215218
count: Arc::new(Mutex::new(11.0)),
216219
});
217-
let result = counter_check_and_bump(
218-
&ctx,
219-
counter,
220-
1.0,
221-
rate_limit_name,
222-
max_value,
223-
1,
224-
false,
225-
scuba_extras.clone(),
226-
)
227-
.await;
220+
let result =
221+
counter_check_and_bump(&ctx, counter, 1.0, limit, false, scuba_extras.clone()).await;
228222
assert!(result.is_ok());
229223
}
230224
}

eden/mononoke/rate_limiting/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ pub enum Scope {
284284

285285
#[derive(Debug, Copy, Clone, PartialEq)]
286286
pub struct FciMetric {
287-
metric: Metric,
287+
pub metric: Metric,
288288
pub window: Duration,
289-
scope: Scope,
289+
pub scope: Scope,
290290
}
291291

292292
#[must_use]

0 commit comments

Comments
 (0)