From 3332297784e46cd346ab6d9894fd4ea027dc9368 Mon Sep 17 00:00:00 2001 From: Vytenis Darulis Date: Tue, 6 Aug 2019 12:52:48 -0400 Subject: [PATCH] Flush reporter from root scope only (#108) --- scope.go | 10 ++++------ scope_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/scope.go b/scope.go index 64694586..59c72cdb 100644 --- a/scope.go +++ b/scope.go @@ -202,11 +202,9 @@ func (s *scope) report(r StatsReporter) { histogram.report(s.fullyQualifiedName(name), s.tags, r) } s.hm.RUnlock() - - r.Flush() } -func (s *scope) cachedReport(c CachedStatsReporter) { +func (s *scope) cachedReport() { s.cm.RLock() for _, counter := range s.counters { counter.cachedReport() @@ -226,8 +224,6 @@ func (s *scope) cachedReport(c CachedStatsReporter) { histogram.cachedReport() } s.hm.RUnlock() - - c.Flush() } // reportLoop is used by the root scope for periodic reporting @@ -266,10 +262,12 @@ func (s *scope) reportRegistryWithLock() { for _, ss := range s.registry.subscopes { ss.report(s.reporter) } + s.reporter.Flush() } else if s.cachedReporter != nil { for _, ss := range s.registry.subscopes { - ss.cachedReport(s.cachedReporter) + ss.cachedReport() } + s.cachedReporter.Flush() } s.registry.RUnlock() } diff --git a/scope_test.go b/scope_test.go index a8198403..58ccb9f1 100644 --- a/scope_test.go +++ b/scope_test.go @@ -327,8 +327,44 @@ func TestCachedReportLoop(t *testing.T) { r.hg.Add(1) s.Histogram("baz", MustMakeLinearValueBuckets(0, 10, 10)). RecordValue(42.42) + r.WaitAll() +} +func testReportLoopFlushOnce(t *testing.T, r *testStatsReporter, s Scope) { + r.cg.Add(2) + s.Counter("foobar").Inc(1) + s.SubScope("baz").Counter("bar").Inc(1) + r.gg.Add(2) + s.Gauge("zed").Update(1) + s.SubScope("baz").Gauge("zed").Update(1) + r.tg.Add(2) + s.Timer("ticky").Record(time.Millisecond * 175) + s.SubScope("woof").Timer("sod").Record(time.Millisecond * 175) + r.hg.Add(2) + s.SubScope("woofers").Histogram("boo", MustMakeLinearValueBuckets(0, 10, 10)). + RecordValue(42.42) + s.Histogram("baz", MustMakeLinearValueBuckets(0, 10, 10)). + RecordValue(42.42) r.WaitAll() + + v := atomic.LoadInt32(&r.flushes) + assert.Equal(t, int32(1), v) +} + +func TestCachedReporterFlushOnce(t *testing.T) { + r := newTestStatsReporter() + s, closer := NewRootScope(ScopeOptions{CachedReporter: r}, 10*time.Millisecond) + defer closer.Close() + + testReportLoopFlushOnce(t, r, s) +} + +func TestReporterFlushOnce(t *testing.T) { + r := newTestStatsReporter() + s, closer := NewRootScope(ScopeOptions{Reporter: r}, 10*time.Millisecond) + defer closer.Close() + + testReportLoopFlushOnce(t, r, s) } func TestWriteOnce(t *testing.T) { @@ -431,7 +467,7 @@ func TestCachedReporter(t *testing.T) { s.Histogram("qux", MustMakeLinearDurationBuckets(0, 10*time.Millisecond, 10)). RecordDuration(42 * time.Millisecond) - s.cachedReport(r) + s.cachedReport() r.WaitAll() assert.EqualValues(t, 1, r.counters["bar"].val)