Add collector.ethtool.metrics-include

This adds a new flag --collector.ethtool.metrics-include to the ethtool
collector. Only metrics matching this regexp will be collected.

Signed-off-by: Johannes 'fish' Ziemke <github@freigeist.org>
This commit is contained in:
Johannes 'fish' Ziemke 2021-08-10 18:57:36 +02:00
parent 4356c09ebd
commit e6b5aaaff4

View file

@ -37,9 +37,10 @@ import (
) )
var ( var (
ethtoolIgnoredDevices = kingpin.Flag("collector.ethtool.ignored-devices", "Regexp of net devices to ignore for ethtool collector.").Default("^$").String() ethtoolIgnoredDevices = kingpin.Flag("collector.ethtool.ignored-devices", "Regexp of net devices to ignore for ethtool collector.").Default("^$").String()
receivedRegex = regexp.MustCompile(`_rx_`) ethtoolIncludedMetrics = kingpin.Flag("collector.ethtool.metrics-include", "Regexp of ethtool stats to include.").Default(".*").String()
transmittedRegex = regexp.MustCompile(`_tx_`) receivedRegex = regexp.MustCompile(`_rx_`)
transmittedRegex = regexp.MustCompile(`_tx_`)
) )
type EthtoolStats interface { type EthtoolStats interface {
@ -57,6 +58,7 @@ type ethtoolCollector struct {
fs sysfs.FS fs sysfs.FS
entries map[string]*prometheus.Desc entries map[string]*prometheus.Desc
ignoredDevicesPattern *regexp.Regexp ignoredDevicesPattern *regexp.Regexp
metricsPattern *regexp.Regexp
logger log.Logger logger log.Logger
stats EthtoolStats stats EthtoolStats
} }
@ -74,6 +76,7 @@ func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) {
return &ethtoolCollector{ return &ethtoolCollector{
fs: fs, fs: fs,
ignoredDevicesPattern: regexp.MustCompile(*ethtoolIgnoredDevices), ignoredDevicesPattern: regexp.MustCompile(*ethtoolIgnoredDevices),
metricsPattern: regexp.MustCompile(*ethtoolIncludedMetrics),
logger: logger, logger: logger,
stats: &ethtoolStats{}, stats: &ethtoolStats{},
entries: map[string]*prometheus.Desc{ entries: map[string]*prometheus.Desc{
@ -176,6 +179,9 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error {
sort.Strings(keys) sort.Strings(keys)
for _, metric := range keys { for _, metric := range keys {
if !c.metricsPattern.MatchString(metric) {
continue
}
val := stats[metric] val := stats[metric]
metricFQName := prometheus.BuildFQName(namespace, "ethtool", metric) metricFQName := prometheus.BuildFQName(namespace, "ethtool", metric)
metricFQName = receivedRegex.ReplaceAllString(metricFQName, "_received_") metricFQName = receivedRegex.ReplaceAllString(metricFQName, "_received_")