From 296fd24177cc37f6b1875136104b38e477914aaa Mon Sep 17 00:00:00 2001 From: Tom Wanielista Date: Wed, 20 Jul 2022 09:03:27 -0700 Subject: [PATCH] don't flush zero counters (#136) This undoes a change in 0ed02ca2404195b02e124f16685a53046bbafa06, 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. --- stats.go | 5 ++++- stats_test.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/stats.go b/stats.go index a21cb0d..e379270 100644 --- a/stats.go +++ b/stats.go @@ -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 }) diff --git a/stats_test.go b/stats_test.go index 2b8a677..80f4372 100644 --- a/stats_test.go +++ b/stats_test.go @@ -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)