mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-05 06:41:46 +00:00
feat(metrics): Record metrics with OpenTelemetry and export over OTLP
This commit is contained in:
1 parent
e96b6e6665
commit
5185045cb6
33 files changed
+1118
-455
No files matched your search
@@ -0,0 +1,43 @@
|
||||
use std::time::Duration;
|
||||
|
||||
/// User and system CPU time.
|
||||
#[cfg(unix)]
|
||||
pub(super) fn cpu_time() -> Option<(Duration, Duration)> {
|
||||
use nix::sys::resource::{UsageWho, getrusage};
|
||||
|
||||
let usage = getrusage(UsageWho::RUSAGE_SELF).ok()?;
|
||||
let to_duration = |tv: nix::sys::time::TimeVal| {
|
||||
Duration::from_secs(tv.tv_sec().try_into().ok()?)
|
||||
.checked_add(Duration::from_micros(tv.tv_usec().try_into().ok()?))
|
||||
};
|
||||
|
||||
Some((to_duration(usage.user_time())?, to_duration(usage.system_time())?))
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
pub(super) fn cpu_time() -> Option<(Duration, Duration)> { None }
|
||||
|
||||
/// Resident and virtual memory in bytes, read from `/proc/self/status`.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(super) fn memory() -> Option<(u64, u64)> {
|
||||
let status = std::fs::read_to_string("/proc/self/status").ok()?;
|
||||
let field = |name: &str| -> Option<u64> {
|
||||
let value = status.lines().find_map(|line| line.strip_prefix(name))?;
|
||||
let kib: u64 = value.trim().trim_end_matches(" kB").parse().ok()?;
|
||||
kib.checked_mul(1024)
|
||||
};
|
||||
|
||||
Some((field("VmRSS:")?, field("VmSize:")?))
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub(super) fn memory() -> Option<(u64, u64)> { None }
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(super) fn open_fds() -> Option<u64> {
|
||||
let count = std::fs::read_dir("/proc/self/fd").ok()?.count();
|
||||
count.try_into().ok()
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub(super) fn open_fds() -> Option<u64> { None }
|
||||
Reference in new issue
Block a user