Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hostmetricsreceiver: (filesystems) Add filters for mount point and filesystem type #1866

Merged
merged 4 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ disk:

```yaml
filesystem:
<include|exclude>:
<include_devices|exclude_devices>:
devices: [ <device name>, ... ]
match_type: <strict|regexp>
<include_fs_types|exclude_fs_types>:
fs_types: [ <filesystem type>, ... ]
match_type: <strict|regexp>
<include_mount_points|exclude_mount_points>:
mount_points: [ <mount point>, ... ]
match_type: <strict|regexp>
```

#### Network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package filesystemscraper

import (
"fmt"

"go.opentelemetry.io/collector/internal/processor/filterset"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
)
Expand All @@ -23,15 +25,116 @@ 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 that should be included in the generated metrics.
IncludeDevices DeviceMatchConfig `mapstructure:"include_devices"`
// 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 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 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"`
}

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"`
}

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.includeDeviceFilter, err = newIncludeFilterHelper(cfg.IncludeDevices.Devices, &cfg.IncludeDevices.Config, deviceLabelName)
if err != nil {
return nil, err
}

filter.excludeDeviceFilter, err = newExcludeFilterHelper(cfg.ExcludeDevices.Devices, &cfg.ExcludeDevices.Config, deviceLabelName)
if err != nil {
return nil, err
}

filter.includeFSTypeFilter, err = newIncludeFilterHelper(cfg.IncludeFSTypes.FSTypes, &cfg.IncludeFSTypes.Config, typeLabelName)
if err != nil {
return nil, err
}

filter.excludeFSTypeFilter, err = newExcludeFilterHelper(cfg.ExcludeFSTypes.FSTypes, &cfg.ExcludeFSTypes.Config, typeLabelName)
if err != nil {
return nil, err
}

filter.includeMountPointFilter, err = newIncludeFilterHelper(cfg.IncludeMountPoints.MountPoints, &cfg.IncludeMountPoints.Config, mountPointLabelName)
if err != nil {
return nil, err
}

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, includeKey, typ)
}

func newExcludeFilterHelper(items []string, filterSet *filterset.Config, typ string) (filterset.FilterSet, error) {
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 {
return nil, fmt.Errorf("error creating %s %s filters: %w", filterType, typ, err)
}
}
return filter, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ package filesystemscraper

import (
"context"
"fmt"
"strings"
"time"

"github.com/shirou/gopsutil/disk"

"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"
)

// 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)
Expand All @@ -46,24 +43,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.createFilter()
if err != nil {
return nil, err
}

scraper := &scraper{config: cfg, partitions: disk.Partitions, usage: disk.Usage, fsFilter: *fsFilter}
return scraper, nil
}

Expand Down Expand Up @@ -92,7 +77,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)
Expand All @@ -102,9 +89,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)

Expand Down Expand Up @@ -155,21 +139,27 @@ 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 {
asuresh4 marked this conversation as resolved.
Show resolved Hide resolved
// 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
}

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) includeDevice(deviceName string) bool {
return (f.includeDeviceFilter == nil || f.includeDeviceFilter.Matches(deviceName)) &&
(f.excludeDeviceFilter == nil || !f.excludeDeviceFilter.Matches(deviceName))
}

func (f *fsFilter) includeFSType(fsType string) bool {
return (f.includeFSTypeFilter == nil || f.includeFSTypeFilter.Matches(fsType)) &&
(f.excludeFSTypeFilter == nil || !f.excludeFSTypeFilter.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) includeMountPoint(mountPoint string) bool {
return (f.includeMountPointFilter == nil || f.includeMountPointFilter.Matches(mountPoint)) &&
(f.excludeMountPointFilter == nil || !f.excludeMountPointFilter.Matches(mountPoint))
}
Loading