Skip to content

Commit

Permalink
Export thread count from process_collector
Browse files Browse the repository at this point in the history
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
  • Loading branch information
free committed May 17, 2021
1 parent 1e7736f commit b2d6349
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/process_collector.rs
Expand Up @@ -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,
Expand All @@ -33,6 +33,7 @@ pub struct ProcessCollector {
vsize: Gauge,
rss: Gauge,
start_time: Gauge,
threads: Gauge,
}

impl ProcessCollector {
Expand Down Expand Up @@ -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,
Expand All @@ -109,6 +116,7 @@ impl ProcessCollector {
vsize,
rss,
start_time,
threads,
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b2d6349

Please sign in to comment.