Skip to content

Commit

Permalink
Report buffered metrics one final time on Close() (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
schallert authored and robskillington committed May 15, 2017
1 parent df06c47 commit 8064211
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go:
- 1.5
- 1.6
- 1.7
- tip
- 1.8
env:
global:
- GO15VENDOREXPERIMENT=1
Expand Down
19 changes: 17 additions & 2 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ func (s *scope) reportLoopRun() {
return
}

s.reportRegistryWithLock()

s.status.RUnlock()
}

// reports current registry with scope status lock held
func (s *scope) reportRegistryWithLock() {
s.registry.RLock()
if s.reporter != nil {
for _, ss := range s.registry.subscopes {
Expand All @@ -258,8 +265,6 @@ func (s *scope) reportLoopRun() {
}
}
s.registry.RUnlock()

s.status.RUnlock()
}

func (s *scope) Counter(name string) Counter {
Expand Down Expand Up @@ -479,8 +484,18 @@ func (s *scope) Snapshot() Snapshot {

func (s *scope) Close() error {
s.status.Lock()

// don't wait to close more than once (panic on double close of
// s.status.quit)
if s.status.closed {
s.status.Unlock()
return nil
}

s.status.closed = true
close(s.status.quit)
s.reportRegistryWithLock()

s.status.Unlock()

if closer, ok := s.baseReporter.(io.Closer); ok {
Expand Down
17 changes: 16 additions & 1 deletion scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,5 +693,20 @@ func TestScopeAvoidReportLoopRunOnClose(t *testing.T) {
assert.NoError(t, closer.Close())

s.reportLoopRun()
assert.Equal(t, int32(1), atomic.LoadInt32(&r.flushes))
assert.Equal(t, int32(2), atomic.LoadInt32(&r.flushes))
}

func TestScopeFlushOnClose(t *testing.T) {
r := newTestStatsReporter()
root, closer := NewRootScope(ScopeOptions{Reporter: r}, time.Hour)

r.cg.Add(1)
root.Counter("foo").Inc(1)
assert.Nil(t, r.counters["foo"])

assert.NoError(t, closer.Close())

assert.EqualValues(t, 1, r.counters["foo"].val)

assert.NoError(t, closer.Close())
}

0 comments on commit 8064211

Please sign in to comment.