From b4b6486d48aaf89c963a81409899e6fb6e75dc7d Mon Sep 17 00:00:00 2001 From: "Brian L. Troutwine" Date: Tue, 3 Dec 2024 09:16:09 -0800 Subject: [PATCH] test fix Signed-off-by: Brian L. Troutwine --- lading/src/observer/linux/cgroup/v2.rs | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lading/src/observer/linux/cgroup/v2.rs b/lading/src/observer/linux/cgroup/v2.rs index e6e7304877..d492d16506 100644 --- a/lading/src/observer/linux/cgroup/v2.rs +++ b/lading/src/observer/linux/cgroup/v2.rs @@ -1,7 +1,11 @@ -use std::{io, path::PathBuf}; +use std::{ + io, + path::{Path, PathBuf}, +}; use metrics::gauge; use tokio::fs; +use tracing::debug; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -68,7 +72,7 @@ pub(crate) async fn poll(path: PathBuf, labels: &[(String, String)]) -> Result<( gauge!(metric_prefix, labels).set(value); } else { // Key-value pairs - if kv_pairs(content, &metric_prefix, labels).is_err() { + if kv_pairs(&file_path, content, &metric_prefix, labels).is_err() { // File may fail to parse, for instance cgroup.controllers // is a list of strings. continue; @@ -80,9 +84,31 @@ pub(crate) async fn poll(path: PathBuf, labels: &[(String, String)]) -> Result<( Ok(()) } -fn kv_pairs(content: &str, metric_prefix: &str, labels: &[(String, String)]) -> Result<(), Error> { +fn kv_pairs( + file_path: &Path, + content: &str, + metric_prefix: &str, + labels: &[(String, String)], +) -> Result<(), Error> { for line in content.lines() { let mut parts = line.split_whitespace(); + if let Some(key) = parts.next() { + if let Some(value_str) = parts.next() { + let value: f64 = value_str.parse()?; + let metric_name = format!("{metric_prefix}.{key}"); + gauge!(metric_name, labels).set(value); + } else { + debug!( + "[{path}] missing value in key/value pair: {content}", + path = file_path.to_string_lossy(), + ); + } + } else { + debug!( + "[{path} missing key in key/value pair: {content}", + path = file_path.to_string_lossy(), + ); + } let key = parts.next().expect("malformed key-value pair"); let value_str = parts.next().expect("malformed key-value pair"); let value: f64 = value_str.parse()?;