Add ifAlias label to netdev collector (#3087)
Signed-off-by: Tomas Vilemaitis <tomas.vilemaitis@gmail.com>
This commit is contained in:
parent
f63b64ec63
commit
041d67d269
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue