Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mway committed Feb 23, 2021
1 parent e99d74e commit 9878244
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
35 changes: 10 additions & 25 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type scope struct {
timersSlice []*timer

bucketCache map[uint64]bucketStorage
lastReport time.Time
lastReport int64
root bool
}

Expand Down Expand Up @@ -207,6 +207,7 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
timers: make(map[string]*timer),
timersSlice: make([]*timer, 0, _defaultInitialSliceSize),
bucketCache: make(map[uint64]bucketStorage),
lastReport: time.Now().UnixNano(),
}

// NB(r): Take a copy of the tags on creation
Expand All @@ -227,34 +228,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 @@ -264,34 +257,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 9878244

Please sign in to comment.