@@ -244,8 +244,15 @@ func (dc *diskCollector) collect() {
244
244
if dc .config .IncludeRootBlk {
245
245
devices = append (devices , listRootBlockDevices (dc .config .LsblkTimeout )... )
246
246
}
247
+
248
+ partitions , err := disk .Partitions (false )
249
+ if err != nil {
250
+ glog .Errorf ("Failed to list disk partitions: %v" , err )
251
+ return
252
+ }
253
+
247
254
if dc .config .IncludeAllAttachedBlk {
248
- devices = append (devices , listAttachedBlockDevices ()... )
255
+ devices = append (devices , listAttachedBlockDevices (partitions )... )
249
256
}
250
257
251
258
// Fetch metrics from /proc, /sys.
@@ -254,11 +261,6 @@ func (dc *diskCollector) collect() {
254
261
glog .Errorf ("Failed to retrieve disk IO counters: %v" , err )
255
262
return
256
263
}
257
- partitions , err := disk .Partitions (false )
258
- if err != nil {
259
- glog .Errorf ("Failed to list disk partitions: %v" , err )
260
- return
261
- }
262
264
sampleTime := time .Now ()
263
265
defer func () { dc .lastSampleTime = sampleTime }()
264
266
@@ -310,15 +312,9 @@ func listRootBlockDevices(timeout time.Duration) []string {
310
312
}
311
313
312
314
// listAttachedBlockDevices lists all currently attached block devices.
313
- func listAttachedBlockDevices () []string {
315
+ func listAttachedBlockDevices (partitions []disk. PartitionStat ) []string {
314
316
blks := []string {}
315
317
316
- partitions , err := disk .Partitions (false )
317
- if err != nil {
318
- glog .Errorf ("Failed to retrieve the list of disk partitions: %v" , err )
319
- return blks
320
- }
321
-
322
318
for _ , partition := range partitions {
323
319
blks = append (blks , partition .Device )
324
320
}
0 commit comments