autopush_common/db/
reporter.rs1use std::{sync::Arc, time::Duration};
2
3use actix_web::rt;
4use cadence::{Gauged, StatsdClient};
5use gethostname::gethostname;
6
7use super::client::DbClient;
8
9pub 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 if let Some(status) = db.pool_status() {
29 metrics
30 .gauge_with_tags(
31 "database.ops.inflight",
32 (status.size - status.available) as u64,
33 )
34 .send();
36 metrics
37 .gauge_with_tags("database.ops.available", status.available as u64)
38 .send();
39 metrics
40 .gauge_with_tags("database.ops.queued", status.waiting as u64)
41 .send();
42 }
43
44 if let Some(count) = db.configured_channel_count() {
48 metrics
49 .gauge_with_tags("database.channels", count as u64)
50 .send();
51 }
52}