Skip to content

Commit

Permalink
metrics: export ratelimit as a gauge (iopsystems#164)
Browse files Browse the repository at this point in the history
The ratelimit is currently extracted from the ratelimiter every time
during output. Export it as a metrics gauge instead.
  • Loading branch information
mihirn authored Feb 21, 2024
1 parent d915e7d commit d531e51
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub mod handlers {
) -> Result<impl warp::Reply, Infallible> {
if let Some(r) = ratelimit {
let amount = (rate as f64 / 1_000_000.0).ceil() as u64;
RATELIMIT_CURR.set(rate as i64);

// even though we might not have nanosecond level clock resolution,
// by using a nanosecond level duration, we achieve more accurate
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ fn main() {
// launch json log output
{
let config = config.clone();
let ratelimiter = workload_ratelimit.clone();
control_runtime.spawn_blocking(move || output::json(config, ratelimiter.as_deref()));
control_runtime.spawn_blocking(move || output::json(config));
}

// begin cli output
Expand Down
2 changes: 2 additions & 0 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ histogram!(PUBSUB_LATENCY, "pubsub_latency");

histogram!(PUBSUB_PUBLISH_LATENCY, "pubsub_publish_latency");

gauge!(RATELIMIT_CURR, "ratelimit/current");

gauge!(CONNECT_CURR, "client/connections/current");
counter!(CONNECT_OK, "client/connect/ok");
counter!(CONNECT_TIMEOUT, "client/connect/timeout");
Expand Down
5 changes: 2 additions & 3 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::*;
use ratelimit::Ratelimiter;
use rpcperf_dataspec::*;
use std::io::{BufWriter, Write};

Expand Down Expand Up @@ -208,7 +207,7 @@ fn pubsub_stats(snapshot: &mut MetricsSnapshot) {
output!("{latencies}");
}

pub fn json(config: Config, ratelimit: Option<&Ratelimiter>) {
pub fn json(config: Config) {
if config.general().json_output().is_none() {
return;
}
Expand Down Expand Up @@ -277,7 +276,7 @@ pub fn json(config: Config, ratelimit: Option<&Ratelimiter>) {
let json = JsonSnapshot {
window: window_id,
elapsed,
target_qps: ratelimit.as_ref().map(|ratelimit| ratelimit.rate()),
target_qps: Some(RATELIMIT_CURR.value() as f64),
client: ClientStats {
connections,
requests,
Expand Down
12 changes: 8 additions & 4 deletions src/workload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ pub struct Generator {
impl Generator {
pub fn new(config: &Config) -> Self {
let ratelimiter = config.workload().ratelimit().start().map(|rate| {
let amount = (rate.get() as f64 / 1_000_000.0).ceil() as u64;
let rate = rate.get();
let amount = (rate as f64 / 1_000_000.0).ceil() as u64;
RATELIMIT_CURR.set(rate as i64);

// even though we might not have nanosecond level clock resolution,
// by using a nanosecond level duration, we achieve more accurate
// ratelimits.
let interval = Duration::from_nanos(1_000_000_000 / (rate.get() / amount));
let interval = Duration::from_nanos(1_000_000_000 / (rate / amount));

let capacity = std::cmp::max(100, amount);

Expand Down Expand Up @@ -689,12 +691,14 @@ pub async fn reconnect(work_sender: Sender<ClientWorkItem>, config: Config) -> R
}

let ratelimiter = config.client().unwrap().reconnect_rate().map(|rate| {
let amount = (rate.get() as f64 / 1_000_000.0).ceil() as u64;
let rate = rate.get();
let amount = (rate as f64 / 1_000_000.0).ceil() as u64;
RATELIMIT_CURR.set(rate as i64);

// even though we might not have nanosecond level clock resolution,
// by using a nanosecond level duration, we achieve more accurate
// ratelimits.
let interval = Duration::from_nanos(1_000_000_000 / (rate.get() / amount));
let interval = Duration::from_nanos(1_000_000_000 / (rate / amount));

let capacity = std::cmp::max(100, amount);

Expand Down

0 comments on commit d531e51

Please sign in to comment.