Skip to content

Commit 87c5aa5

Browse files
committed
fix: collecting disk stats
For devices with multiple mount points, we were overwriting the entry in the hashmap. Changing the hashmap to a vec keeps all mounts, and prevents this issue.
1 parent 93b2751 commit 87c5aa5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/metrics/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,13 @@ impl Metric for DiskStatsCollector {
547547
fn collect(&self) -> DynFuture<'_, Result<()>> {
548548
Box::pin(async move {
549549
let io = IOMetrics::all().await?;
550-
for (device, stats) in &io.disks {
550+
for stats in &io.disks {
551551
if !self.mountpoints.iter().any(|el| el == &stats.mountpoint) {
552552
continue;
553553
}
554554

555555
let labels = DiskStatsLabels {
556-
device: device.clone(),
556+
device: stats.device.clone(),
557557
mountpoint: stats.mountpoint.clone(),
558558
};
559559

src/metrics/disk.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Metrics about bytes read/written to disks.
22
33
use anyhow::{Context, Result};
4-
use hashbrown::HashMap;
54

65
/// Metrics about the bytes read/written to each attached disk.
76
#[derive(Debug)]
87
pub struct IOMetrics {
98
/// Each disk is one entry.
10-
pub disks: HashMap<String, DiskMetrics>,
9+
pub disks: Vec<DiskMetrics>,
1110
}
1211

1312
/// Metrics about a single disk.
@@ -19,6 +18,10 @@ pub struct DiskMetrics {
1918
pub bytes_written_total: u64,
2019
/// Mountpoint
2120
pub mountpoint: String,
21+
/// Device name
22+
pub rootdevice: String,
23+
/// Device name
24+
pub device: String,
2225
}
2326

2427
impl IOMetrics {
@@ -30,7 +33,7 @@ impl IOMetrics {
3033
let mounts = smol::unblock(|| procfs::mounts().context("reading /proc/mounts")).await?;
3134

3235
let mut ret = Self {
33-
disks: HashMap::new(),
36+
disks: Vec::new(),
3437
};
3538

3639
for mount in &mounts {
@@ -68,13 +71,16 @@ impl IOMetrics {
6871
.trim()
6972
.parse::<u64>()
7073
.context("parsing sector size")?;
74+
7175
let metrics = DiskMetrics {
7276
bytes_read_total: stat.sectors_read * sector_size,
7377
bytes_written_total: stat.sectors_written * sector_size,
7478
mountpoint: mount_point.clone(),
79+
rootdevice: stat.name.clone(),
80+
device: device.clone(),
7581
};
7682

77-
ret.disks.insert(stat.name.clone(), metrics);
83+
ret.disks.push(metrics);
7884
}
7985

8086
Ok(ret)

0 commit comments

Comments
 (0)