Skip to content

Commit

Permalink
thread labels through new metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com>
  • Loading branch information
blt committed Dec 3, 2024
1 parent 6575d56 commit c1de391
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions lading/src/observer/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ impl Sampler {
let vsize: u64 = stats.vsize;

let labels = [
("pid", format!("{pid}")),
("exe", basename.clone()),
("cmdline", cmdline.clone()),
("comm", comm.clone()),
(String::from("pid"), format!("{pid}")),
(String::from("exe"), basename.clone()),
(String::from("cmdline"), cmdline.clone()),
(String::from("comm"), comm.clone()),
];

// Number of pages that the process has in real memory.
Expand Down Expand Up @@ -363,8 +363,8 @@ impl Sampler {
let labels = [
("pid", format!("{pid}")),
("exe", basename.clone()),
("comm", comm.clone()),
("cmdline", cmdline.clone()),
("comm", comm.clone()),
];

gauge!("smaps.rss.sum", &labels).set(measures.rss as f64);
Expand Down Expand Up @@ -404,7 +404,7 @@ impl Sampler {
// using the same heuristic as kubernetes:
// total_usage - inactive_file
let cgroup_path = v2::get_path(pid).await?;
v2::poll(cgroup_path).await?;
v2::poll(cgroup_path, &labels).await?;
}

gauge!("num_processes").set(total_processes as f64);
Expand Down
10 changes: 5 additions & 5 deletions lading/src/observer/linux/cgroup/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) async fn get_path(pid: i32) -> Result<PathBuf, Error> {
}

/// Polls for any cgroup metrics that can be read, v2 version.
pub(crate) async fn poll(path: PathBuf) -> Result<(), Error> {
pub(crate) async fn poll(path: PathBuf, labels: &[(String, String)]) -> Result<(), Error> {
let mut entries = fs::read_dir(&path).await?;

while let Some(entry) = entries.next_entry().await? {
Expand All @@ -65,10 +65,10 @@ pub(crate) async fn poll(path: PathBuf) -> Result<(), Error> {
// name.
if let Ok(value) = content.parse::<f64>() {
// Single-valued
gauge!(metric_prefix).set(value);
gauge!(metric_prefix, labels).set(value);
} else {
// Key-value pairs
if kv_pairs(content, &metric_prefix).is_err() {
if kv_pairs(content, &metric_prefix, labels).is_err() {
// File may fail to parse, for instance cgroup.controllers
// is a list of strings.
continue;
Expand All @@ -80,14 +80,14 @@ pub(crate) async fn poll(path: PathBuf) -> Result<(), Error> {
Ok(())
}

fn kv_pairs(content: &str, metric_prefix: &str) -> Result<(), Error> {
fn kv_pairs(content: &str, metric_prefix: &str, labels: &[(String, String)]) -> Result<(), Error> {
for line in content.lines() {
let mut parts = line.split_whitespace();
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()?;
let metric_name = format!("{metric_prefix}.{key}");
gauge!(metric_name).set(value);
gauge!(metric_name, labels).set(value);
}
Ok(())
}

0 comments on commit c1de391

Please sign in to comment.