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 <superq@gmail.com>
This commit is contained in:
Ben Kochie 2022-06-28 08:10:40 +02:00
parent 8fcc6320a2
commit 02f5005ac8
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1
5 changed files with 91 additions and 34 deletions

View file

@ -18,7 +18,12 @@
package collector package collector
import ( import (
"errors"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
) )
const ( const (
@ -28,6 +33,10 @@ const (
var ( var (
diskLabelNames = []string{"device"} 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( readsCompletedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"), prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"),
"The total number of reads completed successfully.", "The total number of reads completed successfully.",
@ -72,3 +81,28 @@ var (
nil, 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
}

View file

@ -24,6 +24,8 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
const diskstatsDefaultIgnoredDevices = ""
type typedDescFunc struct { type typedDescFunc struct {
typedDesc typedDesc
value func(stat *iostat.DriveStats) float64 value func(stat *iostat.DriveStats) float64
@ -31,6 +33,8 @@ type typedDescFunc struct {
type diskstatsCollector struct { type diskstatsCollector struct {
descs []typedDescFunc descs []typedDescFunc
deviceFilter deviceFilter
logger log.Logger logger log.Logger
} }
@ -42,6 +46,11 @@ func init() {
func NewDiskstatsCollector(logger log.Logger) (Collector, error) { func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
var diskLabelNames = []string{"device"} 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{ return &diskstatsCollector{
descs: []typedDescFunc{ descs: []typedDescFunc{
{ {
@ -183,6 +192,8 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
}, },
}, },
}, },
deviceFilter: deviceFilter,
logger: logger, logger: logger,
}, nil }, nil
} }
@ -194,6 +205,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
} }
for _, stats := range diskStats { for _, stats := range diskStats {
if c.deviceFilter.ignored(stats.Name) {
continue
}
for _, desc := range c.descs { for _, desc := range c.descs {
v := desc.value(stats) v := desc.value(stats)
ch <- desc.mustNewConstMetric(v, stats.Name) ch <- desc.mustNewConstMetric(v, stats.Name)

View file

@ -17,13 +17,10 @@
package collector package collector
import ( import (
"errors"
"fmt" "fmt"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs/blockdevice" "github.com/prometheus/procfs/blockdevice"
"gopkg.in/alecthomas/kingpin.v2"
) )
const ( 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." // 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 // See also https://www.kernel.org/doc/Documentation/block/stat.txt
unixSectorSize = 512.0 unixSectorSize = 512.0
)
var ( diskstatsDefaultIgnoredDevices = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"
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 {
@ -69,28 +62,14 @@ 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 == "" { deviceFilter, err := newDiskstatsDeviceFilter(logger)
level.Warn(logger).Log("msg", "--collector.diskstats.ignored-devices is DEPRECATED and will be removed in 2.0.0, use --collector.diskstats.device-exclude") if err != nil {
*diskstatsDeviceExclude = *oldDiskstatsDeviceExclude return nil, fmt.Errorf("failed to parse device filter flags: %w", err)
} 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{
deviceFilter: newDeviceFilter(*diskstatsDeviceExclude, *diskstatsDeviceInclude), deviceFilter: deviceFilter,
fs: fs, fs: fs,
infoDesc: typedFactorDesc{ infoDesc: typedFactorDesc{
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"), desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),

View file

@ -17,6 +17,7 @@
package collector package collector
import ( import (
"fmt"
"unsafe" "unsafe"
"github.com/go-kit/log" "github.com/go-kit/log"
@ -30,12 +31,16 @@ import (
*/ */
import "C" import "C"
const diskstatsDefaultIgnoredDevices = ""
type diskstatsCollector struct { type diskstatsCollector struct {
rxfer typedDesc rxfer typedDesc
rbytes typedDesc rbytes typedDesc
wxfer typedDesc wxfer typedDesc
wbytes typedDesc wbytes typedDesc
time typedDesc time typedDesc
deviceFilter deviceFilter
logger log.Logger logger log.Logger
} }
@ -45,12 +50,19 @@ func init() {
// NewDiskstatsCollector returns a new Collector exposing disk device stats. // NewDiskstatsCollector returns a new Collector exposing disk device stats.
func NewDiskstatsCollector(logger log.Logger) (Collector, error) { 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{ return &diskstatsCollector{
rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue}, rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue},
rbytes: typedDesc{readBytesDesc, prometheus.CounterValue}, rbytes: typedDesc{readBytesDesc, prometheus.CounterValue},
wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue}, wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue},
wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue}, wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue},
time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue}, time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue},
deviceFilter: deviceFilter,
logger: logger, logger: logger,
}, nil }, nil
} }
@ -66,6 +78,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) {
for i := 0; i < ndisks; i++ { for i := 0; i < ndisks; i++ {
diskname := C.GoString(&diskstats[i].ds_name[0]) 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.rxfer.mustNewConstMetric(float64(diskstats[i].ds_rxfer), diskname)
ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].ds_rbytes), diskname) ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].ds_rbytes), diskname)

View file

@ -17,6 +17,7 @@
package collector package collector
import ( import (
"fmt"
"unsafe" "unsafe"
"github.com/go-kit/log" "github.com/go-kit/log"
@ -26,6 +27,8 @@ import (
const ( const (
DS_DISKNAMELEN = 16 DS_DISKNAMELEN = 16
diskstatsDefaultIgnoredDevices = ""
) )
type DiskStats struct { type DiskStats struct {
@ -47,6 +50,8 @@ type diskstatsCollector struct {
wxfer typedDesc wxfer typedDesc
wbytes typedDesc wbytes typedDesc
time typedDesc time typedDesc
deviceFilter deviceFilter
logger log.Logger logger log.Logger
} }
@ -56,12 +61,19 @@ func init() {
// NewDiskstatsCollector returns a new Collector exposing disk device stats. // NewDiskstatsCollector returns a new Collector exposing disk device stats.
func NewDiskstatsCollector(logger log.Logger) (Collector, error) { 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{ return &diskstatsCollector{
rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue}, rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue},
rbytes: typedDesc{readBytesDesc, prometheus.CounterValue}, rbytes: typedDesc{readBytesDesc, prometheus.CounterValue},
wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue}, wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue},
wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue}, wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue},
time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue}, time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue},
deviceFilter: deviceFilter,
logger: logger, logger: logger,
}, nil }, nil
} }
@ -78,6 +90,9 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) {
for i := 0; i < ndisks; i++ { for i := 0; i < ndisks; i++ {
dn := *(*[DS_DISKNAMELEN]int8)(unsafe.Pointer(&diskstats[i].Name[0])) dn := *(*[DS_DISKNAMELEN]int8)(unsafe.Pointer(&diskstats[i].Name[0]))
diskname := int8ToString(dn[:]) diskname := int8ToString(dn[:])
if c.deviceFilter.ignored(diskname) {
continue
}
ch <- c.rxfer.mustNewConstMetric(float64(diskstats[i].Rxfer), diskname) ch <- c.rxfer.mustNewConstMetric(float64(diskstats[i].Rxfer), diskname)
ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].Rbytes), diskname) ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].Rbytes), diskname)