Skip to content

Commit

Permalink
Add device filter for file system scraper (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington authored Jul 16, 2020
1 parent 9031485 commit 558df2e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@

package filesystemscraper

import "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
import (
"go.opentelemetry.io/collector/internal/processor/filterset"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
)

// Config relating to FileSystem Metric Scraper.
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"`
}

type MatchConfig struct {
filterset.Config `mapstructure:",squash"`

Devices []string `mapstructure:"devices"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (f *Factory) CreateMetricsScraper(
_ *zap.Logger,
config internal.Config,
) (internal.Scraper, error) {
cfg := config.(*Config)
return obsreportscraper.WrapScraper(newFileSystemScraper(ctx, cfg), TypeStr), nil
scraper, err := newFileSystemScraper(ctx, config.(*Config))
if err != nil {
return nil, err
}

return obsreportscraper.WrapScraper(scraper, TypeStr), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ func TestCreateMetricsScraper(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, scraper)
}

func TestCreateMetricsScraper_Error(t *testing.T) {
factory := &Factory{}
cfg := &Config{Include: MatchConfig{Devices: []string{""}}}

_, err := factory.CreateMetricsScraper(context.Background(), zap.NewNop(), cfg)

assert.Error(t, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ package filesystemscraper

import (
"context"
"fmt"
"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"
)

// scraper for FileSystem Metrics
type scraper struct {
config *Config
config *Config
includeFS filterset.FilterSet
excludeFS filterset.FilterSet

// for mocking gopsutil disk.Partitions & disk.Usage
partitions func(bool) ([]disk.PartitionStat, error)
Expand All @@ -39,8 +43,26 @@ type deviceUsage struct {
}

// newFileSystemScraper creates a FileSystem Scraper
func newFileSystemScraper(_ context.Context, cfg *Config) *scraper {
return &scraper{config: cfg, partitions: disk.Partitions, usage: disk.Usage}
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)
}
}

return scraper, nil
}

// Initialize
Expand Down Expand Up @@ -75,11 +97,19 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) {
usages = append(usages, &deviceUsage{partition.Device, usage})
}

if len(usages) > 0 {
// filter devices by name
filteredUsages := make([]*deviceUsage, 0, len(usages))
for _, usage := range usages {
if s.includeDevice(usage.deviceName) {
filteredUsages = append(filteredUsages, usage)
}
}

if len(filteredUsages) > 0 {
metrics.Resize(1 + systemSpecificMetricsLen)

initializeFileSystemUsageMetric(metrics.At(0), usages)
appendSystemSpecificMetrics(metrics, 1, usages)
initializeFileSystemUsageMetric(metrics.At(0), filteredUsages)
appendSystemSpecificMetrics(metrics, 1, filteredUsages)
}

if len(errors) > 0 {
Expand All @@ -106,3 +136,8 @@ func initializeFileSystemUsageDataPoint(dataPoint pdata.Int64DataPoint, deviceLa
dataPoint.SetTimestamp(pdata.TimestampUnixNano(uint64(time.Now().UnixNano())))
dataPoint.SetValue(value)
}

func (s *scraper) includeDevice(deviceName string) bool {
return (s.includeFS == nil || s.includeFS.Matches(deviceName)) &&
(s.excludeFS == nil || !s.excludeFS.Matches(deviceName))
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,40 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/processor/filterset"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
)

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
newErrRegex string
expectedErr string
}

testCases := []testCase{
{
name: "Standard",
name: "Standard",
expectMetrics: true,
},
{
name: "Include Filter that matches nothing",
config: Config{Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"@*^#&*$^#)"}}},
expectMetrics: false,
},
{
name: "Invalid Include Filter",
config: Config{Include: MatchConfig{Devices: []string{"test"}}},
newErrRegex: "^error creating device include filters:",
},
{
name: "Invalid Exclude Filter",
config: Config{Exclude: MatchConfig{Devices: []string{"test"}}},
newErrRegex: "^error creating device exclude filters:",
},
{
name: "Partitions Error",
Expand All @@ -54,15 +74,22 @@ func TestScrapeMetrics(t *testing.T) {

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
scraper := newFileSystemScraper(context.Background(), &Config{})
scraper, err := newFileSystemScraper(context.Background(), &test.config)
if test.newErrRegex != "" {
require.Error(t, err)
require.Regexp(t, test.newErrRegex, err)
return
}
require.NoError(t, err, "Failed to create file system scraper: %v", err)

if test.partitionsFunc != nil {
scraper.partitions = test.partitionsFunc
}
if test.usageFunc != nil {
scraper.usage = test.usageFunc
}

err := scraper.Initialize(context.Background())
err = scraper.Initialize(context.Background())
require.NoError(t, err, "Failed to initialize file system scraper: %v", err)
defer func() { assert.NoError(t, scraper.Close(context.Background())) }()

Expand All @@ -73,6 +100,11 @@ func TestScrapeMetrics(t *testing.T) {
}
require.NoError(t, err, "Failed to scrape metrics: %v", err)

if !test.expectMetrics {
assert.Equal(t, 0, metrics.Len())
return
}

assert.GreaterOrEqual(t, metrics.Len(), 1)

assertFileSystemUsageMetricValid(t, metrics.At(0), fileSystemUsageDescriptor)
Expand Down

0 comments on commit 558df2e

Please sign in to comment.