folk_plugin_process/
metrics.rs1use std::sync::Arc;
2
3use folk_api::metrics::{CounterVec, GaugeVec, MetricsRegistry};
4
5pub struct ProcessMetrics {
6 up: Arc<dyn GaugeVec>,
7 restarts_total: Arc<dyn CounterVec>,
8 uptime_seconds: Arc<dyn GaugeVec>,
9 exit_code: Arc<dyn GaugeVec>,
10 status: Arc<dyn GaugeVec>,
11}
12
13impl ProcessMetrics {
14 pub fn new(registry: &dyn MetricsRegistry) -> Self {
15 Self {
16 up: registry.gauge_vec(
17 "folk_process_up",
18 "Whether the process is running (1) or not (0)",
19 &["name"],
20 ),
21 restarts_total: registry.counter_vec(
22 "folk_process_restarts_total",
23 "Total number of process restarts",
24 &["name"],
25 ),
26 uptime_seconds: registry.gauge_vec(
27 "folk_process_uptime_seconds",
28 "Current instance uptime in seconds",
29 &["name"],
30 ),
31 exit_code: registry.gauge_vec(
32 "folk_process_exit_code",
33 "Last exit code of the process",
34 &["name"],
35 ),
36 status: registry.gauge_vec(
37 "folk_process_status",
38 "Process status (1 = active for that status label)",
39 &["name", "status"],
40 ),
41 }
42 }
43
44 pub fn set_up(&self, name: &str, running: bool) {
45 self.up
46 .with_labels(&[name])
47 .set(if running { 1 } else { 0 });
48 }
49
50 pub fn inc_restarts(&self, name: &str) {
51 self.restarts_total.with_labels(&[name]).inc();
52 }
53
54 pub fn set_uptime(&self, name: &str, secs: f64) {
55 self.uptime_seconds.with_labels(&[name]).set(secs as i64);
56 }
57
58 pub fn set_exit_code(&self, name: &str, code: i32) {
59 self.exit_code.with_labels(&[name]).set(code as i64);
60 }
61
62 pub fn set_status(&self, name: &str, active_status: &str) {
63 for s in &["running", "stopped", "failed"] {
64 self.status
65 .with_labels(&[name, s])
66 .set(if *s == active_status { 1 } else { 0 });
67 }
68 }
69}