From 75ba1d05ed21374c1222646de1d28933f41e6a68 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Thu, 6 Jul 2023 12:08:50 +0100 Subject: [PATCH] Add hwmon device filtering, leveraging collector/device_filter.go, as demonstrated by https://github.com/prometheus/node_exporter/pull/2432 Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 4aba9debe1..ea59d2edd5 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -33,16 +33,8 @@ import ( ) var ( - hwmonIncludeSet bool - hwmonInclude = kingpin.Flag("collector.hwmon.unit-include", "Regexp of hwmon units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error { - hwmonIncludeSet = true - return nil - }).String() - hwmonExcludeSet bool - hwmonExclude = kingpin.Flag("collector.hwmon.unit-exclude", "Regexp of hwmon units to exclude. Units must both match include and not match exclude to be included.").Default("").PreAction(func(c *kingpin.ParseContext) error { - hwmonExcludeSet = true - return nil - }).String() + collectorHWmonUnitInclude = kingpin.Flag("collector.hwmon.unit-include", "Regexp of hwmon devices to include (mutually exclusive to device-exclude).").String() + collectorHWmonUnitExclude = kingpin.Flag("collector.hwmon.unit-exclude", "Regexp of hwmon devices to exclude (mutually exclusive to device-include).").String() hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]") hwmonFilenameFormat = regexp.MustCompile(`^(?P[^0-9]+)(?P[0-9]*)?(_(?P.+))?$`) @@ -60,24 +52,17 @@ func init() { } type hwMonCollector struct { - hwmonIncludePattern *regexp.Regexp - hwmonExcludePattern *regexp.Regexp - logger log.Logger + deviceFilter deviceFilter + logger log.Logger } // NewHwMonCollector returns a new Collector exposing /sys/class/hwmon stats // (similar to lm-sensors). func NewHwMonCollector(logger log.Logger) (Collector, error) { - level.Info(logger).Log("msg", "Parsed flag --collector.hwmon.unit-include", "flag", *hwmonInclude) - hwmonIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *hwmonInclude)) - level.Info(logger).Log("msg", "Parsed flag --collector.hwmon.unit-exclude", "flag", *hwmonExclude) - hwmonExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *hwmonExclude)) - return &hwMonCollector{ - hwmonIncludePattern: hwmonIncludePattern, - hwmonExcludePattern: hwmonExcludePattern, - logger: logger, + logger: logger, + deviceFilter: newDeviceFilter(*collectorHWmonUnitExclude, *collectorHWmonUnitExclude), }, nil }