Skip to content

Commit 798fe47

Browse files
committed
tests: register process collector
Signed-off-by: Gustavo Murayama <[email protected]>
1 parent fcbb6a7 commit 798fe47

File tree

3 files changed

+190
-17
lines changed

3 files changed

+190
-17
lines changed

process-collector/src/lib.rs

Lines changed: 181 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ mod linux;
1010

1111
#[derive(Debug)]
1212
pub struct ProcessCollector {
13-
namespace: Option<String>,
13+
namespace: String,
1414
#[cfg(target_os = "linux")]
1515
system: linux::System,
1616
}
1717

1818
impl ProcessCollector {
1919
pub fn new(namespace: Option<String>) -> std::io::Result<Self> {
2020
#[cfg(target_os = "linux")]
21-
let system = linux::System::load()?;
21+
let system = linux::System::load(namespace.clone())?;
22+
let namespace = match namespace {
23+
Some(mut n) => {
24+
n.push('_');
25+
n
26+
}
27+
None => "".to_string(),
28+
};
2229

2330
Ok(ProcessCollector {
2431
namespace,
@@ -34,8 +41,9 @@ impl Collector for ProcessCollector {
3441
.duration_since(UNIX_EPOCH)
3542
.map_err(|_| std::fmt::Error)?;
3643
let start_time = ConstGauge::new(start_time_from_epoch.as_secs_f64());
44+
let metric_name = format!("{}process_start_time", self.namespace);
3745
let start_time_metric = encoder.encode_descriptor(
38-
"process_start_time_seconds",
46+
&metric_name,
3947
"Start time of the process since unix epoch in seconds.",
4048
Some(&Unit::Seconds),
4149
start_time.metric_type(),
@@ -52,11 +60,178 @@ impl Collector for ProcessCollector {
5260
#[cfg(test)]
5361
mod tests {
5462
use super::*;
55-
use prometheus_client::registry::Registry;
63+
use prometheus_client::{encoding::text::encode, registry::Registry};
5664

5765
#[test]
58-
fn register_process_collector() {
66+
fn register_start_time() {
5967
let mut registry = Registry::default();
60-
// registry.register_collector(Box::new(ProcessCollector::new(None)))
68+
let processor_collector = ProcessCollector::new(None).unwrap();
69+
registry.register_collector(Box::new(processor_collector));
70+
let mut encoded = String::new();
71+
encode(&mut encoded, &registry).unwrap();
72+
73+
let start_time = "# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.\n".to_owned() +
74+
"# TYPE process_start_time_seconds gauge\n" +
75+
"# UNIT process_start_time_seconds seconds\n" +
76+
"process_start_time_seconds ";
77+
78+
assert!(
79+
encoded.contains(&start_time),
80+
"encoded does not contain expected start_time"
81+
);
82+
}
83+
84+
#[test]
85+
fn register_resident_memory() {
86+
let mut registry = Registry::default();
87+
let processor_collector = ProcessCollector::new(None).unwrap();
88+
registry.register_collector(Box::new(processor_collector));
89+
let mut encoded = String::new();
90+
encode(&mut encoded, &registry).unwrap();
91+
92+
let resident_memory =
93+
"# HELP process_resident_memory_bytes Resident memory size in bytes.\n".to_owned()
94+
+ "# TYPE process_resident_memory_bytes gauge\n"
95+
+ "# UNIT process_resident_memory_bytes bytes\n"
96+
+ "process_resident_memory_bytes ";
97+
98+
assert!(
99+
encoded.contains(&resident_memory),
100+
"encoded does not contain expected resident_memory"
101+
);
102+
}
103+
104+
#[test]
105+
fn register_virtual_memory() {
106+
let mut registry = Registry::default();
107+
let processor_collector = ProcessCollector::new(None).unwrap();
108+
registry.register_collector(Box::new(processor_collector));
109+
let mut encoded = String::new();
110+
encode(&mut encoded, &registry).unwrap();
111+
112+
let virtual_memory = "# HELP process_virtual_memory_bytes Virtual memory size in bytes\n"
113+
.to_owned()
114+
+ "# TYPE process_virtual_memory_bytes gauge\n"
115+
+ "# UNIT process_virtual_memory_bytes bytes\n"
116+
+ "process_virtual_memory_bytes ";
117+
118+
assert!(
119+
encoded.contains(&virtual_memory),
120+
"encoded does not contain expected virtual_memory"
121+
);
122+
}
123+
124+
#[test]
125+
fn register_virtual_memory_max() {
126+
let mut registry = Registry::default();
127+
let processor_collector = ProcessCollector::new(None).unwrap();
128+
registry.register_collector(Box::new(processor_collector));
129+
let mut encoded = String::new();
130+
encode(&mut encoded, &registry).unwrap();
131+
132+
let virtual_memory_max = "# HELP process_virtual_memory_max Maximum amount of virtual memory available in bytes.\n".to_owned() +
133+
"# TYPE process_virtual_memory_max gauge\n" +
134+
"process_virtual_memory_max ";
135+
136+
assert!(
137+
encoded.contains(&virtual_memory_max),
138+
"encoded does not contain expected virtual_memory_max"
139+
);
140+
}
141+
142+
#[test]
143+
fn register_open_fds() {
144+
let mut registry = Registry::default();
145+
let processor_collector = ProcessCollector::new(None).unwrap();
146+
registry.register_collector(Box::new(processor_collector));
147+
let mut encoded = String::new();
148+
encode(&mut encoded, &registry).unwrap();
149+
150+
let open_fds = "# HELP process_open_fds Number of open file descriptors.\n".to_owned()
151+
+ "# TYPE process_open_fds counter\n"
152+
+ "process_open_fds_total ";
153+
154+
assert!(
155+
encoded.contains(&open_fds),
156+
"encoded does not contain expected open_fds"
157+
);
158+
}
159+
160+
#[test]
161+
fn register_max_fds() {
162+
let mut registry = Registry::default();
163+
let processor_collector = ProcessCollector::new(None).unwrap();
164+
registry.register_collector(Box::new(processor_collector));
165+
let mut encoded = String::new();
166+
encode(&mut encoded, &registry).unwrap();
167+
168+
let max_fds = "# HELP process_max_fds Maximum number of open file descriptors.\n"
169+
.to_owned()
170+
+ "# TYPE process_max_fds gauge\n"
171+
+ "process_max_fds ";
172+
173+
assert!(
174+
encoded.contains(&max_fds),
175+
"encoded does not contain expected max_fds"
176+
);
177+
}
178+
179+
#[test]
180+
fn register_cpu_seconds() {
181+
let mut registry = Registry::default();
182+
let processor_collector = ProcessCollector::new(None).unwrap();
183+
registry.register_collector(Box::new(processor_collector));
184+
let mut encoded = String::new();
185+
encode(&mut encoded, &registry).unwrap();
186+
187+
let cpu_seconds =
188+
"# HELP process_cpu_seconds Total user and system CPU time spent in seconds.\n"
189+
.to_owned()
190+
+ "# TYPE process_cpu_seconds counter\n"
191+
+ "# UNIT process_cpu_seconds seconds\n"
192+
+ "process_cpu_seconds_total ";
193+
194+
assert!(
195+
encoded.contains(&cpu_seconds),
196+
"encoded does not contain expected cpu_seconds"
197+
);
198+
}
199+
200+
#[test]
201+
fn register_network_receive() {
202+
let mut registry = Registry::default();
203+
let processor_collector = ProcessCollector::new(None).unwrap();
204+
registry.register_collector(Box::new(processor_collector));
205+
let mut encoded = String::new();
206+
encode(&mut encoded, &registry).unwrap();
207+
208+
let network_receive = "# HELP process_network_receive_bytes Number of bytes received by the process over the network.\n".to_owned() +
209+
"# TYPE process_network_receive_bytes counter\n" +
210+
"# UNIT process_network_receive_bytes bytes\n" +
211+
"process_network_receive_bytes_total ";
212+
213+
assert!(
214+
encoded.contains(&network_receive),
215+
"encoded does not contain expected network_receive"
216+
);
217+
}
218+
219+
#[test]
220+
fn register_network_transmit() {
221+
let mut registry = Registry::default();
222+
let processor_collector = ProcessCollector::new(None).unwrap();
223+
registry.register_collector(Box::new(processor_collector));
224+
let mut encoded = String::new();
225+
encode(&mut encoded, &registry).unwrap();
226+
227+
let network_transmit = "# HELP process_network_transmit_bytes Number of bytes sent by the process over the network.\n".to_owned() +
228+
"# TYPE process_network_transmit_bytes counter\n" +
229+
"# UNIT process_network_transmit_bytes bytes\n" +
230+
"process_network_transmit_bytes_total ";
231+
232+
assert!(
233+
encoded.contains(&network_transmit),
234+
"encoded does not contain expected network_transmit"
235+
);
61236
}
62237
}

process-collector/src/linux/netstat.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ fn parse_proc_netstat<R: io::Read>(reader: R, file_name: &str) -> io::Result<Net
5555
)
5656
})?;
5757
let key = name_parts[i];
58-
match protocol {
59-
"IpExt" => match key {
58+
if protocol == "IpExt" {
59+
match key {
6060
"InOctets" => proc_netstat.ip_ext.in_octets = Some(value),
6161
"OutOctets" => proc_netstat.ip_ext.out_octets = Some(value),
6262
_ => {}
63-
},
64-
_ => {}
63+
}
6564
}
6665
}
6766
}

process-collector/src/linux/system.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl System {
5959
let tps = procfs::ticks_per_second();
6060
let cpu_time = (stat.stime + stat.utime) / tps;
6161
let counter = ConstCounter::new(cpu_time);
62-
let metric_name = format!("{}process_cpu_seconds_total", &self.namespace);
62+
let metric_name = format!("{}process_cpu", &self.namespace);
6363
let metric_encoder = encoder.encode_descriptor(
6464
&metric_name,
6565
"Total user and system CPU time spent in seconds.",
@@ -116,7 +116,7 @@ impl System {
116116
LimitValue::Value(soft) => soft,
117117
};
118118
let gauge = ConstGauge::new(max_virtual_memory as i64);
119-
let metric_name = format!("{}process_virtual_memory_max_bytes", &self.namespace);
119+
let metric_name = format!("{}process_virtual_memory_max", &self.namespace);
120120
let metric_encoder = encoder.encode_descriptor(
121121
&metric_name,
122122
"Maximum amount of virtual memory available in bytes.",
@@ -135,7 +135,7 @@ impl System {
135135
encoder: &mut prometheus_client::encoding::DescriptorEncoder,
136136
) -> SystemResult {
137137
let vm_bytes = ConstGauge::new(stat.vsize as i64);
138-
let metric_name = format!("{}process_virtual_memory_bytes", &self.namespace);
138+
let metric_name = format!("{}process_virtual_memory", &self.namespace);
139139
let vme = encoder.encode_descriptor(
140140
&metric_name,
141141
"Virtual memory size in bytes",
@@ -153,7 +153,7 @@ impl System {
153153
encoder: &mut prometheus_client::encoding::DescriptorEncoder,
154154
) -> SystemResult {
155155
let rss_bytes = ConstGauge::new((stat.rss * self.page_size) as i64);
156-
let metric_name = format!("{}process_resident_memory_bytes", &self.namespace);
156+
let metric_name = format!("{}process_resident_memory", &self.namespace);
157157
let rsse = encoder.encode_descriptor(
158158
&metric_name,
159159
"Resident memory size in bytes.",
@@ -173,7 +173,7 @@ impl System {
173173
match Netstat::read(stat.pid) {
174174
Ok(Netstat { ip_ext, .. }) => {
175175
let recv_bytes = ConstCounter::new(ip_ext.in_octets.unwrap_or_default());
176-
let metric_name = format!("{}process_network_receive_bytes_total", &self.namespace);
176+
let metric_name = format!("{}process_network_receive", &self.namespace);
177177
let rbe = encoder.encode_descriptor(
178178
&metric_name,
179179
"Number of bytes received by the process over the network.",
@@ -183,8 +183,7 @@ impl System {
183183
recv_bytes.encode(rbe)?;
184184

185185
let transmit_bytes = ConstCounter::new(ip_ext.out_octets.unwrap_or_default());
186-
let metric_name =
187-
format!("{}process_network_transmit_bytes_total", &self.namespace);
186+
let metric_name = format!("{}process_network_transmit", &self.namespace);
188187
let tbe = encoder.encode_descriptor(
189188
&metric_name,
190189
"Number of bytes sent by the process over the network.",

0 commit comments

Comments
 (0)