From 041d67d26949e20819e9622f017d3785607a6f24 Mon Sep 17 00:00:00 2001 From: tomvil Date: Wed, 11 Sep 2024 10:26:34 +0300 Subject: [PATCH] Add ifAlias label to netdev collector (#3087) Signed-off-by: Tomas Vilemaitis --- collector/netdev_common.go | 24 ++++++++++++++++++++---- collector/netdev_darwin.go | 5 +++++ collector/netdev_linux.go | 27 ++++++++++++++++++++++++++- collector/netdev_openbsd.go | 5 +++++ collector/netdev_openbsd_amd64.go | 8 +++++++- 5 files changed, 63 insertions(+), 6 deletions(-) diff --git a/collector/netdev_common.go b/collector/netdev_common.go index 089f1e58..c6abd7ac 100644 --- a/collector/netdev_common.go +++ b/collector/netdev_common.go @@ -93,7 +93,7 @@ func NewNetDevCollector(logger log.Logger) (Collector, error) { }, nil } -func (c *netDevCollector) metricDesc(key string) *prometheus.Desc { +func (c *netDevCollector) metricDesc(key string, labels []string) *prometheus.Desc { c.metricDescsMutex.Lock() defer c.metricDescsMutex.Unlock() @@ -101,7 +101,7 @@ func (c *netDevCollector) metricDesc(key string) *prometheus.Desc { c.metricDescs[key] = prometheus.NewDesc( prometheus.BuildFQName(namespace, c.subsystem, key+"_total"), fmt.Sprintf("Network device statistic %s.", key), - []string{"device"}, + labels, nil, ) } @@ -114,13 +114,29 @@ func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error { if err != nil { return fmt.Errorf("couldn't get netstats: %w", err) } + + netDevLabels, err := getNetDevLabels() + if err != nil { + return fmt.Errorf("couldn't get netdev labels: %w", err) + } + for dev, devStats := range netDev { if !*netdevDetailedMetrics { legacy(devStats) } + + labels := []string{"device"} + labelValues := []string{dev} + if devLabels, exists := netDevLabels[dev]; exists { + for labelName, labelValue := range devLabels { + labels = append(labels, labelName) + labelValues = append(labelValues, labelValue) + } + } + for key, value := range devStats { - desc := c.metricDesc(key) - ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(value), dev) + desc := c.metricDesc(key, labels) + ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(value), labelValues...) } } if *netdevAddressInfo { diff --git a/collector/netdev_darwin.go b/collector/netdev_darwin.go index c08f1f8e..e3d50e26 100644 --- a/collector/netdev_darwin.go +++ b/collector/netdev_darwin.go @@ -118,3 +118,8 @@ type ifData64 struct { Xmittiming uint32 Lastchange unix.Timeval32 } + +func getNetDevLabels() (map[string]map[string]string, error) { + // to be implemented if needed + return nil, nil +} diff --git a/collector/netdev_linux.go b/collector/netdev_linux.go index f3348cda..0767c706 100644 --- a/collector/netdev_linux.go +++ b/collector/netdev_linux.go @@ -24,10 +24,12 @@ import ( "github.com/go-kit/log/level" "github.com/jsimonetti/rtnetlink" "github.com/prometheus/procfs" + "github.com/prometheus/procfs/sysfs" ) var ( - netDevNetlink = kingpin.Flag("collector.netdev.netlink", "Use netlink to gather stats instead of /proc/net/dev.").Default("true").Bool() + netDevNetlink = kingpin.Flag("collector.netdev.netlink", "Use netlink to gather stats instead of /proc/net/dev.").Default("true").Bool() + netdevLabelIfAlias = kingpin.Flag("collector.netdev.label-ifalias", "Add ifAlias label").Default("false").Bool() ) func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) { @@ -184,3 +186,26 @@ func procNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, erro return metrics, nil } + +func getNetDevLabels() (map[string]map[string]string, error) { + if !*netdevLabelIfAlias { + return nil, nil + } + + fs, err := sysfs.NewFS(*sysPath) + if err != nil { + return nil, err + } + + interfaces, err := fs.NetClass() + if err != nil { + return nil, err + } + + labels := make(map[string]map[string]string) + for iface, params := range interfaces { + labels[iface] = map[string]string{"ifalias": params.IfAlias} + } + + return labels, nil +} diff --git a/collector/netdev_openbsd.go b/collector/netdev_openbsd.go index b90e3ba7..177309fd 100644 --- a/collector/netdev_openbsd.go +++ b/collector/netdev_openbsd.go @@ -72,3 +72,8 @@ func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error return netDev, nil } + +func getNetDevLabels() (map[string]map[string]string, error) { + // to be implemented if needed + return nil, nil +} diff --git a/collector/netdev_openbsd_amd64.go b/collector/netdev_openbsd_amd64.go index da8a81f3..25702c12 100644 --- a/collector/netdev_openbsd_amd64.go +++ b/collector/netdev_openbsd_amd64.go @@ -20,8 +20,9 @@ import ( "github.com/go-kit/log" "github.com/go-kit/log/level" - "golang.org/x/sys/unix" "unsafe" + + "golang.org/x/sys/unix" ) func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) { @@ -76,3 +77,8 @@ func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error } return netDev, nil } + +func getNetDevLabels() (map[string]map[string]string, error) { + // to be implemented if needed + return nil, nil +}