From 65711ac33659f8da3ef31065df745a90815b7c71 Mon Sep 17 00:00:00 2001 From: Mahendra Kanani Date: Sat, 29 Jun 2019 13:43:53 +0530 Subject: [PATCH] Enhanced Gauge API (#106) --- stats.go | 9 +++++++++ stats_test.go | 8 ++++++++ types.go | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/stats.go b/stats.go index aba2f9e8..328c0448 100644 --- a/stats.go +++ b/stats.go @@ -118,6 +118,15 @@ func (g *gauge) Update(v float64) { atomic.StoreUint64(&g.updated, 1) } +func (g *gauge) Add(v float64) { + v += math.Float64frombits(atomic.LoadUint64(&g.curr)) + g.Update(v) +} + +func (g *gauge) Sub(v float64) { + g.Add(-v) +} + func (g *gauge) value() float64 { return math.Float64frombits(atomic.LoadUint64(&g.curr)) } diff --git a/stats_test.go b/stats_test.go index 96142fe4..ce87868a 100644 --- a/stats_test.go +++ b/stats_test.go @@ -108,6 +108,14 @@ func TestGauge(t *testing.T) { gauge.report("", nil, r) assert.Equal(t, float64(42), r.last) + gauge.Add(4.2) + gauge.report("", nil, r) + assert.Equal(t, float64(46.2), r.last) + + gauge.Sub(-4) + gauge.report("", nil, r) + assert.Equal(t, float64(50.2), r.last) + gauge.Update(1234) gauge.Update(5678) gauge.report("", nil, r) diff --git a/types.go b/types.go index 14105c68..ec5b730b 100644 --- a/types.go +++ b/types.go @@ -69,6 +69,12 @@ type Counter interface { type Gauge interface { // Update sets the gauges absolute value. Update(value float64) + + // Add updates gauges absolute value by adding 'value' to current value + Add(value float64) + + // Sub updates gauges absolute value by subtracting 'value' from current value + Sub(value float64) } // Timer is the interface for emitting timer metrics.