ethtool_linux: add mutex around entries access

Signed-off-by: t-falconnet <tfalconnet.externe@bedrockstreaming.com>
This commit is contained in:
t-falconnet 2022-02-11 15:06:39 +01:00
parent f7086d437b
commit 4426962ec8
1 changed files with 18 additions and 9 deletions

View File

@ -27,6 +27,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
"syscall"
"github.com/go-kit/log"
@ -73,6 +74,7 @@ func (e *ethtoolLibrary) LinkInfo(intf string) (ethtool.EthtoolCmd, error) {
type ethtoolCollector struct {
fs sysfs.FS
entries map[string]*prometheus.Desc
entriesMutex sync.Mutex
ethtool Ethtool
deviceFilter netDevFilter
infoDesc *prometheus.Desc
@ -420,15 +422,7 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error {
val := stats[metric]
// Check to see if this metric exists; if not then create it and store it in c.entries.
entry, exists := c.entries[metric]
if !exists {
entry = prometheus.NewDesc(
metricFQName,
fmt.Sprintf("Network interface %s", metric),
[]string{"device"}, nil,
)
c.entries[metric] = entry
}
entry := c.entries(metric)
ch <- prometheus.MustNewConstMetric(
entry, prometheus.UntypedValue, float64(val), device)
}
@ -436,3 +430,18 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error {
return nil
}
func (c *ethtoolCollector) entries(key string) *prometheus.Desc {
c.entriesMutex.Lock()
defer c.entriesMutex.Unlock()
if _, ok := c.entries[key]; !ok {
c.entries[key] = prometheus.NewDesc(
metricFQName,
fmt.Sprintf("Network interface %s", metric),
[]string{"device"}, nil,
)
}
return c.entries[key]
}