Add interrupts collector filtering (#3028)

In order to reduce cardinality of the interrupts collector add
filtering options
* Add include/exclude regexp filter flags.
* Add boolean flag to include zero values, enabled by default.

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2024-07-14 10:17:20 -04:00 committed by GitHub
parent 2ce82ca8e8
commit e0c4a47a02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 49 additions and 5 deletions

View File

@ -103,6 +103,7 @@ filesystem | fs-types | N/A | --collector.filesystem.fs-types-exclude
filesystem | mount-points | N/A | --collector.filesystem.mount-points-exclude filesystem | mount-points | N/A | --collector.filesystem.mount-points-exclude
hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-exclude hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-exclude
hwmon | sensor | --collector.hwmon.sensor-include | --collector.hwmon.sensor-exclude hwmon | sensor | --collector.hwmon.sensor-include | --collector.hwmon.sensor-exclude
interrupts | name | --collector.interrupts.name-include | --collector.interrupts.name-exclude
netdev | device | --collector.netdev.device-include | --collector.netdev.device-exclude netdev | device | --collector.netdev.device-include | --collector.netdev.device-exclude
qdisk | device | --collector.qdisk.device-include | --collector.qdisk.device-exclude qdisk | device | --collector.qdisk.device-include | --collector.qdisk.device-exclude
slabinfo | slab-names | --collector.slabinfo.slabs-include | --collector.slabinfo.slabs-exclude slabinfo | slab-names | --collector.slabinfo.slabs-include | --collector.slabinfo.slabs-exclude

View File

@ -18,19 +18,28 @@
package collector package collector
import ( import (
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
type interruptsCollector struct { type interruptsCollector struct {
desc typedDesc desc typedDesc
logger log.Logger logger log.Logger
nameFilter deviceFilter
includeZeros bool
} }
func init() { func init() {
registerCollector("interrupts", defaultDisabled, NewInterruptsCollector) registerCollector("interrupts", defaultDisabled, NewInterruptsCollector)
} }
var (
interruptsInclude = kingpin.Flag("collector.interrupts.name-include", "Regexp of interrupts name to include (mutually exclusive to --collector.interrupts.name-exclude).").String()
interruptsExclude = kingpin.Flag("collector.interrupts.name-exclude", "Regexp of interrupts name to exclude (mutually exclusive to --collector.interrupts.name-include).").String()
interruptsIncludeZeros = kingpin.Flag("collector.interrupts.include-zeros", "Include interrupts that have a zero value").Default("true").Bool()
)
// NewInterruptsCollector returns a new Collector exposing interrupts stats. // NewInterruptsCollector returns a new Collector exposing interrupts stats.
func NewInterruptsCollector(logger log.Logger) (Collector, error) { func NewInterruptsCollector(logger log.Logger) (Collector, error) {
return &interruptsCollector{ return &interruptsCollector{
@ -39,6 +48,8 @@ func NewInterruptsCollector(logger log.Logger) (Collector, error) {
"Interrupt details.", "Interrupt details.",
interruptLabelNames, nil, interruptLabelNames, nil,
), prometheus.CounterValue}, ), prometheus.CounterValue},
logger: logger, logger: logger,
nameFilter: newDeviceFilter(*interruptsExclude, *interruptsInclude),
includeZeros: *interruptsIncludeZeros,
}, nil }, nil
} }

View File

@ -25,6 +25,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
@ -39,10 +40,19 @@ func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) (err error) {
} }
for name, interrupt := range interrupts { for name, interrupt := range interrupts {
for cpuNo, value := range interrupt.values { for cpuNo, value := range interrupt.values {
filterName := name + ";" + interrupt.info + ";" + interrupt.devices
if c.nameFilter.ignored(filterName) {
level.Debug(c.logger).Log("msg", "ignoring interrupt name", "filter_name", filterName)
continue
}
fv, err := strconv.ParseFloat(value, 64) fv, err := strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
return fmt.Errorf("invalid value %s in interrupts: %w", value, err) return fmt.Errorf("invalid value %s in interrupts: %w", value, err)
} }
if !c.includeZeros && fv == 0.0 {
level.Debug(c.logger).Log("msg", "ignoring interrupt with zero value", "filter_name", filterName, "cpu", cpuNo)
continue
}
ch <- c.desc.mustNewConstMetric(fv, strconv.Itoa(cpuNo), name, interrupt.info, interrupt.devices) ch <- c.desc.mustNewConstMetric(fv, strconv.Itoa(cpuNo), name, interrupt.info, interrupt.devices)
} }
} }

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
@ -106,10 +107,20 @@ func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) error {
} }
for dev, interrupt := range interrupts { for dev, interrupt := range interrupts {
for cpuNo, value := range interrupt.values { for cpuNo, value := range interrupt.values {
interruptType := fmt.Sprintf("%d", interrupt.vector)
filterName := interruptType + ";" + dev
if c.nameFilter.ignored(filterName) {
level.Debug(c.logger).Log("msg", "ignoring interrupt name", "filter_name", filterName)
continue
}
if !c.includeZeros && value == 0.0 {
level.Debug(c.logger).Log("msg", "ignoring interrupt with zero value", "filter_name", filterName, "cpu", cpuNo)
continue
}
ch <- c.desc.mustNewConstMetric( ch <- c.desc.mustNewConstMetric(
value, value,
strconv.Itoa(cpuNo), strconv.Itoa(cpuNo),
strconv.Itoa(interrupt.vector), interruptType,
dev, dev,
) )
} }

View File

@ -21,6 +21,7 @@ import (
"strconv" "strconv"
"unsafe" "unsafe"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -77,10 +78,20 @@ func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) error {
} }
for dev, interrupt := range interrupts { for dev, interrupt := range interrupts {
for cpuNo, value := range interrupt.values { for cpuNo, value := range interrupt.values {
interruptType := fmt.Sprintf("%d", interrupt.vector)
filterName := interruptType + ";" + dev
if c.nameFilter.ignored(filterName) {
level.Debug(c.logger).Log("msg", "ignoring interrupt name", "filter_name", filterName)
continue
}
if !c.includeZeros && value == 0.0 {
level.Debug(c.logger).Log("msg", "ignoring interrupt with zero value", "filter_name", filterName, "cpu", cpuNo)
continue
}
ch <- c.desc.mustNewConstMetric( ch <- c.desc.mustNewConstMetric(
value, value,
strconv.Itoa(cpuNo), strconv.Itoa(cpuNo),
fmt.Sprintf("%d", interrupt.vector), interruptType,
dev, dev,
) )
} }