From 8fcc6320a2316d5d179b2a9a4a78d7281891c44d Mon Sep 17 00:00:00 2001 From: rushilenekar20 Date: Tue, 7 Jun 2022 01:17:31 -0500 Subject: [PATCH 1/2] 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 Co-authored-by: rushilenekar20 --- collector/diskstats_linux.go | 44 ++++++++++++++++++++++--------- collector/diskstats_linux_test.go | 2 +- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index f0de4077..8f25b587 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -17,9 +17,8 @@ package collector import ( + "errors" "fmt" - "regexp" - "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" @@ -36,7 +35,9 @@ const ( ) 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 { @@ -49,11 +50,11 @@ func (d *typedFactorDesc) mustNewConstMetric(value float64, labels ...string) pr } type diskstatsCollector struct { - ignoredDevicesPattern *regexp.Regexp - fs blockdevice.FS - infoDesc typedFactorDesc - descs []typedFactorDesc - logger log.Logger + deviceFilter deviceFilter + fs blockdevice.FS + infoDesc typedFactorDesc + descs []typedFactorDesc + logger log.Logger } func init() { @@ -68,10 +69,29 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { if err != nil { 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{ - ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices), - fs: fs, + deviceFilter: newDeviceFilter(*diskstatsDeviceExclude, *diskstatsDeviceInclude), + fs: fs, infoDesc: typedFactorDesc{ desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"), "Info of /sys/block/.", @@ -194,11 +214,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { for _, stats := range diskStats { dev := stats.DeviceName - if c.ignoredDevicesPattern.MatchString(dev) { - level.Debug(c.logger).Log("msg", "Ignoring device", "device", dev, "pattern", c.ignoredDevicesPattern) + if c.deviceFilter.ignored(dev) { continue } - 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 diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 0e8dc84f..4aeb025e 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -49,7 +49,7 @@ func NewTestDiskStatsCollector(logger log.Logger) (prometheus.Collector, error) func TestDiskStats(t *testing.T) { *sysPath = "fixtures/sys" *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. # TYPE node_disk_discard_time_seconds_total counter node_disk_discard_time_seconds_total{device="sdb"} 11.13 From 02f5005ac87181a7ce6844a0fadca6d72cc74171 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Tue, 28 Jun 2022 08:10:40 +0200 Subject: [PATCH 2/2] Add diskstat include/exclude flag to all platforms Refactor diskstats collector include/exclude to work on all platforms. * Fix up default ignored devices. Signed-off-by: Ben Kochie --- collector/diskstats_common.go | 34 ++++++++++++++++++++++++++++ collector/diskstats_darwin.go | 20 +++++++++++++--- collector/diskstats_linux.go | 33 +++++---------------------- collector/diskstats_openbsd.go | 19 ++++++++++++++-- collector/diskstats_openbsd_amd64.go | 19 ++++++++++++++-- 5 files changed, 91 insertions(+), 34 deletions(-) diff --git a/collector/diskstats_common.go b/collector/diskstats_common.go index 0f6bbe46..6d1f3361 100644 --- a/collector/diskstats_common.go +++ b/collector/diskstats_common.go @@ -18,7 +18,12 @@ package collector import ( + "errors" + + "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" + "gopkg.in/alecthomas/kingpin.v2" ) const ( @@ -28,6 +33,10 @@ const ( var ( diskLabelNames = []string{"device"} + diskstatsDeviceExclude = kingpin.Flag("collector.diskstats.device-exclude", "Regexp of diskstats devices to exclude (mutually exclusive to device-include).").Default(diskstatsDefaultIgnoredDevices).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() + readsCompletedDesc = prometheus.NewDesc( prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"), "The total number of reads completed successfully.", @@ -72,3 +81,28 @@ var ( nil, ) ) + +func newDiskstatsDeviceFilter(logger log.Logger) (deviceFilter, error) { + 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 deviceFilter{}, errors.New("--collector.diskstats.ignored-devices and --collector.diskstats.device-exclude are mutually exclusive") + } + } + + if *diskstatsDeviceExclude != "" && *diskstatsDeviceInclude != "" { + return deviceFilter{}, 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 newDeviceFilter(*diskstatsDeviceExclude, *diskstatsDeviceInclude), nil +} diff --git a/collector/diskstats_darwin.go b/collector/diskstats_darwin.go index 350c44f1..b5f6c538 100644 --- a/collector/diskstats_darwin.go +++ b/collector/diskstats_darwin.go @@ -24,14 +24,18 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +const diskstatsDefaultIgnoredDevices = "" + type typedDescFunc struct { typedDesc value func(stat *iostat.DriveStats) float64 } type diskstatsCollector struct { - descs []typedDescFunc - logger log.Logger + descs []typedDescFunc + + deviceFilter deviceFilter + logger log.Logger } func init() { @@ -42,6 +46,11 @@ func init() { func NewDiskstatsCollector(logger log.Logger) (Collector, error) { var diskLabelNames = []string{"device"} + deviceFilter, err := newDiskstatsDeviceFilter(logger) + if err != nil { + return nil, fmt.Errorf("failed to parse device filter flags: %w", err) + } + return &diskstatsCollector{ descs: []typedDescFunc{ { @@ -183,7 +192,9 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { }, }, }, - logger: logger, + + deviceFilter: deviceFilter, + logger: logger, }, nil } @@ -194,6 +205,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } for _, stats := range diskStats { + if c.deviceFilter.ignored(stats.Name) { + continue + } for _, desc := range c.descs { v := desc.value(stats) ch <- desc.mustNewConstMetric(v, stats.Name) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 8f25b587..5daeb1d7 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -17,13 +17,10 @@ package collector import ( - "errors" "fmt" "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/blockdevice" - "gopkg.in/alecthomas/kingpin.v2" ) const ( @@ -32,12 +29,8 @@ const ( // Read sectors and write sectors are the "standard UNIX 512-byte sectors, not any device- or filesystem-specific block size." // See also https://www.kernel.org/doc/Documentation/block/stat.txt unixSectorSize = 512.0 -) -var ( - 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() + diskstatsDefaultIgnoredDevices = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$" ) type typedFactorDesc struct { @@ -69,28 +62,14 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { if err != nil { 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") - } + + deviceFilter, err := newDiskstatsDeviceFilter(logger) + if err != nil { + return nil, fmt.Errorf("failed to parse device filter flags: %w", err) } - 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{ - deviceFilter: newDeviceFilter(*diskstatsDeviceExclude, *diskstatsDeviceInclude), + deviceFilter: deviceFilter, fs: fs, infoDesc: typedFactorDesc{ desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"), diff --git a/collector/diskstats_openbsd.go b/collector/diskstats_openbsd.go index dd4f0b03..46d9bc1a 100644 --- a/collector/diskstats_openbsd.go +++ b/collector/diskstats_openbsd.go @@ -17,6 +17,7 @@ package collector import ( + "fmt" "unsafe" "github.com/go-kit/log" @@ -30,13 +31,17 @@ import ( */ import "C" +const diskstatsDefaultIgnoredDevices = "" + type diskstatsCollector struct { rxfer typedDesc rbytes typedDesc wxfer typedDesc wbytes typedDesc time typedDesc - logger log.Logger + + deviceFilter deviceFilter + logger log.Logger } func init() { @@ -45,13 +50,20 @@ func init() { // NewDiskstatsCollector returns a new Collector exposing disk device stats. func NewDiskstatsCollector(logger log.Logger) (Collector, error) { + deviceFilter, err := newDiskstatsDeviceFilter(logger) + if err != nil { + return nil, fmt.Errorf("failed to parse device filter flags: %w", err) + } + return &diskstatsCollector{ rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue}, rbytes: typedDesc{readBytesDesc, prometheus.CounterValue}, wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue}, wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue}, time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue}, - logger: logger, + + deviceFilter: deviceFilter, + logger: logger, }, nil } @@ -66,6 +78,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) { for i := 0; i < ndisks; i++ { diskname := C.GoString(&diskstats[i].ds_name[0]) + if c.deviceFilter.ignored(diskname) { + continue + } ch <- c.rxfer.mustNewConstMetric(float64(diskstats[i].ds_rxfer), diskname) ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].ds_rbytes), diskname) diff --git a/collector/diskstats_openbsd_amd64.go b/collector/diskstats_openbsd_amd64.go index 41265e23..a39cabb5 100644 --- a/collector/diskstats_openbsd_amd64.go +++ b/collector/diskstats_openbsd_amd64.go @@ -17,6 +17,7 @@ package collector import ( + "fmt" "unsafe" "github.com/go-kit/log" @@ -26,6 +27,8 @@ import ( const ( DS_DISKNAMELEN = 16 + + diskstatsDefaultIgnoredDevices = "" ) type DiskStats struct { @@ -47,7 +50,9 @@ type diskstatsCollector struct { wxfer typedDesc wbytes typedDesc time typedDesc - logger log.Logger + + deviceFilter deviceFilter + logger log.Logger } func init() { @@ -56,13 +61,20 @@ func init() { // NewDiskstatsCollector returns a new Collector exposing disk device stats. func NewDiskstatsCollector(logger log.Logger) (Collector, error) { + deviceFilter, err := newDiskstatsDeviceFilter(logger) + if err != nil { + return nil, fmt.Errorf("failed to parse device filter flags: %w", err) + } + return &diskstatsCollector{ rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue}, rbytes: typedDesc{readBytesDesc, prometheus.CounterValue}, wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue}, wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue}, time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue}, - logger: logger, + + deviceFilter: deviceFilter, + logger: logger, }, nil } @@ -78,6 +90,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) { for i := 0; i < ndisks; i++ { dn := *(*[DS_DISKNAMELEN]int8)(unsafe.Pointer(&diskstats[i].Name[0])) diskname := int8ToString(dn[:]) + if c.deviceFilter.ignored(diskname) { + continue + } ch <- c.rxfer.mustNewConstMetric(float64(diskstats[i].Rxfer), diskname) ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].Rbytes), diskname)