Skip to content

Commit

Permalink
don't flush zero counters (#136)
Browse files Browse the repository at this point in the history
This undoes a change in 0ed02ca,
which always emitted counters even when if the value was set to zero,
i.e. declared, but never incremented.

The idea behind changing this back is to have some consistency - some
orgs like to skip sending zeros in order to save on network costs;
when sending them, aggregation systems tend to drop them anyway. It's
best to not emit them at all.
  • Loading branch information
tomwans authored Jul 20, 2022
1 parent 0392366 commit 296fd24
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ func (s *statStore) Flush() {
s.genMtx.RUnlock()

s.counters.Range(func(key, v interface{}) bool {
s.sink.FlushCounter(key.(string), v.(*counter).latch())
// do not flush counters that are set to zero
if value := v.(*counter).latch(); value != 0 {
s.sink.FlushCounter(key.(string), value)
}
return true
})

Expand Down
4 changes: 2 additions & 2 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func TestMilliTimer(t *testing.T) {
}
}

// Ensure 0 counters are flushed
// Ensure 0 counters are not flushed
func TestZeroCounters(t *testing.T) {
sink := &testStatSink{}
store := NewStore(sink, true)
store.NewCounter("test")
store.Flush()

expected := "test:0|c\n"
expected := ""
counter := sink.record
if counter != expected {
t.Errorf("wanted %q got %q", expected, counter)
Expand Down

0 comments on commit 296fd24

Please sign in to comment.