diff --git a/src/process_collector.rs b/src/process_collector.rs index 136efc9a..4a83e6c7 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,13 +40,8 @@ impl ProcessCollector { let namespace = namespace.into(); let mut descs = Vec::new(); - let cpu_total = IntCounter::with_opts( - Opts::new( - "process_cpu_seconds_total", - "Total user and system CPU time spent in \ - seconds.", - ) - .namespace(namespace.clone()), + let cpu_total = Gauge::with_opts( + Opts::new("process_cpu_seconds_total", "TEST.").namespace(namespace.clone()), ) .unwrap(); descs.extend(cpu_total.desc().into_iter().cloned()); @@ -163,12 +158,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