Skip to content

Commit

Permalink
Add ifAlias label to netdev collector (#3087)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Vilemaitis <[email protected]>
  • Loading branch information
tomvil authored Sep 11, 2024
1 parent f63b64e commit 041d67d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
24 changes: 20 additions & 4 deletions collector/netdev_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ 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()

if _, ok := c.metricDescs[key]; !ok {
c.metricDescs[key] = prometheus.NewDesc(
prometheus.BuildFQName(namespace, c.subsystem, key+"_total"),
fmt.Sprintf("Network device statistic %s.", key),
[]string{"device"},
labels,
nil,
)
}
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions collector/netdev_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 26 additions & 1 deletion collector/netdev_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions collector/netdev_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion collector/netdev_openbsd_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 041d67d

Please sign in to comment.