From f82fa41b9a721bd3d02335a22e07136c1ea1ebe8 Mon Sep 17 00:00:00 2001 From: Noncrab Date: Sat, 28 Mar 2026 13:00:41 +0000 Subject: [PATCH 1/2] Use a float gauge for tracking cpu time. --- src/process_collector.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/process_collector.rs b/src/process_collector.rs index 136efc9a..fc9ee016 100644 --- a/src/process_collector.rs +++ b/src/process_collector.rs @@ -6,9 +6,9 @@ use lazy_static::lazy_static; -use crate::counter::IntCounter; +use crate::counter::{Counter, IntCounter}; use crate::desc::Desc; -use crate::gauge::IntGauge; +use crate::gauge::{Gauge, IntGauge}; use crate::metrics::{Collector, Opts}; use crate::proto; @@ -25,7 +25,7 @@ const METRICS_NUMBER: usize = 7; pub struct ProcessCollector { pid: pid_t, descs: Vec, - cpu_total: IntCounter, + cpu_total: Gauge, open_fds: IntGauge, max_fds: IntGauge, vsize: IntGauge, @@ -40,7 +40,7 @@ impl ProcessCollector { let namespace = namespace.into(); let mut descs = Vec::new(); - let cpu_total = IntCounter::with_opts( + let cpu_total = Gauge::with_opts( Opts::new( "process_cpu_seconds_total", "Total user and system CPU time spent in \ @@ -163,12 +163,8 @@ impl Collector for ProcessCollector { self.rss.set((stat.rss as i64) * *PAGESIZE); // cpu - let total = (stat.utime + stat.stime) / *CLK_TCK as u64; - let past = self.cpu_total.get(); - // If two threads are collecting metrics at the same time, - // the cpu_total counter may have already been updated, - // and the subtraction may underflow. - self.cpu_total.inc_by(total.saturating_sub(past)); + let total = (stat.utime + stat.stime) as f64 / *CLK_TCK as f64; + self.cpu_total.set(total); cpu_total_mfs = Some(self.cpu_total.collect()); // threads From 8bdd80be206925f84539169bec86e713d644ee25 Mon Sep 17 00:00:00 2001 From: Noncrab Date: Sat, 28 Mar 2026 13:42:39 +0000 Subject: [PATCH 2/2] Canary. --- src/process_collector.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/process_collector.rs b/src/process_collector.rs index fc9ee016..4a83e6c7 100644 --- a/src/process_collector.rs +++ b/src/process_collector.rs @@ -41,12 +41,7 @@ impl ProcessCollector { let mut descs = Vec::new(); let cpu_total = Gauge::with_opts( - Opts::new( - "process_cpu_seconds_total", - "Total user and system CPU time spent in \ - seconds.", - ) - .namespace(namespace.clone()), + Opts::new("process_cpu_seconds_total", "TEST.").namespace(namespace.clone()), ) .unwrap(); descs.extend(cpu_total.desc().into_iter().cloned());