Merge pull request #2417 from prometheus/superq/diskstats

Add diskstats include and exclude device flags
This commit is contained in:
Ben Kochie 2022-06-28 10:12:14 +02:00 committed by GitHub
commit 69a3f73a9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 25 deletions

View File

@ -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
}

View File

@ -24,6 +24,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
const diskstatsDefaultIgnoredDevices = ""
type typedDescFunc struct {
typedDesc
value func(stat *iostat.DriveStats) float64
@ -31,6 +33,8 @@ type typedDescFunc struct {
type diskstatsCollector struct {
descs []typedDescFunc
deviceFilter deviceFilter
logger log.Logger
}
@ -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,6 +192,8 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
},
},
},
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)

View File

@ -18,13 +18,9 @@ package collector
import (
"fmt"
"regexp"
"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 (
@ -33,10 +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 (
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()
diskstatsDefaultIgnoredDevices = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"
)
type typedFactorDesc struct {
@ -49,7 +43,7 @@ func (d *typedFactorDesc) mustNewConstMetric(value float64, labels ...string) pr
}
type diskstatsCollector struct {
ignoredDevicesPattern *regexp.Regexp
deviceFilter deviceFilter
fs blockdevice.FS
infoDesc typedFactorDesc
descs []typedFactorDesc
@ -69,8 +63,13 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
return nil, fmt.Errorf("failed to open sysfs: %w", err)
}
deviceFilter, err := newDiskstatsDeviceFilter(logger)
if err != nil {
return nil, fmt.Errorf("failed to parse device filter flags: %w", err)
}
return &diskstatsCollector{
ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices),
deviceFilter: deviceFilter,
fs: fs,
infoDesc: typedFactorDesc{
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),
@ -194,11 +193,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

View File

@ -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

View File

@ -17,6 +17,7 @@
package collector
import (
"fmt"
"unsafe"
"github.com/go-kit/log"
@ -30,12 +31,16 @@ import (
*/
import "C"
const diskstatsDefaultIgnoredDevices = ""
type diskstatsCollector struct {
rxfer typedDesc
rbytes typedDesc
wxfer typedDesc
wbytes typedDesc
time typedDesc
deviceFilter deviceFilter
logger log.Logger
}
@ -45,12 +50,19 @@ 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},
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)

View File

@ -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,6 +50,8 @@ type diskstatsCollector struct {
wxfer typedDesc
wbytes typedDesc
time typedDesc
deviceFilter deviceFilter
logger log.Logger
}
@ -56,12 +61,19 @@ 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},
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)