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)