Skip to main content

hypercall/observability/metrics_collector/
directive_outbox.rs

1use super::MetricsCollector;
2use hypercall_types::directives::ActionKey;
3use metrics::gauge;
4use tracing::{debug, error};
5
6const DELIVERY_STATUSES: [&str; 2] = ["pending", "broadcasted"];
7const WITHDRAWAL_ACTIONS: [ActionKey; 2] = [
8    ActionKey::SystemWithdrawToken,
9    ActionKey::SystemCreditOption,
10];
11
12impl MetricsCollector {
13    pub(super) async fn collect_directive_outbox_metrics(&self) {
14        let Some(db) = &self.directive_outbox_db else {
15            return;
16        };
17
18        debug!("Collecting directive outbox metrics...");
19
20        for action_key in WITHDRAWAL_ACTIONS {
21            for delivery_status in DELIVERY_STATUSES {
22                set_directive_outbox_gauges(action_key.as_str(), delivery_status, 0, 0, 0, 0);
23            }
24        }
25
26        let rows = match db.list_directive_outbox_delivery_metrics().await {
27            Ok(rows) => rows,
28            Err(err) => {
29                error!(error = %err, "Failed to collect directive outbox metrics");
30                return;
31            }
32        };
33
34        for row in rows {
35            set_directive_outbox_gauges(
36                &row.action_key,
37                &row.delivery_status,
38                row.pending_count,
39                row.retrying_count,
40                row.oldest_created_age_seconds,
41                row.oldest_attempt_age_seconds,
42            );
43        }
44    }
45}
46
47fn set_directive_outbox_gauges(
48    action_key: &str,
49    delivery_status: &str,
50    pending_count: i64,
51    retrying_count: i64,
52    oldest_created_age_seconds: i64,
53    oldest_attempt_age_seconds: i64,
54) {
55    gauge!(
56        "ht_directive_outbox_pending",
57        "action_key" => action_key.to_string(),
58        "delivery_status" => delivery_status.to_string(),
59    )
60    .set(pending_count as f64);
61    gauge!(
62        "ht_directive_outbox_retrying",
63        "action_key" => action_key.to_string(),
64        "delivery_status" => delivery_status.to_string(),
65    )
66    .set(retrying_count as f64);
67    gauge!(
68        "ht_directive_outbox_oldest_created_age_seconds",
69        "action_key" => action_key.to_string(),
70        "delivery_status" => delivery_status.to_string(),
71    )
72    .set(oldest_created_age_seconds as f64);
73    gauge!(
74        "ht_directive_outbox_oldest_attempt_age_seconds",
75        "action_key" => action_key.to_string(),
76        "delivery_status" => delivery_status.to_string(),
77    )
78    .set(oldest_attempt_age_seconds as f64);
79}