From 94a00344c6af825807bd1606fcd4bc5a5640e0a5 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 19 Apr 2024 23:15:54 +0800 Subject: [PATCH] fix: data race of NetClassCollector metrics initialization when multiple requests happen (#2995) Signed-off-by: John Guo Signed-off-by: Vitaly Zhuravlev --- collector/netclass_linux.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/collector/netclass_linux.go b/collector/netclass_linux.go index 5d9324cdee..327c89aaf4 100644 --- a/collector/netclass_linux.go +++ b/collector/netclass_linux.go @@ -22,6 +22,7 @@ import ( "net" "os" "regexp" + "sync" "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" @@ -41,6 +42,7 @@ type netClassCollector struct { subsystem string ignoredDevicesPattern *regexp.Regexp metricDescs map[string]*prometheus.Desc + metricDescsMu sync.Mutex logger log.Logger } @@ -136,6 +138,9 @@ func (c *netClassCollector) netClassSysfsUpdate(ch chan<- prometheus.Metric) err } func (c *netClassCollector) getFieldDesc(name string) *prometheus.Desc { + c.metricDescsMu.Lock() + defer c.metricDescsMu.Unlock() + fieldDesc, exists := c.metricDescs[name] if !exists {