Skip to content

Commit

Permalink
Do not duplicate filesystem metrics for devices with many mount point…
Browse files Browse the repository at this point in the history
…s. (#1617)

This commit fixes a bug that produces duplicate "filesystem" metric data points for linux devices that have more than one data point. For example:
```
{"device":"/dev/vda1","mountpoint":"/etc/resolv.conf","fstype":"ext4","opts":"rw,relatime"}
{"device":"/dev/vda1","mountpoint":"/etc/hostname","fstype":"ext4","opts":"rw,relatime"}
{"device":"/dev/vda1","mountpoint":"/etc/hosts","fstype":"ext4","opts":"rw,relatime"}
```
  • Loading branch information
dmitryax committed Aug 24, 2020
1 parent 6a3daca commit 15ede2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) {
var errors []error
usages := make([]*deviceUsage, 0, len(partitions))
for _, partition := range partitions {
// Skip partition stats having more than one mount point for the same device
if deviceUsageAlreadySet(partition.Device, usages) {
continue
}

usage, err := s.usage(partition.Mountpoint)
if err != nil {
errors = append(errors, err)
Expand All @@ -113,6 +118,15 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) {
return metrics, componenterror.CombineErrors(errors)
}

func deviceUsageAlreadySet(device string, usages []*deviceUsage) bool {
for _, usage := range usages {
if device == usage.deviceName {
return true
}
}
return false
}

func initializeFileSystemUsageMetric(metric pdata.Metric, now pdata.TimestampUnixNano, deviceUsages []*deviceUsage) {
fileSystemUsageDescriptor.CopyTo(metric.MetricDescriptor())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ func TestScrapeMetrics(t *testing.T) {
expectMetrics: true,
expectedDeviceDataPoints: 1,
},
{
name: "No duplicate metrics for devices having many mount point",
partitionsFunc: func(bool) ([]disk.PartitionStat, error) {
return []disk.PartitionStat{
{Device: "a", Mountpoint: "/mnt/a1"},
{Device: "a", Mountpoint: "/mnt/a2"},
}, nil
},
usageFunc: func(string) (*disk.UsageStat, error) {
return &disk.UsageStat{}, nil
},
expectMetrics: true,
expectedDeviceDataPoints: 1,
},
{
name: "Include Filter that matches nothing",
config: Config{Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"@*^#&*$^#)"}}},
Expand Down

0 comments on commit 15ede2d

Please sign in to comment.