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

Optimize file system device filtering for simple case #1389

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,13 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) {
}

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

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

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

if len(errors) > 0 {
Expand Down Expand Up @@ -137,6 +132,20 @@ func initializeFileSystemUsageDataPoint(dataPoint pdata.Int64DataPoint, deviceLa
dataPoint.SetValue(value)
}

func (s *scraper) filterByDevice(usages []*deviceUsage) []*deviceUsage {
if s.includeFS == nil && s.excludeFS == nil {
return usages
}

filteredUsages := make([]*deviceUsage, 0, len(usages))
for _, usage := range usages {
if s.includeDevice(usage.deviceName) {
filteredUsages = append(filteredUsages, usage)
}
}
return filteredUsages
}

func (s *scraper) includeDevice(deviceName string) bool {
return (s.includeFS == nil || s.includeFS.Matches(deviceName)) &&
(s.excludeFS == nil || !s.excludeFS.Matches(deviceName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,33 @@ 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
newErrRegex string
expectedErr string
name string
config Config
partitionsFunc func(bool) ([]disk.PartitionStat, error)
usageFunc func(string) (*disk.UsageStat, error)
expectMetrics bool
expectedDeviceDataPoints int
newErrRegex string
expectedErr string
}

testCases := []testCase{
{
name: "Standard",
expectMetrics: true,
},
{
name: "Include single process filter",
config: Config{Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"a"}}},
partitionsFunc: func(bool) ([]disk.PartitionStat, error) {
return []disk.PartitionStat{{Device: "a"}, {Device: "b"}}, 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 Expand Up @@ -107,19 +120,23 @@ func TestScrapeMetrics(t *testing.T) {

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

assertFileSystemUsageMetricValid(t, metrics.At(0), fileSystemUsageDescriptor)
assertFileSystemUsageMetricValid(t, metrics.At(0), fileSystemUsageDescriptor, test.expectedDeviceDataPoints*fileSystemStatesLen)

if isUnix() {
assertFileSystemUsageMetricHasUnixSpecificStateLabels(t, metrics.At(0))
assertFileSystemUsageMetricValid(t, metrics.At(1), fileSystemINodesUsageDescriptor)
assertFileSystemUsageMetricValid(t, metrics.At(1), fileSystemINodesUsageDescriptor, test.expectedDeviceDataPoints*2)
}
})
}
}

func assertFileSystemUsageMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.MetricDescriptor) {
func assertFileSystemUsageMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.MetricDescriptor, expectedDeviceDataPoints int) {
internal.AssertDescriptorEqual(t, descriptor, metric.MetricDescriptor())
assert.GreaterOrEqual(t, metric.Int64DataPoints().Len(), 2)
if expectedDeviceDataPoints > 0 {
assert.Equal(t, expectedDeviceDataPoints, metric.Int64DataPoints().Len())
} else {
assert.GreaterOrEqual(t, metric.Int64DataPoints().Len(), fileSystemStatesLen)
}
internal.AssertInt64MetricLabelHasValue(t, metric, 0, stateLabelName, usedLabelValue)
internal.AssertInt64MetricLabelHasValue(t, metric, 1, stateLabelName, freeLabelValue)
}
Expand Down