Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mway committed Mar 16, 2021
1 parent f05ac64 commit bedce66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
37 changes: 11 additions & 26 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type scope struct {
timersSlice []*timer

bucketCache *bucketCache
lastReport time.Time
lastReport int64
root bool
}

Expand Down Expand Up @@ -181,8 +181,9 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
histograms: make(map[string]*histogram),
histogramsSlice: make([]*histogram, 0, _defaultInitialSliceSize),
timers: make(map[string]*timer),
bucketCache: newBucketCache(),
timersSlice: make([]*timer, 0, _defaultInitialSliceSize),
bucketCache: newBucketCache(),
lastReport: time.Now().UnixNano(),
}

// NB(r): Take a copy of the tags on creation
Expand All @@ -203,34 +204,26 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
func (s *scope) report(r StatsReporter) (reported bool) {
s.cm.RLock()
for name, counter := range s.counters {
if rep := counter.report(s.fullyQualifiedName(name), s.tags, r); rep {
reported = true
}
reported = counter.report(s.fullyQualifiedName(name), s.tags, r) || reported
}
s.cm.RUnlock()

s.gm.RLock()
for name, gauge := range s.gauges {
if rep := gauge.report(s.fullyQualifiedName(name), s.tags, r); rep {
reported = true
}
reported = gauge.report(s.fullyQualifiedName(name), s.tags, r) || reported
}
s.gm.RUnlock()

// we do nothing for timers here because timers report directly to ths StatsReporter without buffering
s.tm.RLock()
for _, timer := range s.timersSlice {
if rep := timer.hasReported(); rep {
reported = true
}
reported = timer.hasReported() || reported
}
s.tm.RUnlock()

s.hm.RLock()
for name, histogram := range s.histograms {
if rep := histogram.report(s.fullyQualifiedName(name), s.tags, r); rep {
reported = true
}
reported = histogram.report(s.fullyQualifiedName(name), s.tags, r) || reported
}
s.hm.RUnlock()

Expand All @@ -240,34 +233,26 @@ func (s *scope) report(r StatsReporter) (reported bool) {
func (s *scope) cachedReport() (reported bool) {
s.cm.RLock()
for _, counter := range s.countersSlice {
if rep := counter.cachedReport(); rep {
reported = true
}
reported = counter.cachedReport() || reported
}
s.cm.RUnlock()

s.gm.RLock()
for _, gauge := range s.gaugesSlice {
if rep := gauge.cachedReport(); rep {
reported = true
}
reported = gauge.cachedReport() || reported
}
s.gm.RUnlock()

// we do nothing for timers here because timers report directly to ths StatsReporter without buffering
s.tm.RLock()
for _, timer := range s.timersSlice {
if rep := timer.hasReported(); rep {
reported = true
}
reported = timer.hasReported() || reported
}
s.tm.RUnlock()

s.hm.RLock()
for _, histogram := range s.histogramsSlice {
if rep := histogram.cachedReport(); rep {
reported = true
}
reported = histogram.cachedReport() || reported
}
s.hm.RUnlock()

Expand Down
9 changes: 5 additions & 4 deletions scope_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (r *scopeRegistry) Report(reporter StatsReporter) {
r.mu.RLock()
defer r.mu.RUnlock()

now := time.Now()
now := time.Now().UnixNano()
for key, s := range r.subscopes {
if s.report(reporter) {
s.lastReport = now
continue
}

if r.ttl > 0 && now.Sub(s.lastReport) > r.ttl {
if r.ttl > 0 && time.Duration(now-s.lastReport) > r.ttl {
s.release(r.deep)

if r.deep {
Expand All @@ -72,14 +72,14 @@ func (r *scopeRegistry) CachedReport() {
r.mu.RLock()
defer r.mu.RUnlock()

now := time.Now()
now := time.Now().UnixNano()
for key, s := range r.subscopes {
if s.cachedReport() {
s.lastReport = now
continue
}

if r.ttl > 0 && now.Sub(s.lastReport) > r.ttl {
if r.ttl > 0 && time.Duration(now-s.lastReport) > r.ttl {
s.release(r.deep)

if r.deep {
Expand Down Expand Up @@ -140,6 +140,7 @@ func (r *scopeRegistry) Subscope(parent *scope, prefix string, tags map[string]s
histogramsSlice: make([]*histogram, 0, _defaultInitialSliceSize),
timers: make(map[string]*timer),
bucketCache: parent.bucketCache,
lastReport: time.Now().UnixNano(),
}
r.subscopes[key] = subscope
return subscope
Expand Down

0 comments on commit bedce66

Please sign in to comment.