Skip to content

Commit 1d5554b

Browse files
author
Mike Heffner
authored
Report gauges once per interval (#274)
1 parent cbdc6a7 commit 1d5554b

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

metriks/gauge.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ const (
1515
)
1616

1717
// PersistentGauge will report on an interval the value to the metrics collector.
18-
// Every call to the methods to modify the value immediately report, but if we
19-
// don't have a change inside the window (default 10s) after the last report
20-
// we will report the current value.
18+
//
2119
type PersistentGauge struct {
2220
name string
2321
value int32
@@ -31,24 +29,17 @@ type PersistentGauge struct {
3129

3230
// Set will replace the value with a new one, it returns the old value
3331
func (g *PersistentGauge) Set(value int32) int32 {
34-
v := atomic.SwapInt32(&g.value, value)
35-
g.report(value)
36-
37-
return v
32+
return atomic.SwapInt32(&g.value, value)
3833
}
3934

4035
// Inc will +1 to the current value and return the new value
4136
func (g *PersistentGauge) Inc() int32 {
42-
v := atomic.AddInt32(&g.value, 1)
43-
g.report(v)
44-
return v
37+
return atomic.AddInt32(&g.value, 1)
4538
}
4639

4740
// Dec will -1 to the current value and return the new value
4841
func (g *PersistentGauge) Dec() int32 {
49-
v := atomic.AddInt32(&g.value, -1)
50-
g.report(v)
51-
return v
42+
return atomic.AddInt32(&g.value, -1)
5243
}
5344

5445
func (g *PersistentGauge) report(v int32) {

metriks/gauge_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ func TestPersistentGauge(t *testing.T) {
2323
assert.EqualValues(t, 0, g.Set(10))
2424

2525
expectedValues := []string{
26-
// these values should be reported everytime we make a call
27-
"test.some_gauge.b:1.000000|g",
28-
"test.some_gauge.b:0.000000|g",
29-
"test.some_gauge.b:10.000000|g",
3026
// this value should be reported after an interval
3127
"test.some_gauge.b:10.000000|g",
3228
}
3329

34-
for i := 0; i < 4; i++ {
30+
for i := 0; i < len(expectedValues); i++ {
3531
select {
3632
case msg := <-res:
3733
assert.Equal(t, expectedValues[i], msg)

0 commit comments

Comments
 (0)