Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Calculate IntervalDelta correctly if there are no changes to the counter
Browse files Browse the repository at this point in the history
  • Loading branch information
prburgu authored and palkan committed Mar 15, 2021
1 parent fdf6aeb commit 9d47ffc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Counter) Value() int64 {

// IntervalValue allows to get last interval value for counter.
func (c *Counter) IntervalValue() int64 {
if c.lastIntervalDelta == 0 {
if c.lastIntervalValue == 0 {
return c.Value()
}
return atomic.LoadInt64(&c.lastIntervalDelta)
Expand Down
6 changes: 6 additions & 0 deletions metrics/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ func TestCounter(t *testing.T) {
cnt.Inc()
assert.Equal(t, int64(1501), cnt.Value())
assert.Equal(t, int64(1500), cnt.IntervalValue())
cnt.UpdateDelta()
assert.Equal(t, int64(1501), cnt.Value())
assert.Equal(t, int64(1), cnt.IntervalValue())
cnt.UpdateDelta()
assert.Equal(t, int64(1501), cnt.Value())
assert.Equal(t, int64(0), cnt.IntervalValue())
}

0 comments on commit 9d47ffc

Please sign in to comment.