autopush_common/db/
reporter.rs

1use std::{sync::Arc, time::Duration};
2
3use actix_web::rt;
4use cadence::{Gauged, StatsdClient};
5use gethostname::gethostname;
6
7use super::client::DbClient;
8
9/// Emit db pool (deadpool) metrics periodically
10pub fn spawn_pool_periodic_reporter(
11    interval: Duration,
12    db: Box<dyn DbClient>,
13    metrics: Arc<StatsdClient>,
14) {
15    let hostname = gethostname().to_string_lossy().to_string();
16    rt::spawn(async move {
17        loop {
18            pool_periodic_reporter(&*db, &metrics, &hostname);
19            rt::time::sleep(interval).await;
20        }
21    });
22}
23
24fn pool_periodic_reporter(db: &dyn DbClient, metrics: &StatsdClient, hostname: &str) {
25    let Some(status) = db.pool_status() else {
26        return;
27    };
28    metrics
29        .gauge_with_tags(
30            "database.pool.active",
31            (status.size - status.available) as u64,
32        )
33        .with_tag("hostname", hostname)
34        .send();
35    metrics
36        .gauge_with_tags("database.pool.idle", status.available as u64)
37        .with_tag("hostname", hostname)
38        .send();
39    metrics
40        .gauge_with_tags("database.pool.waiting", status.waiting as u64)
41        .with_tag("hostname", hostname)
42        .send();
43}