Skip to content

Commit

Permalink
Flush reporter from root scope only (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdarulis authored and mway committed Aug 6, 2019
1 parent f266f90 commit 3332297
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
10 changes: 4 additions & 6 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
38 changes: 37 additions & 1 deletion scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3332297

Please sign in to comment.