metrics_process_promstyle/
lib.rs1use metrics::{describe_gauge, gauge, Unit};
2use procfs::process::Process;
3use procfs::{page_size, ticks_per_second, ProcResult};
4
5pub fn describe() {
6 describe_gauge!(
7 "process_cpu_seconds_total",
8 Unit::Seconds,
9 "Total user and system CPU time spent in seconds."
10 );
11 describe_gauge!(
19 "process_virtual_memory_bytes",
20 Unit::Bytes,
21 "Virtual memory size in bytes."
22 );
23 describe_gauge!(
26 "process_resident_memory_bytes",
27 Unit::Bytes,
28 "Resident memory size in bytes."
29 );
30 describe_gauge!(
34 "process_start_time_seconds",
35 Unit::Seconds,
36 "Start time of the process since unix epoch in seconds."
37 );
38 describe_gauge!(
39 "process_threads",
40 Unit::Count,
41 "Number of OS threads in the process."
42 );
43}
44
45pub fn emit_now() -> ProcResult<()> {
46 let boot_time_secs = procfs::boot_time_secs()?;
47
48 let this_process = Process::myself()?;
49
50 let total_time_ticks = this_process.stat.utime + this_process.stat.stime;
51 let ticks_per_second = ticks_per_second()?;
52
53 let rss_bytes = this_process.stat.rss * page_size()?;
54
55 let start_time_secs =
56 (this_process.stat.starttime as f64) / (ticks_per_second as f64) + boot_time_secs as f64;
57 let total_time_secs = (total_time_ticks as f64) / (ticks_per_second as f64);
58
59 let num_threads = this_process.stat.num_threads;
60
61 gauge!("process_cpu_seconds_total", total_time_secs);
62 gauge!(
65 "process_virtual_memory_bytes",
66 this_process.stat.vsize as f64
67 );
68 gauge!("process_resident_memory_bytes", rss_bytes as f64);
70 gauge!("process_start_time_seconds", start_time_secs);
72 gauge!("process_threads", num_threads as f64);
73
74 Ok(())
75}