#[cfg_attr(target_os = "macos", path = "implementation/macos.rs")]
#[cfg_attr(target_os = "linux", path = "implementation/linux.rs")]
#[cfg_attr(target_os = "windows", path = "implementation/windows.rs")]
#[cfg_attr(target_os = "freebsd", path = "implementation/freebsd.rs")]
#[cfg_attr(target_os = "openbsd", path = "implementation/openbsd.rs")]
#[allow(unused_attributes)]
#[cfg_attr(feature = "dummy", path = "implementation/dummy.rs")]
mod implementation;
#[cfg(all(
not(feature = "dummy"),
not(any(
target_os = "macos",
target_os = "linux",
target_os = "windows",
target_os = "freebsd",
target_os = "openbsd"
))
))]
compile_error!(
"A feature \"dummy\" must be enabled to compile this crate on non supported platforms."
);
pub use implementation::collect;
#[derive(Debug, Default, PartialEq)]
pub struct Metrics {
pub cpu_seconds_total: Option<f64>,
pub open_fds: Option<u64>,
pub max_fds: Option<u64>,
pub virtual_memory_bytes: Option<u64>,
pub virtual_memory_max_bytes: Option<u64>,
pub resident_memory_bytes: Option<u64>,
pub start_time_seconds: Option<u64>,
pub threads: Option<u64>,
}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
fn fibonacci(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 2) + fibonacci(n - 1),
}
}
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "windows",
target_os = "freebsd"
))]
#[test]
fn test_collect_internal_ok() {
fibonacci(40);
let m = collect();
dbg!(&m);
assert_matches!(m.cpu_seconds_total, Some(_));
assert_matches!(m.open_fds, Some(_));
assert_matches!(m.max_fds, Some(_));
assert_matches!(m.virtual_memory_bytes, Some(_));
#[cfg(not(target_os = "windows"))]
assert_matches!(m.virtual_memory_max_bytes, Some(_)); assert_matches!(m.resident_memory_bytes, Some(_));
assert_matches!(m.start_time_seconds, Some(_));
#[cfg(not(target_os = "windows"))]
assert_matches!(m.threads, Some(_));
}
#[cfg(target_os = "openbsd")]
#[test]
fn test_collect_internal_ok_openbsd() {
fibonacci(40);
let m = collect();
dbg!(&m);
assert_matches!(m.cpu_seconds_total, Some(_));
assert_matches!(m.open_fds, None);
assert_matches!(m.max_fds, Some(_));
assert_matches!(m.virtual_memory_bytes, None);
assert_matches!(m.virtual_memory_max_bytes, None);
assert_matches!(m.resident_memory_bytes, Some(_));
assert_matches!(m.start_time_seconds, Some(_));
assert_matches!(m.threads, None);
}
#[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "linux"))]
#[cfg(not(target_os = "windows"))]
#[cfg(not(target_os = "freebsd"))]
#[cfg(not(target_os = "openbsd"))]
#[cfg(feature = "dummy")]
#[test]
fn test_collect_internal_ok_dummy() {
fibonacci(40);
let m = collect();
dbg!(&m);
assert_matches!(m.cpu_seconds_total, None);
assert_matches!(m.open_fds, None);
assert_matches!(m.max_fds, None);
assert_matches!(m.virtual_memory_bytes, None);
assert_matches!(m.virtual_memory_max_bytes, None);
assert_matches!(m.resident_memory_bytes, None);
assert_matches!(m.start_time_seconds, None);
assert_matches!(m.threads, None);
}
}