Skip to content

Commit c89fee6

Browse files
committed
Merge branch 'performance/improve_docker_stats_perf'
2 parents 55405ab + 698a3c6 commit c89fee6

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/metrics_logger/docker.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use chrono::Utc;
66
use futures_util::stream::StreamExt;
77
use std::collections::HashMap;
88
use std::sync::{Arc, Mutex};
9+
use std::time::Duration;
910
use tracing::{debug, error, warn};
1011

1112
/// Enters an infinite loop logging metrics for each process to the metrics log. This function is
@@ -98,6 +99,7 @@ pub async fn keep_logging(
9899
// continue;
99100
}
100101

102+
let mut last_stats_per_container: HashMap<String, Stats> = HashMap::new();
101103
loop {
102104
for container in &containers {
103105
if let Some(container_id) = container.id.as_ref() {
@@ -113,22 +115,28 @@ pub async fn keep_logging(
113115
container_id,
114116
Some(StatsOptions {
115117
stream: false,
118+
one_shot: true,
116119
..Default::default()
117120
}),
118121
)
122+
// // .skip(1)
123+
.take(1)
119124
.next()
120125
.await;
121126

122127
match docker_stats {
123128
Some(Ok(stats)) => {
124-
let cpu_metrics =
125-
calculate_cpu_metrics(container_id, container_name.to_string(), &stats);
126-
debug!(
127-
"Pushing metrics to metrics log form container name/s {:?}",
128-
container.names
129-
);
130-
metrics_log.lock().unwrap().push_metrics(cpu_metrics);
131-
debug!("Logged metrics for container {}", container_id);
129+
if let Some(previous) = last_stats_per_container.get(container_name) {
130+
let cpu_metrics =
131+
calculate_cpu_metrics(container_id, container_name.to_string(), &stats, &previous);
132+
debug!(
133+
"Pushing metrics to metrics log form container name/s {:?}",
134+
container.names
135+
);
136+
metrics_log.lock().unwrap().push_metrics(cpu_metrics);
137+
debug!("Logged metrics for container {}", container_id);
138+
}
139+
last_stats_per_container.insert(container_name.to_owned(), stats);
132140
}
133141
Some(Err(e)) => {
134142
error!("Error getting stats for container {}: {}", container_id, e);
@@ -144,15 +152,16 @@ pub async fn keep_logging(
144152
}
145153
}
146154
}
155+
tokio::time::sleep(Duration::from_millis(2000)).await;
147156
}
148157
}
149158

150-
fn calculate_cpu_metrics(container_id: &str, container_name: String, stats: &Stats) -> CpuMetrics {
159+
fn calculate_cpu_metrics(container_id: &str, container_name: String, stats: &Stats, previous_stats: &Stats) -> CpuMetrics {
151160
let core_count = stats.cpu_stats.online_cpus.unwrap_or(0);
152161
let cpu_delta =
153-
stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage;
162+
stats.cpu_stats.cpu_usage.total_usage - previous_stats.cpu_stats.cpu_usage.total_usage;
154163
let system_delta = stats.cpu_stats.system_cpu_usage.unwrap_or(0)
155-
- stats.precpu_stats.system_cpu_usage.unwrap_or(0);
164+
- previous_stats.cpu_stats.system_cpu_usage.unwrap_or(0);
156165
let cpu_usage = if system_delta > 0 {
157166
(cpu_delta as f64 / system_delta as f64) * core_count as f64
158167
} else {

0 commit comments

Comments
 (0)