From be0ce61cb8ac5a0fb4600ab608757e0d2c7ee2cb Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Tue, 23 May 2023 14:53:27 +0100 Subject: [PATCH 01/13] Add include and exclude kingpin flags, following example in systemd collector Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index fec2df4037..6b26e985e5 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -18,12 +18,14 @@ package collector import ( "errors" + "fmt" "os" "path/filepath" "regexp" "strconv" "strings" + "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" @@ -31,6 +33,17 @@ 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() + hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]") hwmonFilenameFormat = regexp.MustCompile(`^(?P[^0-9]+)(?P[0-9]*)?(_(?P.+))?$`) hwmonLabelDesc = []string{"chip", "sensor"} @@ -47,13 +60,25 @@ func init() { } type hwMonCollector struct { - logger log.Logger + hwmonIncludePattern *regexp.Regexp + hwmonExcludePattern *regexp.Regexp + logger log.Logger } // NewHwMonCollector returns a new Collector exposing /sys/class/hwmon stats // (similar to lm-sensors). func NewHwMonCollector(logger log.Logger) (Collector, error) { - return &hwMonCollector{logger}, nil + + 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, + }, nil } func cleanMetricName(name string) string { From 15d61375b9b7d7296927437a8e081683769c7e31 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Tue, 23 May 2023 14:58:26 +0100 Subject: [PATCH 02/13] Rename unitInclude* and unitExclude* variables to systemdInclude* and systemdExclude*, for consistency with other collectors Signed-off-by: Conall O'Brien --- collector/systemd_linux.go | 38 ++++++++++++++++----------------- collector/systemd_linux_test.go | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 31eb84d534..830e43ec20 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go @@ -42,15 +42,15 @@ const ( ) var ( - unitIncludeSet bool - unitInclude = kingpin.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error { - unitIncludeSet = true + systemdIncludeSet bool + systemdInclude = kingpin.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error { + systemdIncludeSet = true return nil }).String() oldUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String() - unitExcludeSet bool - unitExclude = kingpin.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").PreAction(func(c *kingpin.ParseContext) error { - unitExcludeSet = true + systemdExcludeSet bool + systemdExclude = kingpin.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").PreAction(func(c *kingpin.ParseContext) error { + systemdExcludeSet = true return nil }).String() oldUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String() @@ -75,8 +75,8 @@ type systemdCollector struct { socketCurrentConnectionsDesc *prometheus.Desc socketRefusedConnectionsDesc *prometheus.Desc systemdVersionDesc *prometheus.Desc - unitIncludePattern *regexp.Regexp - unitExcludePattern *regexp.Regexp + systemdIncludePattern *regexp.Regexp + systemdExcludePattern *regexp.Regexp logger log.Logger } @@ -134,25 +134,25 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) { "Detected systemd version", []string{"version"}, nil) if *oldUnitExclude != "" { - if !unitExcludeSet { + if !systemdExcludeSet { level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude") - *unitExclude = *oldUnitExclude + *systemdExclude = *oldUnitExclude } else { return nil, errors.New("--collector.systemd.unit-blacklist and --collector.systemd.unit-exclude are mutually exclusive") } } if *oldUnitInclude != "" { - if !unitIncludeSet { + if !systemdIncludeSet { level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include") - *unitInclude = *oldUnitInclude + *systemdInclude = *oldUnitInclude } else { return nil, errors.New("--collector.systemd.unit-whitelist and --collector.systemd.unit-include are mutually exclusive") } } - level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *unitInclude) - unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude)) - level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *unitExclude) - unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude)) + level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *systemdInclude) + systemdIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *systemdInclude)) + level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *systemdExclude) + systemdExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *systemdExclude)) return &systemdCollector{ unitDesc: unitDesc, @@ -167,8 +167,8 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) { socketCurrentConnectionsDesc: socketCurrentConnectionsDesc, socketRefusedConnectionsDesc: socketRefusedConnectionsDesc, systemdVersionDesc: systemdVersionDesc, - unitIncludePattern: unitIncludePattern, - unitExcludePattern: unitExcludePattern, + systemdIncludePattern: systemdIncludePattern, + systemdExcludePattern: systemdExcludePattern, logger: logger, }, nil } @@ -206,7 +206,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error { level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds()) begin = time.Now() - units := filterUnits(allUnits, c.unitIncludePattern, c.unitExcludePattern, c.logger) + units := filterUnits(allUnits, c.systemdIncludePattern, c.systemdExcludePattern, c.logger) level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds()) var wg sync.WaitGroup diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 1905f5d992..40f0d201c6 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go @@ -106,7 +106,7 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { } fixtures := getUnitListFixtures() collector := c.(*systemdCollector) - filtered := filterUnits(fixtures[0], collector.unitIncludePattern, collector.unitExcludePattern, logger) + filtered := filterUnits(fixtures[0], collector.systemdIncludePattern, collector.systemdExcludePattern, logger) // Adjust fixtures by 3 "not-found" units. if len(filtered) != len(fixtures[0])-3 { t.Error("Default filters removed units") From 7c49531c9c76b2bdd0fe6979d2b2c81a4e2ad5c7 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Wed, 24 May 2023 16:12:00 +0100 Subject: [PATCH 03/13] Revert "Rename unitInclude* and unitExclude* variables to systemdInclude* and" This reverts commit 15d61375b9b7d7296927437a8e081683769c7e31. Signed-off-by: Conall O'Brien --- collector/systemd_linux.go | 38 ++++++++++++++++----------------- collector/systemd_linux_test.go | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 830e43ec20..31eb84d534 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go @@ -42,15 +42,15 @@ const ( ) var ( - systemdIncludeSet bool - systemdInclude = kingpin.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error { - systemdIncludeSet = true + unitIncludeSet bool + unitInclude = kingpin.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error { + unitIncludeSet = true return nil }).String() oldUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String() - systemdExcludeSet bool - systemdExclude = kingpin.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").PreAction(func(c *kingpin.ParseContext) error { - systemdExcludeSet = true + unitExcludeSet bool + unitExclude = kingpin.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").PreAction(func(c *kingpin.ParseContext) error { + unitExcludeSet = true return nil }).String() oldUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String() @@ -75,8 +75,8 @@ type systemdCollector struct { socketCurrentConnectionsDesc *prometheus.Desc socketRefusedConnectionsDesc *prometheus.Desc systemdVersionDesc *prometheus.Desc - systemdIncludePattern *regexp.Regexp - systemdExcludePattern *regexp.Regexp + unitIncludePattern *regexp.Regexp + unitExcludePattern *regexp.Regexp logger log.Logger } @@ -134,25 +134,25 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) { "Detected systemd version", []string{"version"}, nil) if *oldUnitExclude != "" { - if !systemdExcludeSet { + if !unitExcludeSet { level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude") - *systemdExclude = *oldUnitExclude + *unitExclude = *oldUnitExclude } else { return nil, errors.New("--collector.systemd.unit-blacklist and --collector.systemd.unit-exclude are mutually exclusive") } } if *oldUnitInclude != "" { - if !systemdIncludeSet { + if !unitIncludeSet { level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include") - *systemdInclude = *oldUnitInclude + *unitInclude = *oldUnitInclude } else { return nil, errors.New("--collector.systemd.unit-whitelist and --collector.systemd.unit-include are mutually exclusive") } } - level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *systemdInclude) - systemdIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *systemdInclude)) - level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *systemdExclude) - systemdExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *systemdExclude)) + level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *unitInclude) + unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude)) + level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *unitExclude) + unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude)) return &systemdCollector{ unitDesc: unitDesc, @@ -167,8 +167,8 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) { socketCurrentConnectionsDesc: socketCurrentConnectionsDesc, socketRefusedConnectionsDesc: socketRefusedConnectionsDesc, systemdVersionDesc: systemdVersionDesc, - systemdIncludePattern: systemdIncludePattern, - systemdExcludePattern: systemdExcludePattern, + unitIncludePattern: unitIncludePattern, + unitExcludePattern: unitExcludePattern, logger: logger, }, nil } @@ -206,7 +206,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error { level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds()) begin = time.Now() - units := filterUnits(allUnits, c.systemdIncludePattern, c.systemdExcludePattern, c.logger) + units := filterUnits(allUnits, c.unitIncludePattern, c.unitExcludePattern, c.logger) level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds()) var wg sync.WaitGroup diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 40f0d201c6..1905f5d992 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go @@ -106,7 +106,7 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { } fixtures := getUnitListFixtures() collector := c.(*systemdCollector) - filtered := filterUnits(fixtures[0], collector.systemdIncludePattern, collector.systemdExcludePattern, logger) + filtered := filterUnits(fixtures[0], collector.unitIncludePattern, collector.unitExcludePattern, logger) // Adjust fixtures by 3 "not-found" units. if len(filtered) != len(fixtures[0])-3 { t.Error("Default filters removed units") From e7fa213f6edce4afe36b8059e38be1aa9ec61df5 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Wed, 24 May 2023 16:15:29 +0100 Subject: [PATCH 04/13] Run gofmt on hwmon_linux.go Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 6b26e985e5..1c7a257a48 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -35,12 +35,12 @@ 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 + 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 + hwmonExcludeSet = true return nil }).String() From a8bcad342d69f29d4e1d212187c483a4137f1f36 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Mon, 29 May 2023 17:05:10 +0100 Subject: [PATCH 05/13] Add checks against hwmonIncludePattern and hwmonExcludePattern, where @SuperQ suggested Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 1c7a257a48..2972742395 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -179,6 +179,14 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er return err } + if c.hwmonIncludePattern.MatchString(hwmonName) && !c.hwmonExcludePattern.MatchString(hwmonName) { + level.Debug(c.logger).Log("msg", "Adding device", "device", hwmonName) + return nil + } else { + level.Debug(c.logger).Log("msg", "Ignoring device", "device", hwmonName) + return err + } + data := make(map[string]map[string]string) err = collectSensorData(dir, data) if err != nil { From 731483c8f6af13788fdb2bf17ffc0a57d8ea9431 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Mon, 29 May 2023 17:10:04 +0100 Subject: [PATCH 06/13] Run gofmt Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 2972742395..cb9accdc6f 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -180,12 +180,12 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er } if c.hwmonIncludePattern.MatchString(hwmonName) && !c.hwmonExcludePattern.MatchString(hwmonName) { - level.Debug(c.logger).Log("msg", "Adding device", "device", hwmonName) + level.Debug(c.logger).Log("msg", "Adding device", "device", hwmonName) return nil - } else { - level.Debug(c.logger).Log("msg", "Ignoring device", "device", hwmonName) + } else { + level.Debug(c.logger).Log("msg", "Ignoring device", "device", hwmonName) return err - } + } data := make(map[string]map[string]string) err = collectSensorData(dir, data) From f8096b70f55534a8a1f5065584a4bd789b3bebcc Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Mon, 29 May 2023 17:13:58 +0100 Subject: [PATCH 07/13] Add checks against hwmonIncludePattern and hwmonExcludePattern, where @SuperQ suggested Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 1c7a257a48..cb9accdc6f 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -179,6 +179,14 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er return err } + if c.hwmonIncludePattern.MatchString(hwmonName) && !c.hwmonExcludePattern.MatchString(hwmonName) { + level.Debug(c.logger).Log("msg", "Adding device", "device", hwmonName) + return nil + } else { + level.Debug(c.logger).Log("msg", "Ignoring device", "device", hwmonName) + return err + } + data := make(map[string]map[string]string) err = collectSensorData(dir, data) if err != nil { From 9168508438c3acbdbc00fbfce2bedd21adaa50df Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Mon, 29 May 2023 18:50:33 +0100 Subject: [PATCH 08/13] Don't return anything when including a device. Return nil, not err when excluding a device Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index cb9accdc6f..4aba9debe1 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -181,10 +181,9 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er if c.hwmonIncludePattern.MatchString(hwmonName) && !c.hwmonExcludePattern.MatchString(hwmonName) { level.Debug(c.logger).Log("msg", "Adding device", "device", hwmonName) - return nil } else { level.Debug(c.logger).Log("msg", "Ignoring device", "device", hwmonName) - return err + return nil } data := make(map[string]map[string]string) From 75ba1d05ed21374c1222646de1d28933f41e6a68 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Thu, 6 Jul 2023 12:08:50 +0100 Subject: [PATCH 09/13] 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 } From e20df5506628835c5e621b5758f8ae6cda848eb2 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Thu, 6 Jul 2023 12:10:51 +0100 Subject: [PATCH 10/13] Remove superfluous regexp handling, device_filter.go handles this for us Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index ea59d2edd5..051bb3d8ed 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -164,13 +164,6 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er return err } - if c.hwmonIncludePattern.MatchString(hwmonName) && !c.hwmonExcludePattern.MatchString(hwmonName) { - level.Debug(c.logger).Log("msg", "Adding device", "device", hwmonName) - } else { - level.Debug(c.logger).Log("msg", "Ignoring device", "device", hwmonName) - return nil - } - data := make(map[string]map[string]string) err = collectSensorData(dir, data) if err != nil { From 71dc549bab397bcd4a79b889acdaa22dc03baf47 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Thu, 6 Jul 2023 12:35:32 +0100 Subject: [PATCH 11/13] Update collector/hwmon_linux.go Co-authored-by: Ben Kochie Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 1 - 1 file changed, 1 deletion(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 051bb3d8ed..7d5f00dfcd 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -18,7 +18,6 @@ package collector import ( "errors" - "fmt" "os" "path/filepath" "regexp" From af4790990148a7b0f288caab4566f2b250b395b5 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Thu, 6 Jul 2023 15:33:30 +0100 Subject: [PATCH 12/13] Update filtering variable names to be HWmon Chips, not units Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 051bb3d8ed..0325e31311 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -18,7 +18,6 @@ package collector import ( "errors" - "fmt" "os" "path/filepath" "regexp" @@ -33,8 +32,8 @@ import ( ) var ( - 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() + collectorHWmonChipInclude = kingpin.Flag("collector.hwmon.chip-include", "Regexp of hwmon chip to include (mutually exclusive to device-exclude).").String() + collectorHWmonChipExclude = kingpin.Flag("collector.hwmon.chip-exclude", "Regexp of hwmon chip to exclude (mutually exclusive to device-include).").String() hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]") hwmonFilenameFormat = regexp.MustCompile(`^(?P[^0-9]+)(?P[0-9]*)?(_(?P.+))?$`) @@ -62,7 +61,7 @@ func NewHwMonCollector(logger log.Logger) (Collector, error) { return &hwMonCollector{ logger: logger, - deviceFilter: newDeviceFilter(*collectorHWmonUnitExclude, *collectorHWmonUnitExclude), + deviceFilter: newDeviceFilter(*collectorHWmonChipExclude, *collectorHWmonChipExclude), }, nil } From 03e4a4de9e886866250b85c3d1bf93772e4d86c5 Mon Sep 17 00:00:00 2001 From: Conall O'Brien Date: Thu, 6 Jul 2023 22:53:47 +0100 Subject: [PATCH 13/13] Skip hwmon chip if it's marked as ignored by device_filter Signed-off-by: Conall O'Brien --- collector/hwmon_linux.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 0325e31311..1d06e89107 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -163,6 +163,10 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er return err } + if c.deviceFilter.ignored(hwmonName) { + return nil + } + data := make(map[string]map[string]string) err = collectSensorData(dir, data) if err != nil {