Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do flush 0-valued counters #108

Merged
merged 6 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,7 @@ func (s *statStore) Flush() {
s.genMtx.RUnlock()

s.counters.Range(func(key, v interface{}) bool {
// Skip counters not incremented
if value := v.(*counter).latch(); value != 0 {
s.sink.FlushCounter(key.(string), value)
}
s.sink.FlushCounter(key.(string), v.(*counter).latch())
return true
})

Expand Down
42 changes: 26 additions & 16 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ func TestTimer(t *testing.T) {
}
}

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

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

func randomString(tb testing.TB, size int) string {
b := make([]byte, hex.DecodedLen(size))
if _, err := crand.Read(b); err != nil {
Expand Down Expand Up @@ -276,33 +290,31 @@ func TestPerInstanceStats(t *testing.T) {
},
}

sink := mock.NewSink()

testPerInstanceMethods := func(t *testing.T, scope Scope) {
testPerInstanceMethods := func(t *testing.T, setupScope func(Scope) Scope) {
charlievieth marked this conversation as resolved.
Show resolved Hide resolved
for _, x := range testCases {
sink.Reset()
sink := mock.NewSink()
scope := setupScope(&statStore{sink: sink})

scope.NewPerInstanceCounter("name", x.tags).Inc()
scope.NewPerInstanceGauge("name", x.tags).Inc()
scope.NewPerInstanceTimer("name", x.tags).AddValue(1)
scope.Store().Flush()

for key := range sink.Counters() {
if key != x.expected {
t.Errorf("Counter (%+v): got: %q want: %q", x, key, x.expected)
}
break
}

scope.NewPerInstanceGauge("name", x.tags).Inc()
scope.Store().Flush()
for key := range sink.Counters() {
for key := range sink.Gauges() {
if key != x.expected {
t.Errorf("Gauge (%+v): got: %q want: %q", x, key, x.expected)
}
break
}

scope.NewPerInstanceTimer("name", x.tags).AddValue(1)
scope.Store().Flush()
for key := range sink.Counters() {
for key := range sink.Timers() {
if key != x.expected {
t.Errorf("Timer (%+v): got: %q want: %q", x, key, x.expected)
}
Expand All @@ -312,20 +324,18 @@ func TestPerInstanceStats(t *testing.T) {
}

t.Run("StatsStore", func(t *testing.T) {
store := &statStore{sink: sink}

testPerInstanceMethods(t, store)
testPerInstanceMethods(t, func(scope Scope) Scope { return scope })
})

t.Run("SubScope", func(t *testing.T) {
store := &subScope{registry: &statStore{sink: sink}, name: "x"}

// Add sub-scope prefix to the name
for i, x := range testCases {
testCases[i].expected = "x." + x.expected
}

testPerInstanceMethods(t, store)
testPerInstanceMethods(t, func(scope Scope) Scope {
return scope.Scope("x")
})
})
}

Expand Down