From e0c4a47a02639a77563b100021b56c867d75d3b5 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Sun, 14 Jul 2024 10:17:20 -0400 Subject: [PATCH] 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 --- README.md | 1 + collector/interrupts_common.go | 17 ++++++++++++++--- collector/interrupts_linux.go | 10 ++++++++++ collector/interrupts_openbsd.go | 13 ++++++++++++- collector/interrupts_openbsd_amd64.go | 13 ++++++++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 65199f88..afe496fd 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ filesystem | fs-types | N/A | --collector.filesystem.fs-types-exclude filesystem | mount-points | N/A | --collector.filesystem.mount-points-exclude hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-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 qdisk | device | --collector.qdisk.device-include | --collector.qdisk.device-exclude slabinfo | slab-names | --collector.slabinfo.slabs-include | --collector.slabinfo.slabs-exclude diff --git a/collector/interrupts_common.go b/collector/interrupts_common.go index eea703f9..a0996e91 100644 --- a/collector/interrupts_common.go +++ b/collector/interrupts_common.go @@ -18,19 +18,28 @@ package collector import ( + "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" ) type interruptsCollector struct { - desc typedDesc - logger log.Logger + desc typedDesc + logger log.Logger + nameFilter deviceFilter + includeZeros bool } func init() { 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. func NewInterruptsCollector(logger log.Logger) (Collector, error) { return &interruptsCollector{ @@ -39,6 +48,8 @@ func NewInterruptsCollector(logger log.Logger) (Collector, error) { "Interrupt details.", interruptLabelNames, nil, ), prometheus.CounterValue}, - logger: logger, + logger: logger, + nameFilter: newDeviceFilter(*interruptsExclude, *interruptsInclude), + includeZeros: *interruptsIncludeZeros, }, nil } diff --git a/collector/interrupts_linux.go b/collector/interrupts_linux.go index ede78191..3d3bd037 100644 --- a/collector/interrupts_linux.go +++ b/collector/interrupts_linux.go @@ -25,6 +25,7 @@ import ( "strconv" "strings" + "github.com/go-kit/log/level" "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 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) if err != nil { 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) } } diff --git a/collector/interrupts_openbsd.go b/collector/interrupts_openbsd.go index 9fa5b68d..049f6407 100644 --- a/collector/interrupts_openbsd.go +++ b/collector/interrupts_openbsd.go @@ -20,6 +20,7 @@ import ( "fmt" "strconv" + "github.com/go-kit/log/level" "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 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( value, strconv.Itoa(cpuNo), - strconv.Itoa(interrupt.vector), + interruptType, dev, ) } diff --git a/collector/interrupts_openbsd_amd64.go b/collector/interrupts_openbsd_amd64.go index ba1091c4..4ce4edfd 100644 --- a/collector/interrupts_openbsd_amd64.go +++ b/collector/interrupts_openbsd_amd64.go @@ -21,6 +21,7 @@ import ( "strconv" "unsafe" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "golang.org/x/sys/unix" ) @@ -77,10 +78,20 @@ func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) error { } for dev, interrupt := range interrupts { 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( value, strconv.Itoa(cpuNo), - fmt.Sprintf("%d", interrupt.vector), + interruptType, dev, ) }