Line data Source code
1 : //! process metrics that the [`::prometheus`] crate doesn't provide.
2 :
3 : // This module has heavy inspiration from the prometheus crate's `process_collector.rs`.
4 :
5 : use crate::UIntGauge;
6 :
7 : pub struct Collector {
8 : descs: Vec<prometheus::core::Desc>,
9 : vmlck: crate::UIntGauge,
10 : }
11 :
12 : const NMETRICS: usize = 1;
13 :
14 : impl prometheus::core::Collector for Collector {
15 0 : fn desc(&self) -> Vec<&prometheus::core::Desc> {
16 0 : self.descs.iter().collect()
17 0 : }
18 :
19 0 : fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
20 0 : let Ok(myself) = procfs::process::Process::myself() else {
21 0 : return vec![];
22 : };
23 0 : let mut mfs = Vec::with_capacity(NMETRICS);
24 0 : if let Ok(status) = myself.status() {
25 0 : if let Some(vmlck) = status.vmlck {
26 0 : self.vmlck.set(vmlck);
27 0 : mfs.extend(self.vmlck.collect())
28 0 : }
29 0 : }
30 0 : mfs
31 0 : }
32 : }
33 :
34 : impl Collector {
35 0 : pub fn new() -> Self {
36 0 : let mut descs = Vec::new();
37 0 :
38 0 : let vmlck =
39 0 : UIntGauge::new("libmetrics_process_status_vmlck", "/proc/self/status vmlck").unwrap();
40 0 : descs.extend(
41 0 : prometheus::core::Collector::desc(&vmlck)
42 0 : .into_iter()
43 0 : .cloned(),
44 0 : );
45 0 :
46 0 : Self { descs, vmlck }
47 0 : }
48 : }
49 :
50 : impl Default for Collector {
51 0 : fn default() -> Self {
52 0 : Self::new()
53 0 : }
54 : }
|