metrics_process_promstyle/
lib.rs

1use 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    // TODO(feature): Getting the number of open file descriptors involves extra work
12    //     and I don't care *that* much about the metric for now anyway.
13    // describe_gauge!("process_open_fds", Unit::Count, "Number of open file descriptors.");
14    // TODO(feature): Getting the max number of file descriptors involves reading the limits file,
15    //     which is easy enough to implement but I don't care about the limit if there's no count of
16    //     open FDs (and may as well reduce the amount of work the collector needs to do).
17    // describe_gauge!("process_max_fds", Unit::Count, "Maximum number of open file descriptors.");
18    describe_gauge!(
19        "process_virtual_memory_bytes",
20        Unit::Bytes,
21        "Virtual memory size in bytes."
22    );
23    // TODO(feature): Couldn't immediately see where to get this one, and I don't usually rely on it.
24    // describe_gauge!("process_virtual_memory_max_bytes", Unit::Bytes, "Maximum amount of virtual memory available in bytes.");
25    describe_gauge!(
26        "process_resident_memory_bytes",
27        Unit::Bytes,
28        "Resident memory size in bytes."
29    );
30    // TODO(feature): counting heap bytes presumably needs access to the allocator.
31    //     I can see that breaking depending on which allocator is in use, so won't bother for now.
32    // describe_gauge!("process_heap_bytes", Unit::Bytes, "Process heap size in bytes.");
33    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!("process_open_fds", );
63    // gauge!("process_max_fds", );
64    gauge!(
65        "process_virtual_memory_bytes",
66        this_process.stat.vsize as f64
67    );
68    // gauge!("process_virtual_memory_max_bytes", );
69    gauge!("process_resident_memory_bytes", rss_bytes as f64);
70    // gauge!("process_heap_bytes", );
71    gauge!("process_start_time_seconds", start_time_secs);
72    gauge!("process_threads", num_threads as f64);
73
74    Ok(())
75}