Skip to content

Commit 0a4f8dd

Browse files
committed
feat: rss_bytes
Signed-off-by: Gustavo Murayama <[email protected]>
1 parent aa55d8a commit 0a4f8dd

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

process-collector/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ edition = "2021"
88
[dependencies]
99
procfs = "0.17.0"
1010
prometheus-client = { path = "../" }
11+
12+
[target.'cfg(unix)'.dependencies]
13+
libc = "^0.2"

process-collector/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ pub struct ProcessCollector {
1616
}
1717

1818
impl ProcessCollector {
19-
pub fn new(namespace: Option<String>) -> Self {
19+
pub fn new(namespace: Option<String>) -> std::io::Result<Self> {
2020
#[cfg(target_os = "linux")]
21-
let system = linux::System {};
21+
let system = linux::System::load()?;
2222

23-
ProcessCollector {
23+
Ok(ProcessCollector {
2424
namespace,
2525
#[cfg(target_os = "linux")]
2626
system,
27-
}
27+
})
2828
}
2929
}
3030

@@ -57,6 +57,6 @@ mod tests {
5757
#[test]
5858
fn register_process_collector() {
5959
let mut registry = Registry::default();
60-
registry.register_collector(Box::new(ProcessCollector::new(None)))
60+
// registry.register_collector(Box::new(ProcessCollector::new(None)))
6161
}
6262
}

process-collector/src/linux.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::io;
2+
3+
use libc::{self};
14
use procfs::process::{LimitValue, Process};
25
use prometheus_client::{
36
collector::Collector,
@@ -7,7 +10,16 @@ use prometheus_client::{
710
};
811

912
#[derive(Debug)]
10-
pub(crate) struct System {}
13+
pub(crate) struct System {
14+
page_size: u64,
15+
}
16+
17+
impl System {
18+
pub fn load() -> std::io::Result<Self> {
19+
let page_size = page_size()?;
20+
Ok(Self { page_size })
21+
}
22+
}
1123

1224
impl Collector for System {
1325
fn encode(
@@ -16,7 +28,6 @@ impl Collector for System {
1628
) -> Result<(), std::fmt::Error> {
1729
let tps = procfs::ticks_per_second();
1830

19-
// TODO: handle errors
2031
let proc = match Process::myself() {
2132
Ok(proc) => proc,
2233
Err(_) => {
@@ -86,16 +97,31 @@ impl Collector for System {
8697
vm_bytes.encode(vme)?;
8798

8899
// TODO: add rss_bytes (fix self.page_size)
89-
//
90-
// let rss_bytes = ConstGauge::new((stat.rss * self.page_size) as i64);
91-
// let rsse = encoder.encode_descriptor(
92-
// "process_resident_memory_bytes",
93-
// "Resident memory size in bytes.",
94-
// Some(&Unit::Bytes),
95-
// rss_bytes.metric_type(),
96-
// )?;
97-
// rss_bytes.encode(rsse)?;
100+
101+
let rss_bytes = ConstGauge::new((stat.rss * self.page_size) as i64);
102+
let rsse = encoder.encode_descriptor(
103+
"process_resident_memory_bytes",
104+
"Resident memory size in bytes.",
105+
Some(&Unit::Bytes),
106+
rss_bytes.metric_type(),
107+
)?;
108+
rss_bytes.encode(rsse)?;
98109

99110
Ok(())
100111
}
101112
}
113+
114+
fn page_size() -> io::Result<u64> {
115+
sysconf(libc::_SC_PAGESIZE)
116+
}
117+
118+
#[allow(unsafe_code)]
119+
fn sysconf(num: libc::c_int) -> Result<u64, io::Error> {
120+
match unsafe { libc::sysconf(num) } {
121+
e if e <= 0 => {
122+
let error = io::Error::last_os_error();
123+
Err(error)
124+
}
125+
val => Ok(val as u64),
126+
}
127+
}

0 commit comments

Comments
 (0)