From 867ecb9a1bc928dfbe8d49dcca200a0634822f61 Mon Sep 17 00:00:00 2001 From: Akash Suresh Date: Fri, 25 Sep 2020 17:07:40 -0400 Subject: [PATCH 1/4] hostmetricsreceiver: (filesystems) Add filters for mount point and filesystem type --- .../scraper/filesystemscraper/config.go | 92 ++++++++++++- .../scraper/filesystemscraper/factory_test.go | 2 +- .../filesystemscraper/filesystem_scraper.go | 73 +++++----- .../filesystem_scraper_test.go | 125 +++++++++++++++--- 4 files changed, 229 insertions(+), 63 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go index c304e914c79..bdfb6d7ff9a 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go @@ -15,6 +15,8 @@ package filesystemscraper import ( + "fmt" + "go.opentelemetry.io/collector/internal/processor/filterset" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" ) @@ -23,15 +25,93 @@ import ( type Config struct { internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct - // Include specifies a filter on the devices that should be included from the generated metrics. - // Exclude specifies a filter on the devices that should be excluded from the generated metrics. - // If neither `include` or `exclude` are set, metrics will be generated for all devices. - Include MatchConfig `mapstructure:"include"` - Exclude MatchConfig `mapstructure:"exclude"` + // IncludeDevices specifies a filter on the devices, filesystem types or mount points that should be included from the generated metrics. + IncludeDevices DeviceMatchConfig `mapstructure:"include_devices"` + // ExcludeDevices specifies a filter on the devices, filesystem types or mount points that should be excluded from the generated metrics. + ExcludeDevices DeviceMatchConfig `mapstructure:"exclude_devices"` + + // IncludeFSTypes specifies a filter on the filesystem types that should be included from the generated metrics. + IncludeFSTypes FSTypeMatchConfig `mapstructure:"include_fs_types"` + // ExcludeFSTypes specifies a filter on the filesystem types points that should be excluded from the generated metrics. + ExcludeFSTypes FSTypeMatchConfig `mapstructure:"exclude_fs_types"` + + // IncludeMountPoints specifies a filter on the mount points that should be included from the generated metrics. + IncludeMountPoints MountPointMatchConfig `mapstructure:"include_mount_points"` + // ExcludeMountPoints specifies a filter on the mount points that should be excluded from the generated metrics. + ExcludeMountPoints MountPointMatchConfig `mapstructure:"exclude_mount_points"` } -type MatchConfig struct { +type DeviceMatchConfig struct { filterset.Config `mapstructure:",squash"` Devices []string `mapstructure:"devices"` } + +type FSTypeMatchConfig struct { + filterset.Config `mapstructure:",squash"` + + FSTypes []string `mapstructure:"fs_types"` +} + +type MountPointMatchConfig struct { + filterset.Config `mapstructure:",squash"` + + MountPoints []string `mapstructure:"mount_points"` +} + +func (cfg *Config) newFilter() (*fsFilter, error) { + var err error + filter := fsFilter{} + + filter.includeDevice, err = newIncludeFilterHelper(cfg.IncludeDevices.Devices, &cfg.IncludeDevices.Config, "device") + if err != nil { + return nil, err + } + + filter.excludeDevice, err = newExcludeFilterHelper(cfg.ExcludeDevices.Devices, &cfg.ExcludeDevices.Config, "device") + if err != nil { + return nil, err + } + + filter.includeFSType, err = newIncludeFilterHelper(cfg.IncludeFSTypes.FSTypes, &cfg.IncludeFSTypes.Config, "fs_type") + if err != nil { + return nil, err + } + + filter.excludeFSType, err = newExcludeFilterHelper(cfg.ExcludeFSTypes.FSTypes, &cfg.ExcludeFSTypes.Config, "fs_type") + if err != nil { + return nil, err + } + + filter.includeMountPoint, err = newIncludeFilterHelper(cfg.IncludeMountPoints.MountPoints, &cfg.IncludeMountPoints.Config, "mount_point") + if err != nil { + return nil, err + } + + filter.excludeMountPoint, err = newExcludeFilterHelper(cfg.ExcludeMountPoints.MountPoints, &cfg.ExcludeMountPoints.Config, "mount_point") + if err != nil { + return nil, err + } + + return &filter, nil +} + +func newIncludeFilterHelper(items []string, filterSet *filterset.Config, typ string) (filterset.FilterSet, error) { + return newFilterHelper(items, filterSet, "include", typ) +} + +func newExcludeFilterHelper(items []string, filterSet *filterset.Config, typ string) (filterset.FilterSet, error) { + return newFilterHelper(items, filterSet, "exclude", typ) +} + +func newFilterHelper(items []string, filterSet *filterset.Config, typ string, filterType string) (filterset.FilterSet, error) { + var err error + var filter filterset.FilterSet + if len(items) > 0 { + filter, err = filterset.CreateFilterSet(items, filterSet) + if err != nil { + return nil, fmt.Errorf("error creating %s %s filters: %w", filterType, typ, err) + } + } + return filter, nil +} diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory_test.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory_test.go index f80b3e1ba66..7d2712f02bb 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/factory_test.go @@ -40,7 +40,7 @@ func TestCreateMetricsScraper(t *testing.T) { func TestCreateMetricsScraper_Error(t *testing.T) { factory := &Factory{} - cfg := &Config{Include: MatchConfig{Devices: []string{""}}} + cfg := &Config{IncludeDevices: DeviceMatchConfig{Devices: []string{""}}} _, err := factory.CreateMetricsScraper(context.Background(), zap.NewNop(), cfg) diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go index cdc0a8ce742..66ed70466d2 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go @@ -16,7 +16,6 @@ package filesystemscraper import ( "context" - "fmt" "strings" "time" @@ -30,15 +29,23 @@ import ( // scraper for FileSystem Metrics type scraper struct { - config *Config - includeFS filterset.FilterSet - excludeFS filterset.FilterSet + config *Config + fsFilter fsFilter // for mocking gopsutil disk.Partitions & disk.Usage partitions func(bool) ([]disk.PartitionStat, error) usage func(string) (*disk.UsageStat, error) } +type fsFilter struct { + includeDevice filterset.FilterSet + excludeDevice filterset.FilterSet + includeFSType filterset.FilterSet + excludeFSType filterset.FilterSet + includeMountPoint filterset.FilterSet + excludeMountPoint filterset.FilterSet +} + type deviceUsage struct { partition disk.PartitionStat usage *disk.UsageStat @@ -46,24 +53,12 @@ type deviceUsage struct { // newFileSystemScraper creates a FileSystem Scraper func newFileSystemScraper(_ context.Context, cfg *Config) (*scraper, error) { - scraper := &scraper{config: cfg, partitions: disk.Partitions, usage: disk.Usage} - - var err error - - if len(cfg.Include.Devices) > 0 { - scraper.includeFS, err = filterset.CreateFilterSet(cfg.Include.Devices, &cfg.Include.Config) - if err != nil { - return nil, fmt.Errorf("error creating device include filters: %w", err) - } - } - - if len(cfg.Exclude.Devices) > 0 { - scraper.excludeFS, err = filterset.CreateFilterSet(cfg.Exclude.Devices, &cfg.Exclude.Config) - if err != nil { - return nil, fmt.Errorf("error creating device exclude filters: %w", err) - } + fsFilter, err := cfg.newFilter() + if err != nil { + return nil, err } + scraper := &scraper{config: cfg, partitions: disk.Partitions, usage: disk.Usage, fsFilter: *fsFilter} return scraper, nil } @@ -92,7 +87,9 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { var errors []error usages := make([]*deviceUsage, 0, len(partitions)) for _, partition := range partitions { - // TODO: Add filters to include/exclude mount points + if !s.fsFilter.includePartition(partition) { + continue + } usage, err := s.usage(partition.Mountpoint) if err != nil { errors = append(errors, err) @@ -102,9 +99,6 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { usages = append(usages, &deviceUsage{partition, usage}) } - // filter devices by name - usages = s.filterByDevice(usages) - if len(usages) > 0 { metrics.Resize(1 + systemSpecificMetricsLen) @@ -155,21 +149,26 @@ func exists(options []string, opt string) bool { return false } -func (s *scraper) filterByDevice(usages []*deviceUsage) []*deviceUsage { - if s.includeFS == nil && s.excludeFS == nil { - return usages +func (f *fsFilter) includePartition(partition disk.PartitionStat) bool { + if f.wantDevice(partition.Device) && + f.wantFSType(partition.Fstype) && + f.wantMountPoint(partition.Mountpoint) { + return true } + return false +} - filteredUsages := make([]*deviceUsage, 0, len(usages)) - for _, usage := range usages { - if s.includeDevice(usage.partition.Device) { - filteredUsages = append(filteredUsages, usage) - } - } - return filteredUsages +func (f *fsFilter) wantDevice(deviceName string) bool { + return (f.includeDevice == nil || f.includeDevice.Matches(deviceName)) && + (f.excludeDevice == nil || !f.excludeDevice.Matches(deviceName)) +} + +func (f *fsFilter) wantFSType(fsType string) bool { + return (f.includeFSType == nil || f.includeFSType.Matches(fsType)) && + (f.excludeFSType == nil || !f.excludeFSType.Matches(fsType)) } -func (s *scraper) includeDevice(deviceName string) bool { - return (s.includeFS == nil || s.includeFS.Matches(deviceName)) && - (s.excludeFS == nil || !s.excludeFS.Matches(deviceName)) +func (f *fsFilter) wantMountPoint(mountPoint string) bool { + return (f.includeMountPoint == nil || f.includeMountPoint.Matches(mountPoint)) && + (f.excludeMountPoint == nil || !f.excludeMountPoint.Matches(mountPoint)) } diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go index dbb32931e45..cbaebea80cc 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go @@ -31,14 +31,15 @@ import ( func TestScrapeMetrics(t *testing.T) { type testCase struct { - name string - config Config - partitionsFunc func(bool) ([]disk.PartitionStat, error) - usageFunc func(string) (*disk.UsageStat, error) - expectMetrics bool - expectedDeviceDataPoints int - newErrRegex string - expectedErr string + name string + config Config + partitionsFunc func(bool) ([]disk.PartitionStat, error) + usageFunc func(string) (*disk.UsageStat, error) + expectMetrics bool + expectedDeviceDataPoints int + expectedDeviceLabelValues []map[string]string + newErrRegex string + expectedErr string } testCases := []testCase{ @@ -47,8 +48,8 @@ func TestScrapeMetrics(t *testing.T) { expectMetrics: true, }, { - name: "Include single process filter", - config: Config{Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"a"}}}, + name: "Include single device filter", + config: Config{IncludeDevices: DeviceMatchConfig{filterset.Config{MatchType: "strict"}, []string{"a"}}}, partitionsFunc: func(bool) ([]disk.PartitionStat, error) { return []disk.PartitionStat{{Device: "a"}, {Device: "b"}}, nil }, @@ -59,18 +60,84 @@ func TestScrapeMetrics(t *testing.T) { expectedDeviceDataPoints: 1, }, { - name: "Include Filter that matches nothing", - config: Config{Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"@*^#&*$^#)"}}}, + name: "Include Device Filter that matches nothing", + config: Config{IncludeDevices: DeviceMatchConfig{filterset.Config{MatchType: "strict"}, []string{"@*^#&*$^#)"}}}, expectMetrics: false, }, { - name: "Invalid Include Filter", - config: Config{Include: MatchConfig{Devices: []string{"test"}}}, + name: "Include filter with devices, filesystem type and mount points", + config: Config{ + IncludeDevices: DeviceMatchConfig{ + Config: filterset.Config{ + MatchType: filterset.Strict, + }, + Devices: []string{"device_a", "device_b"}, + }, + ExcludeFSTypes: FSTypeMatchConfig{ + Config: filterset.Config{ + MatchType: filterset.Strict, + }, + FSTypes: []string{"fs_type_b"}, + }, + ExcludeMountPoints: MountPointMatchConfig{ + Config: filterset.Config{ + MatchType: filterset.Strict, + }, + MountPoints: []string{"mount_point_b", "mount_point_c"}, + }, + }, + usageFunc: func(s string) (*disk.UsageStat, error) { + return &disk.UsageStat{ + Fstype: "fs_type_a", + }, nil + }, + partitionsFunc: func(b bool) ([]disk.PartitionStat, error) { + return []disk.PartitionStat{ + { + Device: "device_a", + Mountpoint: "mount_point_a", + Fstype: "fs_type_a", + }, + { + Device: "device_a", + Mountpoint: "mount_point_b", + Fstype: "fs_type_b", + }, + { + Device: "device_b", + Mountpoint: "mount_point_c", + Fstype: "fs_type_b", + }, + { + Device: "device_b", + Mountpoint: "mount_point_d", + Fstype: "fs_type_c", + }, + }, nil + }, + expectMetrics: true, + expectedDeviceDataPoints: 2, + expectedDeviceLabelValues: []map[string]string{ + { + "device": "device_a", + "mount.point": "mount_point_a", + "type": "fs_type_a", + }, + { + "device": "device_b", + "mount.point": "mount_point_d", + "type": "fs_type_c", + }, + }, + }, + { + name: "Invalid Include Device Filter", + config: Config{IncludeDevices: DeviceMatchConfig{Devices: []string{"test"}}}, newErrRegex: "^error creating device include filters:", }, { - name: "Invalid Exclude Filter", - config: Config{Exclude: MatchConfig{Devices: []string{"test"}}}, + name: "Invalid Exclude Device Filter", + config: Config{ExcludeDevices: DeviceMatchConfig{Devices: []string{"test"}}}, newErrRegex: "^error creating device exclude filters:", }, { @@ -120,11 +187,16 @@ func TestScrapeMetrics(t *testing.T) { assert.GreaterOrEqual(t, metrics.Len(), 1) - assertFileSystemUsageMetricValid(t, metrics.At(0), fileSystemUsageDescriptor, test.expectedDeviceDataPoints*fileSystemStatesLen) + assertFileSystemUsageMetricValid( + t, metrics.At(0), fileSystemUsageDescriptor, + test.expectedDeviceDataPoints*fileSystemStatesLen, test.expectedDeviceLabelValues, + ) if isUnix() { assertFileSystemUsageMetricHasUnixSpecificStateLabels(t, metrics.At(0)) - assertFileSystemUsageMetricValid(t, metrics.At(1), fileSystemINodesUsageDescriptor, test.expectedDeviceDataPoints*2) + assertFileSystemUsageMetricValid(t, metrics.At(1), fileSystemINodesUsageDescriptor, + test.expectedDeviceDataPoints*2, test.expectedDeviceLabelValues, + ) } internal.AssertSameTimeStampForAllMetrics(t, metrics) @@ -132,7 +204,8 @@ func TestScrapeMetrics(t *testing.T) { } } -func assertFileSystemUsageMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.Metric, expectedDeviceDataPoints int) { +func assertFileSystemUsageMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.Metric, + expectedDeviceDataPoints int, expectedDeviceLabelValues []map[string]string) { internal.AssertDescriptorEqual(t, descriptor, metric) for i := 0; i < metric.IntSum().DataPoints().Len(); i++ { for _, label := range []string{deviceLabelName, typeLabelName, mountModeLabelName, mountPointLabelName} { @@ -142,6 +215,20 @@ func assertFileSystemUsageMetricValid(t *testing.T, metric pdata.Metric, descrip if expectedDeviceDataPoints > 0 { assert.Equal(t, expectedDeviceDataPoints, metric.IntSum().DataPoints().Len()) + + // Assert label values if specified. + if expectedDeviceLabelValues != nil { + dpsPerDevice := expectedDeviceDataPoints / len(expectedDeviceLabelValues) + deviceIdx := 0 + for i := 0; i < metric.IntSum().DataPoints().Len(); i += dpsPerDevice { + for j := i; j < i+dpsPerDevice; j++ { + for labelKey, labelValue := range expectedDeviceLabelValues[deviceIdx] { + internal.AssertIntSumMetricLabelHasValue(t, metric, j, labelKey, labelValue) + } + } + deviceIdx++ + } + } } else { assert.GreaterOrEqual(t, metric.IntSum().DataPoints().Len(), fileSystemStatesLen) } From da976bce47ca2564a8f9a4bfe3adb27d160fa70d Mon Sep 17 00:00:00 2001 From: Akash Suresh Date: Tue, 29 Sep 2020 08:39:14 -0400 Subject: [PATCH 2/4] Address comments --- .../scraper/filesystemscraper/config.go | 49 ++++++++++++++----- .../filesystemscraper/filesystem_scraper.go | 37 ++++++-------- .../filesystem_scraper_test.go | 23 ++++++--- 3 files changed, 67 insertions(+), 42 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go index bdfb6d7ff9a..b28e08913fa 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/config.go @@ -25,17 +25,17 @@ import ( type Config struct { internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct - // IncludeDevices specifies a filter on the devices, filesystem types or mount points that should be included from the generated metrics. + // IncludeDevices specifies a filter on the devices that should be included in the generated metrics. IncludeDevices DeviceMatchConfig `mapstructure:"include_devices"` - // ExcludeDevices specifies a filter on the devices, filesystem types or mount points that should be excluded from the generated metrics. + // ExcludeDevices specifies a filter on the devices that should be excluded from the generated metrics. ExcludeDevices DeviceMatchConfig `mapstructure:"exclude_devices"` - // IncludeFSTypes specifies a filter on the filesystem types that should be included from the generated metrics. + // IncludeFSTypes specifies a filter on the filesystem types that should be included in the generated metrics. IncludeFSTypes FSTypeMatchConfig `mapstructure:"include_fs_types"` // ExcludeFSTypes specifies a filter on the filesystem types points that should be excluded from the generated metrics. ExcludeFSTypes FSTypeMatchConfig `mapstructure:"exclude_fs_types"` - // IncludeMountPoints specifies a filter on the mount points that should be included from the generated metrics. + // IncludeMountPoints specifies a filter on the mount points that should be included in the generated metrics. IncludeMountPoints MountPointMatchConfig `mapstructure:"include_mount_points"` // ExcludeMountPoints specifies a filter on the mount points that should be excluded from the generated metrics. ExcludeMountPoints MountPointMatchConfig `mapstructure:"exclude_mount_points"` @@ -59,54 +59,77 @@ type MountPointMatchConfig struct { MountPoints []string `mapstructure:"mount_points"` } -func (cfg *Config) newFilter() (*fsFilter, error) { +type fsFilter struct { + includeDeviceFilter filterset.FilterSet + excludeDeviceFilter filterset.FilterSet + includeFSTypeFilter filterset.FilterSet + excludeFSTypeFilter filterset.FilterSet + includeMountPointFilter filterset.FilterSet + excludeMountPointFilter filterset.FilterSet + filtersExist bool +} + +func (cfg *Config) createFilter() (*fsFilter, error) { var err error filter := fsFilter{} - filter.includeDevice, err = newIncludeFilterHelper(cfg.IncludeDevices.Devices, &cfg.IncludeDevices.Config, "device") + filter.includeDeviceFilter, err = newIncludeFilterHelper(cfg.IncludeDevices.Devices, &cfg.IncludeDevices.Config, deviceLabelName) if err != nil { return nil, err } - filter.excludeDevice, err = newExcludeFilterHelper(cfg.ExcludeDevices.Devices, &cfg.ExcludeDevices.Config, "device") + filter.excludeDeviceFilter, err = newExcludeFilterHelper(cfg.ExcludeDevices.Devices, &cfg.ExcludeDevices.Config, deviceLabelName) if err != nil { return nil, err } - filter.includeFSType, err = newIncludeFilterHelper(cfg.IncludeFSTypes.FSTypes, &cfg.IncludeFSTypes.Config, "fs_type") + filter.includeFSTypeFilter, err = newIncludeFilterHelper(cfg.IncludeFSTypes.FSTypes, &cfg.IncludeFSTypes.Config, typeLabelName) if err != nil { return nil, err } - filter.excludeFSType, err = newExcludeFilterHelper(cfg.ExcludeFSTypes.FSTypes, &cfg.ExcludeFSTypes.Config, "fs_type") + filter.excludeFSTypeFilter, err = newExcludeFilterHelper(cfg.ExcludeFSTypes.FSTypes, &cfg.ExcludeFSTypes.Config, typeLabelName) if err != nil { return nil, err } - filter.includeMountPoint, err = newIncludeFilterHelper(cfg.IncludeMountPoints.MountPoints, &cfg.IncludeMountPoints.Config, "mount_point") + filter.includeMountPointFilter, err = newIncludeFilterHelper(cfg.IncludeMountPoints.MountPoints, &cfg.IncludeMountPoints.Config, mountPointLabelName) if err != nil { return nil, err } - filter.excludeMountPoint, err = newExcludeFilterHelper(cfg.ExcludeMountPoints.MountPoints, &cfg.ExcludeMountPoints.Config, "mount_point") + filter.excludeMountPointFilter, err = newExcludeFilterHelper(cfg.ExcludeMountPoints.MountPoints, &cfg.ExcludeMountPoints.Config, mountPointLabelName) if err != nil { return nil, err } + filter.setFiltersExist() return &filter, nil } +func (f *fsFilter) setFiltersExist() { + f.filtersExist = f.includeMountPointFilter != nil || f.excludeMountPointFilter != nil || + f.includeFSTypeFilter != nil || f.excludeFSTypeFilter != nil || + f.includeDeviceFilter != nil || f.excludeDeviceFilter != nil +} + +const ( + excludeKey = "exclude" + includeKey = "include" +) + func newIncludeFilterHelper(items []string, filterSet *filterset.Config, typ string) (filterset.FilterSet, error) { - return newFilterHelper(items, filterSet, "include", typ) + return newFilterHelper(items, filterSet, includeKey, typ) } func newExcludeFilterHelper(items []string, filterSet *filterset.Config, typ string) (filterset.FilterSet, error) { - return newFilterHelper(items, filterSet, "exclude", typ) + return newFilterHelper(items, filterSet, excludeKey, typ) } func newFilterHelper(items []string, filterSet *filterset.Config, typ string, filterType string) (filterset.FilterSet, error) { var err error var filter filterset.FilterSet + if len(items) > 0 { filter, err = filterset.CreateFilterSet(items, filterSet) if err != nil { diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go index 66ed70466d2..acd6ef5cdd3 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go @@ -23,7 +23,6 @@ import ( "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/internal/processor/filterset" "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" ) @@ -37,15 +36,6 @@ type scraper struct { usage func(string) (*disk.UsageStat, error) } -type fsFilter struct { - includeDevice filterset.FilterSet - excludeDevice filterset.FilterSet - includeFSType filterset.FilterSet - excludeFSType filterset.FilterSet - includeMountPoint filterset.FilterSet - excludeMountPoint filterset.FilterSet -} - type deviceUsage struct { partition disk.PartitionStat usage *disk.UsageStat @@ -53,7 +43,7 @@ type deviceUsage struct { // newFileSystemScraper creates a FileSystem Scraper func newFileSystemScraper(_ context.Context, cfg *Config) (*scraper, error) { - fsFilter, err := cfg.newFilter() + fsFilter, err := cfg.createFilter() if err != nil { return nil, err } @@ -150,25 +140,26 @@ func exists(options []string, opt string) bool { } func (f *fsFilter) includePartition(partition disk.PartitionStat) bool { - if f.wantDevice(partition.Device) && - f.wantFSType(partition.Fstype) && - f.wantMountPoint(partition.Mountpoint) { + // If filters do not exist, return early. + if !f.filtersExist || (f.includeDevice(partition.Device) && + f.includeFSType(partition.Fstype) && + f.includeMountPoint(partition.Mountpoint)) { return true } return false } -func (f *fsFilter) wantDevice(deviceName string) bool { - return (f.includeDevice == nil || f.includeDevice.Matches(deviceName)) && - (f.excludeDevice == nil || !f.excludeDevice.Matches(deviceName)) +func (f *fsFilter) includeDevice(deviceName string) bool { + return (f.includeDeviceFilter == nil || f.includeDeviceFilter.Matches(deviceName)) && + (f.excludeDeviceFilter == nil || !f.excludeDeviceFilter.Matches(deviceName)) } -func (f *fsFilter) wantFSType(fsType string) bool { - return (f.includeFSType == nil || f.includeFSType.Matches(fsType)) && - (f.excludeFSType == nil || !f.excludeFSType.Matches(fsType)) +func (f *fsFilter) includeFSType(fsType string) bool { + return (f.includeFSTypeFilter == nil || f.includeFSTypeFilter.Matches(fsType)) && + (f.excludeFSTypeFilter == nil || !f.excludeFSTypeFilter.Matches(fsType)) } -func (f *fsFilter) wantMountPoint(mountPoint string) bool { - return (f.includeMountPoint == nil || f.includeMountPoint.Matches(mountPoint)) && - (f.excludeMountPoint == nil || !f.excludeMountPoint.Matches(mountPoint)) +func (f *fsFilter) includeMountPoint(mountPoint string) bool { + return (f.includeMountPointFilter == nil || f.includeMountPointFilter.Matches(mountPoint)) && + (f.excludeMountPointFilter == nil || !f.excludeMountPointFilter.Matches(mountPoint)) } diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go index cbaebea80cc..5fd369213a4 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go @@ -188,14 +188,21 @@ func TestScrapeMetrics(t *testing.T) { assert.GreaterOrEqual(t, metrics.Len(), 1) assertFileSystemUsageMetricValid( - t, metrics.At(0), fileSystemUsageDescriptor, - test.expectedDeviceDataPoints*fileSystemStatesLen, test.expectedDeviceLabelValues, + t, + metrics.At(0), + fileSystemUsageDescriptor, + test.expectedDeviceDataPoints*fileSystemStatesLen, + test.expectedDeviceLabelValues, ) if isUnix() { assertFileSystemUsageMetricHasUnixSpecificStateLabels(t, metrics.At(0)) - assertFileSystemUsageMetricValid(t, metrics.At(1), fileSystemINodesUsageDescriptor, - test.expectedDeviceDataPoints*2, test.expectedDeviceLabelValues, + assertFileSystemUsageMetricValid( + t, + metrics.At(1), + fileSystemINodesUsageDescriptor, + test.expectedDeviceDataPoints*2, + test.expectedDeviceLabelValues, ) } @@ -204,8 +211,12 @@ func TestScrapeMetrics(t *testing.T) { } } -func assertFileSystemUsageMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.Metric, - expectedDeviceDataPoints int, expectedDeviceLabelValues []map[string]string) { +func assertFileSystemUsageMetricValid( + t *testing.T, + metric pdata.Metric, + descriptor pdata.Metric, + expectedDeviceDataPoints int, + expectedDeviceLabelValues []map[string]string) { internal.AssertDescriptorEqual(t, descriptor, metric) for i := 0; i < metric.IntSum().DataPoints().Len(); i++ { for _, label := range []string{deviceLabelName, typeLabelName, mountModeLabelName, mountPointLabelName} { From 5f8a30e162de7b06cde27aefd278186d3515e2ec Mon Sep 17 00:00:00 2001 From: Akash Suresh Date: Tue, 29 Sep 2020 08:41:48 -0400 Subject: [PATCH 3/4] Improve coverage --- .../filesystem_scraper_test.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go index 5fd369213a4..d4ec2b12897 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go @@ -140,6 +140,26 @@ func TestScrapeMetrics(t *testing.T) { config: Config{ExcludeDevices: DeviceMatchConfig{Devices: []string{"test"}}}, newErrRegex: "^error creating device exclude filters:", }, + { + name: "Invalid Include Filesystems Filter", + config: Config{IncludeFSTypes: FSTypeMatchConfig{FSTypes: []string{"test"}}}, + newErrRegex: "^error creating type include filters:", + }, + { + name: "Invalid Exclude Filesystems Filter", + config: Config{ExcludeFSTypes: FSTypeMatchConfig{FSTypes: []string{"test"}}}, + newErrRegex: "^error creating type exclude filters:", + }, + { + name: "Invalid Include Moountpoints Filter", + config: Config{IncludeMountPoints: MountPointMatchConfig{MountPoints: []string{"test"}}}, + newErrRegex: "^error creating mount.point include filters:", + }, + { + name: "Invalid Exclude Moountpoints Filter", + config: Config{ExcludeMountPoints: MountPointMatchConfig{MountPoints: []string{"test"}}}, + newErrRegex: "^error creating mount.point exclude filters:", + }, { name: "Partitions Error", partitionsFunc: func(bool) ([]disk.PartitionStat, error) { return nil, errors.New("err1") }, From 5cbf3dcc61807b865e76b56e92891a798a23a428 Mon Sep 17 00:00:00 2001 From: Akash Suresh Date: Tue, 29 Sep 2020 08:43:18 -0400 Subject: [PATCH 4/4] Update README --- receiver/hostmetricsreceiver/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/receiver/hostmetricsreceiver/README.md b/receiver/hostmetricsreceiver/README.md index 27719fcd424..08dfec2349f 100644 --- a/receiver/hostmetricsreceiver/README.md +++ b/receiver/hostmetricsreceiver/README.md @@ -77,9 +77,15 @@ disk: ```yaml filesystem: - : + : devices: [ , ... ] match_type: + : + fs_types: [ , ... ] + match_type: + : + mount_points: [ , ... ] + match_type: ``` #### Network