@@ -9,12 +9,13 @@ use std::fs;
99use std:: collections:: HashMap ;
1010use std:: sync:: LazyLock ;
1111use procfs;
12- use tracing:: { error, info} ;
12+ use tracing:: { error, info, warn } ;
1313use syslog_tracing;
1414use std:: ffi:: CString ;
1515use serde:: Deserialize ;
1616
1717const 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