|
| 1 | +//! Process metrics collection. |
| 2 | +
|
| 3 | +use prometheus_client::{metrics::gauge::Gauge, registry::Registry}; |
| 4 | +use std::{future::Future, time::Duration}; |
| 5 | +use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System}; |
| 6 | + |
| 7 | +/// The interval at which to update process metrics. |
| 8 | +const TICK_INTERVAL: Duration = Duration::from_secs(10); |
| 9 | + |
| 10 | +/// Process metrics collector. |
| 11 | +pub struct Metrics { |
| 12 | + /// Resident set size in bytes. |
| 13 | + pub rss: Gauge, |
| 14 | + /// Virtual memory size in bytes. |
| 15 | + pub virtual_memory: Gauge, |
| 16 | + |
| 17 | + /// Process ID. |
| 18 | + pid: sysinfo::Pid, |
| 19 | + /// System information handle. |
| 20 | + system: System, |
| 21 | +} |
| 22 | + |
| 23 | +impl Metrics { |
| 24 | + /// Initialize process metrics and register them with the given registry. |
| 25 | + pub fn init(registry: &mut Registry) -> Self { |
| 26 | + let metrics = Self { |
| 27 | + pid: sysinfo::Pid::from_u32(std::process::id()), |
| 28 | + rss: Gauge::default(), |
| 29 | + virtual_memory: Gauge::default(), |
| 30 | + |
| 31 | + system: System::new(), |
| 32 | + }; |
| 33 | + |
| 34 | + // Register all metrics |
| 35 | + registry.register( |
| 36 | + "process_rss", |
| 37 | + "Resident set size of the current process", |
| 38 | + metrics.rss.clone(), |
| 39 | + ); |
| 40 | + registry.register( |
| 41 | + "process_virtual_memory", |
| 42 | + "Virtual memory size of the current process", |
| 43 | + metrics.virtual_memory.clone(), |
| 44 | + ); |
| 45 | + |
| 46 | + metrics |
| 47 | + } |
| 48 | + |
| 49 | + /// Update all process metrics. |
| 50 | + fn update(&mut self) { |
| 51 | + // Refresh process information |
| 52 | + self.system.refresh_processes_specifics( |
| 53 | + ProcessesToUpdate::Some(&[self.pid]), |
| 54 | + false, |
| 55 | + ProcessRefreshKind::nothing().with_memory(), |
| 56 | + ); |
| 57 | + |
| 58 | + // If the process exists, update the metrics |
| 59 | + if let Some(process) = self.system.process(self.pid) { |
| 60 | + self.rss.set(process.memory() as i64); |
| 61 | + self.virtual_memory.set(process.virtual_memory() as i64); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// Update process metrics periodically. |
| 66 | + /// |
| 67 | + /// This function takes a sleep function as a parameter to allow different runtimes |
| 68 | + /// to provide their own implementation. |
| 69 | + pub async fn collect<F, Fut>(mut self, sleep_fn: F) |
| 70 | + where |
| 71 | + F: Fn(Duration) -> Fut, |
| 72 | + Fut: Future<Output = ()>, |
| 73 | + { |
| 74 | + loop { |
| 75 | + self.update(); |
| 76 | + sleep_fn(TICK_INTERVAL).await; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_process_metrics_init() { |
| 87 | + let mut registry = Registry::default(); |
| 88 | + let mut metrics = Metrics::init(&mut registry); |
| 89 | + |
| 90 | + // Update metrics |
| 91 | + metrics.update(); |
| 92 | + |
| 93 | + // Check that RSS is reasonable (> 1MB for a running process) |
| 94 | + let rss = metrics.rss.get(); |
| 95 | + assert!(rss > 0); |
| 96 | + |
| 97 | + // Check that virtual memory is >= RSS |
| 98 | + let virt = metrics.virtual_memory.get(); |
| 99 | + assert!(virt >= rss); |
| 100 | + |
| 101 | + // Allocate some memory |
| 102 | + let mut vec = vec![0; 10 * 1024 * 1024]; // 10MB |
| 103 | + vec.push(1); |
| 104 | + |
| 105 | + // Update metrics |
| 106 | + metrics.update(); |
| 107 | + |
| 108 | + // Check that the metrics are updated |
| 109 | + let new_rss = metrics.rss.get(); |
| 110 | + assert!(new_rss > rss, "RSS should be > {rss}"); |
| 111 | + |
| 112 | + // Check that virtual memory is updated |
| 113 | + let new_virt = metrics.virtual_memory.get(); |
| 114 | + assert!(new_virt > virt, "Virtual memory should be > {virt}"); |
| 115 | + } |
| 116 | +} |
0 commit comments