Skip to content

Commit

Permalink
Add locking for running average metric.
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Littley <[email protected]>
  • Loading branch information
cody-littley committed Nov 27, 2024
1 parent 2b84f21 commit fb0cad5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions common/metrics/running_average_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit fb0cad5

Please sign in to comment.