Skip to content

Commit

Permalink
Enhanced Gauge API (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgkanani committed Jun 29, 2019
1 parent f266f90 commit 732f603
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
8 changes: 8 additions & 0 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 732f603

Please sign in to comment.