Skip to content

Commit d9ba0bc

Browse files
Ignore invalid output from docker stats command
Signed-off-by: Junchao-Mellanox <junchao@nvidia.com>
1 parent fda4e65 commit d9ba0bc

File tree

1 file changed

+10
-2
lines changed
  • crates/procdockerstatsd-rs/src

1 file changed

+10
-2
lines changed

crates/procdockerstatsd-rs/src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use std::fs;
99
use std::collections::HashMap;
1010
use std::sync::LazyLock;
1111
use procfs;
12-
use tracing::{error, info};
12+
use tracing::{error, info, warn};
1313
use syslog_tracing;
1414
use std::ffi::CString;
1515
use serde::Deserialize;
1616

1717
const UPDATE_INTERVAL: u64 = 120; // 2 minutes
18+
const INVALID_CONTAINER_NAME: &str = "—-"; // invalid container name returned by docker stats command
1819

1920
#[derive(Debug, Deserialize)]
2021
#[serde(rename_all = "PascalCase")]
@@ -98,11 +99,18 @@ fn parse_docker_json_output(json_output: &str) -> HashMap<String, HashMap<String
9899
let stats: DockerStats = match serde_json::from_str(line) {
99100
Ok(s) => s,
100101
Err(e) => {
101-
error!("Failed to parse docker stats JSON: {}", e);
102+
error!("Failed to parse docker stats JSON for output {} with error {}", line, e);
102103
continue;
103104
}
104105
};
105106

107+
if stats.name.is_empty() || stats.name == INVALID_CONTAINER_NAME {
108+
// If a container stops suddenly after we send the docker stats command,
109+
// it might return with a container name "—-". We should ignore such output.
110+
warn!("Skipping docker stats JSON for container {} with output: {}", stats.id, line);
111+
continue;
112+
}
113+
106114
let key = format!("DOCKER_STATS|{}", stats.id);
107115
let mut container_data = HashMap::new();
108116

0 commit comments

Comments
 (0)