Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export thread count from process_collector #401

Merged
merged 1 commit into from Aug 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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