From fb0cad5b58bb1e511cec7a86b94313a7b15ce7c4 Mon Sep 17 00:00:00 2001 From: Cody Littley Date: Wed, 27 Nov 2024 11:11:24 -0600 Subject: [PATCH] Add locking for running average metric. Signed-off-by: Cody Littley --- common/metrics/running_average_metric.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/common/metrics/running_average_metric.go b/common/metrics/running_average_metric.go index 8456644e0..f0e263e92 100644 --- a/common/metrics/running_average_metric.go +++ b/common/metrics/running_average_metric.go @@ -5,6 +5,7 @@ import ( "github.com/Layr-Labs/eigensdk-go/logging" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "sync" "time" ) @@ -35,6 +36,9 @@ type runningAverageMetric struct { // timeWindow is the time window used to calculate the running average. timeWindow time.Duration + + // lock is used to provide thread safety for the running average calculator. + lock sync.Mutex } // newRunningAverageMetric creates a new RunningAverageMetric instance. @@ -104,7 +108,9 @@ func (m *runningAverageMetric) Update(value float64, label ...any) { m.logger.Errorf("error extracting values from label: %v", err) } + m.lock.Lock() average := m.runningAverage.Update(time.Now(), value) + m.lock.Unlock() m.vec.WithLabelValues(values...).Set(average) }