Skip to content

Commit 16f2988

Browse files
committed
fix(hostmetricsreceiver): do not duplicate mountpoint metrics
Mountpoints can be reported multiple times for each mount into a namespace. This causes duplicate metrics which causes issues with some exporters. Each instance of the mountpoint will have identical metrics, so it is safe to ignore repeated mountpoints. Closes open-telemetry#34512
1 parent c0ffc7b commit 16f2988

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,23 @@ func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
9090
}
9191

9292
usages := make([]*deviceUsage, 0, len(partitions))
93+
94+
type mountKey struct {
95+
mountpoint string
96+
device string
97+
}
98+
seen := map[mountKey]struct{}{}
99+
93100
for _, partition := range partitions {
101+
key := mountKey{
102+
mountpoint: partition.Mountpoint,
103+
device: partition.Device,
104+
}
105+
if _, ok := seen[key]; partition.Mountpoint != "" && ok {
106+
continue
107+
}
108+
seen[key] = struct{}{}
109+
94110
if !s.fsFilter.includePartition(partition) {
95111
continue
96112
}

receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,41 @@ func TestScrape(t *testing.T) {
309309
usageFunc: func(context.Context, string) (*disk.UsageStat, error) { return nil, errors.New("err2") },
310310
expectedErr: "err2",
311311
},
312+
{
313+
name: "Do not report duplicate mount points",
314+
config: Config{
315+
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
316+
},
317+
usageFunc: func(context.Context, string) (*disk.UsageStat, error) {
318+
return &disk.UsageStat{
319+
Fstype: "fs_type_a",
320+
}, nil
321+
},
322+
partitionsFunc: func(context.Context, bool) ([]disk.PartitionStat, error) {
323+
return []disk.PartitionStat{
324+
{
325+
Device: "device_a",
326+
Mountpoint: "mount_point_a",
327+
Fstype: "fs_type_a",
328+
},
329+
{
330+
Device: "device_a",
331+
Mountpoint: "mount_point_a",
332+
Fstype: "fs_type_a",
333+
},
334+
}, nil
335+
},
336+
expectMetrics: true,
337+
expectedDeviceDataPoints: 1,
338+
expectedDeviceAttributes: []map[string]pcommon.Value{
339+
{
340+
"device": pcommon.NewValueStr("device_a"),
341+
"mountpoint": pcommon.NewValueStr("mount_point_a"),
342+
"type": pcommon.NewValueStr("fs_type_a"),
343+
"mode": pcommon.NewValueStr("unknown"),
344+
},
345+
},
346+
},
312347
}
313348

314349
for _, test := range testCases {

0 commit comments

Comments
 (0)