Skip to content

Commit 0023ac2

Browse files
committed
wip: process collector
1 parent ad05f0f commit 0023ac2

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

Cargo.toml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ authors = ["Max Inden <[email protected]>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."
77
license = "Apache-2.0 OR MIT"
8-
keywords = ["openmetrics", "prometheus", "metrics", "instrumentation", "monitoring"]
8+
keywords = [
9+
"openmetrics",
10+
"prometheus",
11+
"metrics",
12+
"instrumentation",
13+
"monitoring",
14+
]
915
repository = "https://github.com/prometheus/client_rust"
1016
homepage = "https://github.com/prometheus/client_rust"
1117
documentation = "https://docs.rs/prometheus-client"
@@ -15,7 +21,7 @@ default = []
1521
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
1622

1723
[workspace]
18-
members = ["derive-encode"]
24+
members = ["derive-encode", "process-collector"]
1925

2026
[dependencies]
2127
dtoa = "1.0"
@@ -35,7 +41,12 @@ quickcheck = "1"
3541
rand = "0.8.4"
3642
tide = "0.16"
3743
actix-web = "4"
38-
tokio = { version = "1", features = ["rt-multi-thread", "net", "macros", "signal"] }
44+
tokio = { version = "1", features = [
45+
"rt-multi-thread",
46+
"net",
47+
"macros",
48+
"signal",
49+
] }
3950
hyper = { version = "1.3.1", features = ["server", "http1"] }
4051
hyper-util = { version = "0.1.3", features = ["tokio"] }
4152
http-body-util = "0.1.1"

process-collector/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "process-collector"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
procfs = "0.17.0"
10+
prometheus-client = { path = "../" }

process-collector/src/lib.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use procfs::process::Process;
2+
use prometheus_client::{
3+
collector::Collector,
4+
encoding::{DescriptorEncoder, EncodeMetric},
5+
metrics::counter::ConstCounter,
6+
registry::Unit,
7+
};
8+
9+
#[derive(Debug)]
10+
pub struct ProcessCollector {
11+
namespace: String,
12+
}
13+
14+
impl Collector for ProcessCollector {
15+
fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
16+
let tps = procfs::ticks_per_second();
17+
// process_cpu_seconds_total Total user and system CPU time spent in seconds.
18+
// process_max_fds Maximum number of open file descriptors.
19+
// process_open_fds Number of open file descriptors.
20+
// process_virtual_memory_bytes Virtual memory size in bytes.
21+
// process_resident_memory_bytes Resident memory size in bytes.
22+
// process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.
23+
// process_start_time_seconds Start time of the process since unix epoch in seconds.
24+
// process_network_receive_bytes_total Number of bytes received by the process over the network.
25+
// process_network_transmit_bytes_total Number of bytes sent by the process over the network.
26+
27+
if let Ok(proc) = Process::myself() {
28+
if let Ok(stat) = proc.stat() {
29+
let cpu_time = (stat.stime + stat.utime) / tps as u64;
30+
let counter = ConstCounter::new(cpu_time);
31+
let metric_encoder = encoder.encode_descriptor(
32+
"process_cpu_seconds_total",
33+
"Total user and system CPU time spent in seconds.",
34+
Some(&Unit::Seconds),
35+
counter.metric_type(),
36+
)?;
37+
counter.encode(metric_encoder)?;
38+
}
39+
40+
if let Ok(limits) = proc.limits() {
41+
let max_fds = match limits.max_open_files.soft_limit {
42+
procfs::process::LimitValue::Value(v) => v,
43+
procfs::process::LimitValue::Unlimited => 0,
44+
};
45+
let counter = ConstCounter::new(max_fds);
46+
let metric_encoder = encoder.encode_descriptor(
47+
"process_max_fds",
48+
"Maximum number of open file descriptors.",
49+
None,
50+
counter.metric_type(),
51+
)?;
52+
counter.encode(metric_encoder)?;
53+
}
54+
}
55+
56+
Ok(())
57+
}
58+
}
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use super::*;
63+
use prometheus_client::registry::Registry;
64+
65+
#[test]
66+
fn register_process_collector() {
67+
let mut registry = Registry::default();
68+
registry.register_collector(Box::new(ProcessCollector {
69+
namespace: String::new(),
70+
}))
71+
}
72+
}

0 commit comments

Comments
 (0)