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): Refactor to use metrics metadata #2405

Merged
merged 2 commits into from
Jan 28, 2021
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

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

// Config relating to FileSystem Metric Scraper.
Expand Down Expand Up @@ -73,32 +74,32 @@ func (cfg *Config) createFilter() (*fsFilter, error) {
var err error
filter := fsFilter{}

filter.includeDeviceFilter, err = newIncludeFilterHelper(cfg.IncludeDevices.Devices, &cfg.IncludeDevices.Config, deviceLabelName)
filter.includeDeviceFilter, err = newIncludeFilterHelper(cfg.IncludeDevices.Devices, &cfg.IncludeDevices.Config, metadata.Labels.FilesystemDevice)
if err != nil {
return nil, err
}

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

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

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

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

filter.excludeMountPointFilter, err = newExcludeFilterHelper(cfg.ExcludeMountPoints.MountPoints, &cfg.ExcludeMountPoints.Config, mountPointLabelName)
filter.excludeMountPointFilter, err = newExcludeFilterHelper(cfg.ExcludeMountPoints.MountPoints, &cfg.ExcludeMountPoints.Config, metadata.Labels.FilesystemMountpoint)
if err != nil {
return nil, err
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/metadata"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

Expand Down Expand Up @@ -102,7 +103,7 @@ func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
}

func initializeFileSystemUsageMetric(metric pdata.Metric, now pdata.TimestampUnixNano, deviceUsages []*deviceUsage) {
fileSystemUsageDescriptor.CopyTo(metric)
metadata.Metrics.SystemFilesystemUsage.Init(metric)

idps := metric.IntSum().DataPoints()
idps.Resize(fileSystemStatesLen * len(deviceUsages))
Expand All @@ -113,11 +114,11 @@ func initializeFileSystemUsageMetric(metric pdata.Metric, now pdata.TimestampUni

func initializeFileSystemUsageDataPoint(dataPoint pdata.IntDataPoint, now pdata.TimestampUnixNano, partition disk.PartitionStat, stateLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(deviceLabelName, partition.Device)
labelsMap.Insert(typeLabelName, partition.Fstype)
labelsMap.Insert(mountModeLabelName, getMountMode(partition.Opts))
labelsMap.Insert(mountPointLabelName, partition.Mountpoint)
labelsMap.Insert(stateLabelName, stateLabel)
labelsMap.Insert(metadata.Labels.FilesystemDevice, partition.Device)
labelsMap.Insert(metadata.Labels.FilesystemType, partition.Fstype)
labelsMap.Insert(metadata.Labels.FilesystemMode, getMountMode(partition.Opts))
labelsMap.Insert(metadata.Labels.FilesystemMountpoint, partition.Mountpoint)
labelsMap.Insert(metadata.Labels.FilesystemState, stateLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetValue(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package filesystemscraper

import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/metadata"
)

const fileSystemStatesLen = 2

func appendFileSystemUsageStateDataPoints(idps pdata.IntDataPointSlice, startIdx int, now pdata.TimestampUnixNano, deviceUsage *deviceUsage) {
initializeFileSystemUsageDataPoint(idps.At(startIdx+0), now, deviceUsage.partition, usedLabelValue, int64(deviceUsage.usage.Used))
initializeFileSystemUsageDataPoint(idps.At(startIdx+1), now, deviceUsage.partition, freeLabelValue, int64(deviceUsage.usage.Free))
initializeFileSystemUsageDataPoint(idps.At(startIdx+0), now, deviceUsage.partition, metadata.LabelFilesystemState.Used, int64(deviceUsage.usage.Used))
initializeFileSystemUsageDataPoint(idps.At(startIdx+1), now, deviceUsage.partition, metadata.LabelFilesystemState.Free, int64(deviceUsage.usage.Free))
}

const systemSpecificMetricsLen = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/processor/filterset"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/metadata"
)

func TestScrape(t *testing.T) {
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestScrape(t *testing.T) {
assertFileSystemUsageMetricValid(
t,
metrics.At(0),
fileSystemUsageDescriptor,
metadata.Metrics.SystemFilesystemUsage.New(),
test.expectedDeviceDataPoints*fileSystemStatesLen,
test.expectedDeviceLabelValues,
)
Expand All @@ -226,7 +227,7 @@ func TestScrape(t *testing.T) {
assertFileSystemUsageMetricValid(
t,
metrics.At(1),
fileSystemINodesUsageDescriptor,
metadata.Metrics.SystemFilesystemInodesUsage.New(),
test.expectedDeviceDataPoints*2,
test.expectedDeviceLabelValues,
)
Expand All @@ -245,7 +246,7 @@ func assertFileSystemUsageMetricValid(
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} {
for _, label := range []string{"device", "type", "mode", "mountpoint"} {
internal.AssertIntSumMetricLabelExists(t, metric, i, label)
}
}
Expand All @@ -269,12 +270,12 @@ func assertFileSystemUsageMetricValid(
} else {
assert.GreaterOrEqual(t, metric.IntSum().DataPoints().Len(), fileSystemStatesLen)
}
internal.AssertIntSumMetricLabelHasValue(t, metric, 0, stateLabelName, usedLabelValue)
internal.AssertIntSumMetricLabelHasValue(t, metric, 1, stateLabelName, freeLabelValue)
internal.AssertIntSumMetricLabelHasValue(t, metric, 0, "state", "used")
internal.AssertIntSumMetricLabelHasValue(t, metric, 1, "state", "free")
}

func assertFileSystemUsageMetricHasUnixSpecificStateLabels(t *testing.T, metric pdata.Metric) {
internal.AssertIntSumMetricLabelHasValue(t, metric, 2, stateLabelName, reservedLabelValue)
internal.AssertIntSumMetricLabelHasValue(t, metric, 2, "state", "reserved")
}

func isUnix() bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ package filesystemscraper

import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/metadata"
)

const fileSystemStatesLen = 3

func appendFileSystemUsageStateDataPoints(idps pdata.IntDataPointSlice, startIdx int, now pdata.TimestampUnixNano, deviceUsage *deviceUsage) {
initializeFileSystemUsageDataPoint(idps.At(startIdx+0), now, deviceUsage.partition, usedLabelValue, int64(deviceUsage.usage.Used))
initializeFileSystemUsageDataPoint(idps.At(startIdx+1), now, deviceUsage.partition, freeLabelValue, int64(deviceUsage.usage.Free))
initializeFileSystemUsageDataPoint(idps.At(startIdx+2), now, deviceUsage.partition, reservedLabelValue, int64(deviceUsage.usage.Total-deviceUsage.usage.Used-deviceUsage.usage.Free))
initializeFileSystemUsageDataPoint(idps.At(startIdx+0), now, deviceUsage.partition, metadata.LabelFilesystemState.Used, int64(deviceUsage.usage.Used))
initializeFileSystemUsageDataPoint(idps.At(startIdx+1), now, deviceUsage.partition, metadata.LabelFilesystemState.Free, int64(deviceUsage.usage.Free))
initializeFileSystemUsageDataPoint(idps.At(startIdx+2), now, deviceUsage.partition, metadata.LabelFilesystemState.Reserved, int64(deviceUsage.usage.Total-deviceUsage.usage.Used-deviceUsage.usage.Free))
}

const systemSpecificMetricsLen = 1

func appendSystemSpecificMetrics(metrics pdata.MetricSlice, startIdx int, now pdata.TimestampUnixNano, deviceUsages []*deviceUsage) {
metric := metrics.At(startIdx)
fileSystemINodesUsageDescriptor.CopyTo(metric)
metadata.Metrics.SystemFilesystemInodesUsage.Init(metric)

idps := metric.IntSum().DataPoints()
idps.Resize(2 * len(deviceUsages))
for idx, deviceUsage := range deviceUsages {
startIndex := 2 * idx
initializeFileSystemUsageDataPoint(idps.At(startIndex+0), now, deviceUsage.partition, usedLabelValue, int64(deviceUsage.usage.InodesUsed))
initializeFileSystemUsageDataPoint(idps.At(startIndex+1), now, deviceUsage.partition, freeLabelValue, int64(deviceUsage.usage.InodesFree))
initializeFileSystemUsageDataPoint(idps.At(startIndex+0), now, deviceUsage.partition, metadata.LabelFilesystemState.Used, int64(deviceUsage.usage.InodesUsed))
initializeFileSystemUsageDataPoint(idps.At(startIndex+1), now, deviceUsage.partition, metadata.LabelFilesystemState.Free, int64(deviceUsage.usage.InodesFree))
}
}
Loading