From 4426962ec85142e3254de8ccbd57cb5d49a1d9c9 Mon Sep 17 00:00:00 2001 From: t-falconnet Date: Fri, 11 Feb 2022 15:06:39 +0100 Subject: [PATCH 1/5] ethtool_linux: add mutex around entries access Signed-off-by: t-falconnet --- collector/ethtool_linux.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index c68b3e9f..99995960 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -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] +} From 642f64b7015933973c39b3fc93d6a9313d47ad25 Mon Sep 17 00:00:00 2001 From: t-falconnet Date: Fri, 11 Feb 2022 15:58:10 +0100 Subject: [PATCH 2/5] ethtool_linux: fix entry function Signed-off-by: t-falconnet --- collector/ethtool_linux.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index 99995960..06c08795 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -422,7 +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 := c.entries(metric) + entry := c.entry(metric, metricFQName) ch <- prometheus.MustNewConstMetric( entry, prometheus.UntypedValue, float64(val), device) } @@ -431,14 +431,14 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error { return nil } -func (c *ethtoolCollector) entries(key string) *prometheus.Desc { +func (c *ethtoolCollector) entry(key, metricFQName 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), + fmt.Sprintf("Network interface %s", key), []string{"device"}, nil, ) } From b0708e4c47eec567d2b88ba745d85ca05dbeb331 Mon Sep 17 00:00:00 2001 From: t-falconnet Date: Fri, 11 Feb 2022 16:55:26 +0100 Subject: [PATCH 3/5] ethtool-linux: add remaining unlocked access to entries Signed-off-by: t-falconnet --- collector/ethtool_linux.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index 06c08795..95bca73d 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -235,9 +235,9 @@ func (c *ethtoolCollector) updatePortCapabilities(ch chan<- prometheus.Metric, p if linkModes&(1< Date: Fri, 11 Feb 2022 17:04:33 +0100 Subject: [PATCH 4/5] ethtool-linux: split between create and show entry Signed-off-by: t-falconnet --- collector/ethtool_linux.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index 95bca73d..b7b413fc 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -422,7 +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 := c.entry(metric, metricFQName) + entry := c.entryWithCreate(metric, metricFQName) ch <- prometheus.MustNewConstMetric( entry, prometheus.UntypedValue, float64(val), device) } @@ -431,7 +431,7 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error { return nil } -func (c *ethtoolCollector) entry(key, metricFQName string) *prometheus.Desc { +func (c *ethtoolCollector) entryWithCreate(key, metricFQName string) *prometheus.Desc { c.entriesMutex.Lock() defer c.entriesMutex.Unlock() @@ -445,3 +445,9 @@ func (c *ethtoolCollector) entry(key, metricFQName string) *prometheus.Desc { return c.entries[key] } + +func (c *ethtoolCollector) entry(key) *prometheus.Desc { + c.entriesMutex.Lock() + defer c.entriesMutex.Unlock() + return c.entries[key] +} From 5c8407b77252b0f0759f8f7e2747d50bc2209b73 Mon Sep 17 00:00:00 2001 From: t-falconnet Date: Fri, 11 Feb 2022 17:06:53 +0100 Subject: [PATCH 5/5] ethtool-linux: fix entry function Signed-off-by: t-falconnet --- collector/ethtool_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index b7b413fc..04a7260d 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -446,7 +446,7 @@ func (c *ethtoolCollector) entryWithCreate(key, metricFQName string) *prometheus return c.entries[key] } -func (c *ethtoolCollector) entry(key) *prometheus.Desc { +func (c *ethtoolCollector) entry(key string) *prometheus.Desc { c.entriesMutex.Lock() defer c.entriesMutex.Unlock() return c.entries[key]