Skip to content

Commit 206fdc9

Browse files
authored
Fix some stats (#47)
* fix some stats * use constant * lint
1 parent f74101c commit 206fdc9

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

src/stats.rs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use log::{error, info};
1+
use log::{debug, error, info};
22
use once_cell::sync::OnceCell;
33
use statsd::Client;
44
/// Events collector and publisher.
@@ -10,6 +10,7 @@ use std::sync::{Arc, Mutex};
1010
use crate::config::get_config;
1111

1212
static LATEST_STATS: OnceCell<Arc<Mutex<HashMap<String, i64>>>> = OnceCell::new();
13+
static STAT_PERIOD: u64 = 15000; //15 seconds
1314

1415
#[derive(Debug, Clone, Copy)]
1516
enum EventName {
@@ -228,7 +229,15 @@ impl Collector {
228229
("total_xact_count", 0),
229230
("total_sent", 0),
230231
("total_received", 0),
232+
("total_xact_time", 0),
233+
("total_query_time", 0),
231234
("total_wait_time", 0),
235+
("avg_xact_time", 0),
236+
("avg_query_time", 0),
237+
("avg_xact_count", 0),
238+
("avg_sent", 0),
239+
("avg_received", 0),
240+
("avg_wait_time", 0),
232241
("maxwait_us", 0),
233242
("maxwait", 0),
234243
("cl_waiting", 0),
@@ -240,11 +249,18 @@ impl Collector {
240249
("sv_tested", 0),
241250
]);
242251

252+
// Stats saved after each iteration of the flush event. Used in calculation
253+
// of averages in the last flush period.
254+
let mut old_stats: HashMap<String, i64> = HashMap::new();
255+
256+
// Track which state the client and server are at any given time.
243257
let mut client_server_states: HashMap<i32, EventName> = HashMap::new();
244-
let tx = self.tx.clone();
245258

259+
// Flush stats to StatsD and calculate averages every 15 seconds.
260+
let tx = self.tx.clone();
246261
tokio::task::spawn(async move {
247-
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(15000));
262+
let mut interval =
263+
tokio::time::interval(tokio::time::Duration::from_millis(STAT_PERIOD));
248264
loop {
249265
interval.tick().await;
250266
let _ = tx.try_send(Event {
@@ -255,6 +271,7 @@ impl Collector {
255271
}
256272
});
257273

274+
// The collector loop
258275
loop {
259276
let stat = match self.rx.recv().await {
260277
Some(stat) => stat,
@@ -291,10 +308,11 @@ impl Collector {
291308
*counter += stat.value;
292309

293310
let counter = stats.entry("maxwait_us").or_insert(0);
311+
let mic_part = stat.value % 1_000_000;
294312

295313
// Report max time here
296-
if stat.value > *counter {
297-
*counter = stat.value;
314+
if mic_part > *counter {
315+
*counter = mic_part;
298316
}
299317

300318
let counter = stats.entry("maxwait").or_insert(0);
@@ -320,6 +338,7 @@ impl Collector {
320338
}
321339

322340
EventName::FlushStatsToStatsD => {
341+
// Calculate connection states
323342
for (_, state) in &client_server_states {
324343
match state {
325344
EventName::ClientActive => {
@@ -361,8 +380,26 @@ impl Collector {
361380
};
362381
}
363382

364-
info!("{:?}", stats);
383+
// Calculate averages
384+
for stat in &[
385+
"avg_query_count",
386+
"avgxact_count",
387+
"avg_sent",
388+
"avg_received",
389+
"avg_wait_time",
390+
] {
391+
let total_name = stat.replace("avg_", "total_");
392+
let old_value = old_stats.entry(total_name.clone()).or_insert(0);
393+
let new_value = stats.get(total_name.as_str()).unwrap_or(&0).to_owned();
394+
let avg = (new_value - *old_value) / (STAT_PERIOD as i64 / 1_000); // Avg / second
395+
396+
stats.insert(stat, avg);
397+
*old_value = new_value;
398+
}
365399

400+
debug!("{:?}", stats);
401+
402+
// Update latest stats used in SHOW STATS
366403
match LATEST_STATS.get() {
367404
Some(arc) => {
368405
let mut guard = arc.lock().unwrap();
@@ -376,9 +413,24 @@ impl Collector {
376413

377414
let mut pipeline = self.client.pipeline();
378415

379-
for (key, value) in stats.iter_mut() {
416+
for (key, value) in stats.iter() {
380417
pipeline.gauge(key, *value as f64);
381-
*value = 0;
418+
}
419+
420+
// These are re-calculated every iteration of the loop, so we don't want to add values
421+
// from the last iteration.
422+
for stat in &[
423+
"cl_active",
424+
"cl_waiting",
425+
"cl_idle",
426+
"sv_idle",
427+
"sv_active",
428+
"sv_tested",
429+
"sv_login",
430+
"maxwait",
431+
"maxwait_us",
432+
] {
433+
stats.insert(stat, 0);
382434
}
383435

384436
pipeline.send(&self.client);

0 commit comments

Comments
 (0)