Skip to content

Commit

Permalink
Export thread count from process_collector (tikv#401)
Browse files Browse the repository at this point in the history
This simply retrieves and exports the value of the  field from the
already populated  that CPU, memory and start time are
sourced.

Signed-off-by: Alin Sinpalean <alin.sinpalean@gmail.com>
Signed-off-by: Jan Berktold <jberktold@roblox.com>
  • Loading branch information
free authored and JanBerktold committed Nov 12, 2022
1 parent d2b0fb3 commit 1730559
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/process_collector.rs
Expand Up @@ -17,12 +17,12 @@ use crate::proto;
/// The `pid_t` data type represents process IDs.
pub use libc::pid_t;

/// Six metrics per ProcessCollector.
const METRICS_NUMBER: usize = 6;
/// Seven metrics per ProcessCollector.
const METRICS_NUMBER: usize = 7;

/// 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,13 @@ impl ProcessCollector {
.unwrap();
descs.extend(start_time.desc().into_iter().cloned());

let threads = Gauge::with_opts(
Opts::new("process_threads", "Number of OS threads in the process.")
.namespace(namespace.clone()),
)
.unwrap();
descs.extend(threads.desc().into_iter().cloned());

ProcessCollector {
pid,
descs,
Expand All @@ -109,6 +117,7 @@ impl ProcessCollector {
vsize,
rss,
start_time,
threads,
}
}

Expand Down Expand Up @@ -166,6 +175,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 All @@ -174,6 +186,7 @@ impl Collector for ProcessCollector {
mfs.extend(self.vsize.collect());
mfs.extend(self.rss.collect());
mfs.extend(self.start_time.collect());
mfs.extend(self.threads.collect());
mfs
}
}
Expand Down Expand Up @@ -208,7 +221,7 @@ mod tests {
fn test_process_collector() {
let pc = ProcessCollector::for_self();
{
// Six metrics per process collector.
// Seven metrics per process collector.
let descs = pc.desc();
assert_eq!(descs.len(), super::METRICS_NUMBER);
let mfs = pc.collect();
Expand Down

0 comments on commit 1730559

Please sign in to comment.