Add diskstats include and exclude device flags

Use standard include/exclude pattern for device include/exclude in the
diskstats collector.

Signed-off-by: Ben Kochie <superq@gmail.com>
Co-authored-by: rushilenekar20 <rushilenekar20@gmail.com>
This commit is contained in:
rushilenekar20 2022-06-07 01:17:31 -05:00 committed by Ben Kochie
parent 88f1811eb1
commit 8fcc6320a2
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1
2 changed files with 32 additions and 14 deletions

View file

@ -17,9 +17,8 @@
package collector package collector
import ( import (
"errors"
"fmt" "fmt"
"regexp"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level" "github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -36,7 +35,9 @@ const (
) )
var ( var (
ignoredDevices = kingpin.Flag("collector.diskstats.ignored-devices", "Regexp of devices to ignore for diskstats.").Default("^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$").String() diskstatsDeviceExclude = kingpin.Flag("collector.diskstats.device-exclude", "Regexp of diskstats devices to exclude (mutually exclusive to device-include).").String()
oldDiskstatsDeviceExclude = kingpin.Flag("collector.diskstats.ignored-devices", "DEPRECATED: Use collector.diskstats.device-exclude").String()
diskstatsDeviceInclude = kingpin.Flag("collector.diskstats.device-include", "Regexp of diskstats devices to include (mutually exclusive to device-exclude).").String()
) )
type typedFactorDesc struct { type typedFactorDesc struct {
@ -49,7 +50,7 @@ func (d *typedFactorDesc) mustNewConstMetric(value float64, labels ...string) pr
} }
type diskstatsCollector struct { type diskstatsCollector struct {
ignoredDevicesPattern *regexp.Regexp deviceFilter deviceFilter
fs blockdevice.FS fs blockdevice.FS
infoDesc typedFactorDesc infoDesc typedFactorDesc
descs []typedFactorDesc descs []typedFactorDesc
@ -68,9 +69,28 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to open sysfs: %w", err) return nil, fmt.Errorf("failed to open sysfs: %w", err)
} }
if *oldDiskstatsDeviceExclude != "" {
if *diskstatsDeviceExclude == "" {
level.Warn(logger).Log("msg", "--collector.diskstats.ignored-devices is DEPRECATED and will be removed in 2.0.0, use --collector.diskstats.device-exclude")
*diskstatsDeviceExclude = *oldDiskstatsDeviceExclude
} else {
return nil, errors.New("--collector.diskstats.ignored-devices and --collector.diskstats.device-exclude are mutually exclusive")
}
}
if *diskstatsDeviceExclude != "" && *diskstatsDeviceInclude != "" {
return nil, errors.New("device-exclude & device-include are mutually exclusive")
}
if *diskstatsDeviceExclude != "" {
level.Info(logger).Log("msg", "Parsed flag --collector.diskstats.device-exclude", "flag", *diskstatsDeviceExclude)
}
if *diskstatsDeviceInclude != "" {
level.Info(logger).Log("msg", "Parsed Flag --collector.diskstats.device-include", "flag", *diskstatsDeviceInclude)
}
return &diskstatsCollector{ return &diskstatsCollector{
ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices), deviceFilter: newDeviceFilter(*diskstatsDeviceExclude, *diskstatsDeviceInclude),
fs: fs, fs: fs,
infoDesc: typedFactorDesc{ infoDesc: typedFactorDesc{
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"), desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),
@ -194,11 +214,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
for _, stats := range diskStats { for _, stats := range diskStats {
dev := stats.DeviceName dev := stats.DeviceName
if c.ignoredDevicesPattern.MatchString(dev) { if c.deviceFilter.ignored(dev) {
level.Debug(c.logger).Log("msg", "Ignoring device", "device", dev, "pattern", c.ignoredDevicesPattern)
continue continue
} }
ch <- c.infoDesc.mustNewConstMetric(1.0, dev, fmt.Sprint(stats.MajorNumber), fmt.Sprint(stats.MinorNumber)) ch <- c.infoDesc.mustNewConstMetric(1.0, dev, fmt.Sprint(stats.MajorNumber), fmt.Sprint(stats.MinorNumber))
statCount := stats.IoStatsCount - 3 // Total diskstats record count, less MajorNumber, MinorNumber and DeviceName statCount := stats.IoStatsCount - 3 // Total diskstats record count, less MajorNumber, MinorNumber and DeviceName

View file

@ -49,7 +49,7 @@ func NewTestDiskStatsCollector(logger log.Logger) (prometheus.Collector, error)
func TestDiskStats(t *testing.T) { func TestDiskStats(t *testing.T) {
*sysPath = "fixtures/sys" *sysPath = "fixtures/sys"
*procPath = "fixtures/proc" *procPath = "fixtures/proc"
*ignoredDevices = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$" *diskstatsDeviceExclude = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"
testcase := `# HELP node_disk_discard_time_seconds_total This is the total number of seconds spent by all discards. testcase := `# HELP node_disk_discard_time_seconds_total This is the total number of seconds spent by all discards.
# TYPE node_disk_discard_time_seconds_total counter # TYPE node_disk_discard_time_seconds_total counter
node_disk_discard_time_seconds_total{device="sdb"} 11.13 node_disk_discard_time_seconds_total{device="sdb"} 11.13