From 698f566a062f2925ee7e5925b793b443f51145dc Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Tue, 23 May 2023 15:46:08 +0200 Subject: [PATCH] fix: gauge continuity (#52) --- stats/otel_measurement.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/stats/otel_measurement.go b/stats/otel_measurement.go index bcc15311..89df7d38 100644 --- a/stats/otel_measurement.go +++ b/stats/otel_measurement.go @@ -2,7 +2,7 @@ package stats import ( "context" - "sync" + "sync/atomic" "time" "go.opentelemetry.io/otel/attribute" @@ -38,8 +38,7 @@ func (c *otelCounter) Increment() { // otelGauge represents a gauge stat type otelGauge struct { *otelMeasurement - value interface{} - valueMu sync.Mutex + value atomic.Value } // Gauge records an absolute value for this stat. Only applies to GaugeType stats @@ -47,20 +46,14 @@ func (g *otelGauge) Gauge(value interface{}) { if g.disabled { return } - g.valueMu.Lock() - g.value = value - g.valueMu.Unlock() + g.value.Store(value) } func (g *otelGauge) getValue() interface{} { if g.disabled { return nil } - g.valueMu.Lock() - v := g.value - g.value = nil - g.valueMu.Unlock() - return v + return g.value.Load() } // otelTimer represents a timer stat