From b2d63493cfed3aec4707ecc06d2a2c299f956887 Mon Sep 17 00:00:00 2001 From: Alin Sinpalean Date: Mon, 17 May 2021 11:49:30 +0200 Subject: [PATCH] Export thread count from `process_collector` This simply retrieves and exports the value of the `num_threads` field from the already populated `procfs::process::Stat` that CPU, memory and start time are sourced. Signed-off-by: Alin Sinpalean alin.sinpalean@gmail.com --- src/process_collector.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/process_collector.rs b/src/process_collector.rs index 51d1039f..fca590a5 100644 --- a/src/process_collector.rs +++ b/src/process_collector.rs @@ -20,9 +20,9 @@ pub use libc::pid_t; /// Six metrics per ProcessCollector. const METRICS_NUMBER: usize = 6; -/// A collector which exports the current state of -/// process metrics including cpu, memory and file descriptor usage as well as -/// the process start time for the given process id. +/// A collector which exports the current state of process metrics including +/// CPU, memory and file descriptor usage, thread count, as well as the process +/// start time for the given process id. #[derive(Debug)] pub struct ProcessCollector { pid: pid_t, @@ -33,6 +33,7 @@ pub struct ProcessCollector { vsize: Gauge, rss: Gauge, start_time: Gauge, + threads: Gauge, } impl ProcessCollector { @@ -100,6 +101,12 @@ impl ProcessCollector { .unwrap(); descs.extend(start_time.desc().into_iter().cloned()); + let threads = Gauge::with_opts( + Opts::new("process_threads", "Number of OS threads.").namespace(namespace.clone()), + ) + .unwrap(); + descs.extend(threads.desc().into_iter().cloned()); + ProcessCollector { pid, descs, @@ -109,6 +116,7 @@ impl ProcessCollector { vsize, rss, start_time, + threads, } } @@ -166,6 +174,9 @@ impl Collector for ProcessCollector { cpu_total.collect() }; + // threads + self.threads.set(p.stat.num_threads as f64); + // collect MetricFamilys. let mut mfs = Vec::with_capacity(METRICS_NUMBER); mfs.extend(cpu_total_mfs);