|
| 1 | +use metrics_process::Collector; |
| 2 | +use once_cell::sync::Lazy; |
| 3 | +use std::{fmt, sync::Arc, time::SystemTime}; |
| 4 | + |
| 5 | +pub(crate) trait Hook: Fn() + Send + Sync {} |
| 6 | +impl<T: Fn() + Send + Sync> Hook for T {} |
| 7 | + |
| 8 | +impl fmt::Debug for Hooks { |
| 9 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 10 | + let hooks_len = self.inner.len(); |
| 11 | + f.debug_struct("Hooks") |
| 12 | + .field("inner", &format!("Arc<Vec<Box<dyn Hook>>>, len: {}", hooks_len)) |
| 13 | + .finish() |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +/// Helper type for managing hooks. |
| 18 | +#[derive(Clone)] |
| 19 | +pub struct Hooks { |
| 20 | + inner: Arc<Vec<Box<dyn Hook<Output = ()>>>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl Hooks { |
| 24 | + /// Create a new set of hooks. |
| 25 | + pub fn new() -> Self { |
| 26 | + let collector = Collector::default(); |
| 27 | + let hooks: Vec<Box<dyn Hook<Output = ()>>> = vec![ |
| 28 | + Box::new(move || collector.collect()), |
| 29 | + Box::new(collect_memory_stats), |
| 30 | + Box::new(collect_io_stats), |
| 31 | + Box::new(collect_uptime_seconds), |
| 32 | + ]; |
| 33 | + Self { inner: Arc::new(hooks) } |
| 34 | + } |
| 35 | + |
| 36 | + pub(crate) fn iter(&self) -> impl Iterator<Item = &Box<dyn Hook<Output = ()>>> { |
| 37 | + self.inner.iter() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl Default for Hooks { |
| 42 | + fn default() -> Self { |
| 43 | + Self::new() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fn collect_memory_stats() {} |
| 48 | + |
| 49 | +#[cfg(target_os = "linux")] |
| 50 | +fn collect_io_stats() { |
| 51 | + use metrics::counter; |
| 52 | + use tracing::error; |
| 53 | + |
| 54 | + let Ok(process) = procfs::process::Process::myself() |
| 55 | + .map_err(|error| error!(%error, "failed to get currently running process")) |
| 56 | + else { |
| 57 | + return; |
| 58 | + }; |
| 59 | + |
| 60 | + let Ok(io) = process.io().map_err( |
| 61 | + |error| error!(%error, "failed to get io stats for the currently running process"), |
| 62 | + ) else { |
| 63 | + return; |
| 64 | + }; |
| 65 | + |
| 66 | + counter!("io.rchar").absolute(io.rchar); |
| 67 | + counter!("io.wchar").absolute(io.wchar); |
| 68 | + counter!("io.syscr").absolute(io.syscr); |
| 69 | + counter!("io.syscw").absolute(io.syscw); |
| 70 | + counter!("io.read_bytes").absolute(io.read_bytes); |
| 71 | + counter!("io.write_bytes").absolute(io.write_bytes); |
| 72 | + counter!("io.cancelled_write_bytes").absolute(io.cancelled_write_bytes); |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(not(target_os = "linux"))] |
| 76 | +const fn collect_io_stats() {} |
| 77 | + |
| 78 | +/// Global start time of the process. |
| 79 | +static START_TIME: Lazy<SystemTime> = Lazy::new(SystemTime::now); |
| 80 | + |
| 81 | +/// Collects and records the process uptime in seconds. |
| 82 | +fn collect_uptime_seconds() { |
| 83 | + use metrics::gauge; |
| 84 | + let uptime = START_TIME.elapsed().unwrap_or_default().as_secs(); |
| 85 | + let gauge = gauge!("process_uptime_seconds"); |
| 86 | + gauge.set(uptime as f64); |
| 87 | +} |
0 commit comments