mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-07 07:01:31 +00:00
feat: guest metrics support (#46)
* feat: initial support for idm send in daemon * feat: implement IdmClient backend support * feat: daemon idm now uses IdmClient * fix: implement channel destruction propagation * feat: implement request response idm system * feat: implement metrics support * proto: move metrics into GuestMetrics for reusability * fix: log level of guest agent was trace * feat: metrics tree with process information
This commit is contained in:
121
crates/guest/src/metrics.rs
Normal file
121
crates/guest/src/metrics.rs
Normal file
@ -0,0 +1,121 @@
|
||||
use std::{ops::Add, path::Path};
|
||||
|
||||
use anyhow::Result;
|
||||
use krata::idm::protocol::{IdmMetricFormat, IdmMetricNode};
|
||||
use sysinfo::Process;
|
||||
|
||||
pub struct MetricsCollector {}
|
||||
|
||||
impl MetricsCollector {
|
||||
pub fn new() -> Result<Self> {
|
||||
Ok(MetricsCollector {})
|
||||
}
|
||||
|
||||
pub fn collect(&self) -> Result<IdmMetricNode> {
|
||||
let mut sysinfo = sysinfo::System::new();
|
||||
Ok(IdmMetricNode::structural(
|
||||
"guest",
|
||||
vec![
|
||||
self.collect_system(&mut sysinfo)?,
|
||||
self.collect_processes(&mut sysinfo)?,
|
||||
],
|
||||
))
|
||||
}
|
||||
|
||||
fn collect_system(&self, sysinfo: &mut sysinfo::System) -> Result<IdmMetricNode> {
|
||||
sysinfo.refresh_memory();
|
||||
Ok(IdmMetricNode::structural(
|
||||
"system",
|
||||
vec![IdmMetricNode::structural(
|
||||
"memory",
|
||||
vec![
|
||||
IdmMetricNode::value("total", sysinfo.total_memory(), IdmMetricFormat::Bytes),
|
||||
IdmMetricNode::value("used", sysinfo.used_memory(), IdmMetricFormat::Bytes),
|
||||
IdmMetricNode::value("free", sysinfo.free_memory(), IdmMetricFormat::Bytes),
|
||||
],
|
||||
)],
|
||||
))
|
||||
}
|
||||
|
||||
fn collect_processes(&self, sysinfo: &mut sysinfo::System) -> Result<IdmMetricNode> {
|
||||
sysinfo.refresh_processes();
|
||||
let mut processes = Vec::new();
|
||||
let mut sysinfo_processes = sysinfo.processes().values().collect::<Vec<_>>();
|
||||
sysinfo_processes.sort_by_key(|x| x.pid());
|
||||
for process in sysinfo_processes {
|
||||
if process.thread_kind().is_some() {
|
||||
continue;
|
||||
}
|
||||
processes.push(MetricsCollector::process_node(process)?);
|
||||
}
|
||||
Ok(IdmMetricNode::structural("process", processes))
|
||||
}
|
||||
|
||||
fn process_node(process: &Process) -> Result<IdmMetricNode> {
|
||||
let mut metrics = vec![];
|
||||
|
||||
if let Some(parent) = process.parent() {
|
||||
metrics.push(IdmMetricNode::value(
|
||||
"parent",
|
||||
parent.as_u32() as u64,
|
||||
IdmMetricFormat::Integer,
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(exe) = process.exe().and_then(path_as_str) {
|
||||
metrics.push(IdmMetricNode::raw_value("executable", exe));
|
||||
}
|
||||
|
||||
if let Some(working_directory) = process.cwd().and_then(path_as_str) {
|
||||
metrics.push(IdmMetricNode::raw_value("cwd", working_directory));
|
||||
}
|
||||
|
||||
let cmdline = process.cmd().to_vec();
|
||||
metrics.push(IdmMetricNode::raw_value("cmdline", cmdline));
|
||||
metrics.push(IdmMetricNode::structural(
|
||||
"memory",
|
||||
vec![
|
||||
IdmMetricNode::value("resident", process.memory(), IdmMetricFormat::Bytes),
|
||||
IdmMetricNode::value("virtual", process.virtual_memory(), IdmMetricFormat::Bytes),
|
||||
],
|
||||
));
|
||||
|
||||
metrics.push(IdmMetricNode::value(
|
||||
"lifetime",
|
||||
process.run_time(),
|
||||
IdmMetricFormat::DurationSeconds,
|
||||
));
|
||||
metrics.push(IdmMetricNode::value(
|
||||
"uid",
|
||||
process.user_id().map(|x| (*x).add(0)).unwrap_or(0) as f64,
|
||||
IdmMetricFormat::Integer,
|
||||
));
|
||||
metrics.push(IdmMetricNode::value(
|
||||
"gid",
|
||||
process.group_id().map(|x| (*x).add(0)).unwrap_or(0) as f64,
|
||||
IdmMetricFormat::Integer,
|
||||
));
|
||||
metrics.push(IdmMetricNode::value(
|
||||
"euid",
|
||||
process
|
||||
.effective_user_id()
|
||||
.map(|x| (*x).add(0))
|
||||
.unwrap_or(0) as f64,
|
||||
IdmMetricFormat::Integer,
|
||||
));
|
||||
metrics.push(IdmMetricNode::value(
|
||||
"egid",
|
||||
process.effective_group_id().map(|x| x.add(0)).unwrap_or(0) as f64,
|
||||
IdmMetricFormat::Integer,
|
||||
));
|
||||
|
||||
Ok(IdmMetricNode::structural(
|
||||
process.pid().to_string(),
|
||||
metrics,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn path_as_str(path: &Path) -> Option<String> {
|
||||
String::from_utf8(path.as_os_str().as_encoded_bytes().to_vec()).ok()
|
||||
}
|
Reference in New Issue
Block a user