-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DO NOT LAND] Process only changed metrics #236
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,9 @@ | |
done chan struct{} | ||
wg sync.WaitGroup | ||
root bool | ||
|
||
counterChangeNotifyCh chan *counter | ||
gaugeChangeNotifyCh chan *gauge | ||
} | ||
|
||
// ScopeOptions is a set of options to construct a scope. | ||
|
@@ -166,23 +169,25 @@ | |
} | ||
|
||
s := &scope{ | ||
baseReporter: baseReporter, | ||
bucketCache: newBucketCache(), | ||
cachedReporter: opts.CachedReporter, | ||
counters: make(map[string]*counter), | ||
countersSlice: make([]*counter, 0, _defaultInitialSliceSize), | ||
defaultBuckets: opts.DefaultBuckets, | ||
done: make(chan struct{}), | ||
gauges: make(map[string]*gauge), | ||
gaugesSlice: make([]*gauge, 0, _defaultInitialSliceSize), | ||
histograms: make(map[string]*histogram), | ||
histogramsSlice: make([]*histogram, 0, _defaultInitialSliceSize), | ||
prefix: sanitizer.Name(opts.Prefix), | ||
reporter: opts.Reporter, | ||
sanitizer: sanitizer, | ||
separator: sanitizer.Name(opts.Separator), | ||
timers: make(map[string]*timer), | ||
root: true, | ||
baseReporter: baseReporter, | ||
bucketCache: newBucketCache(), | ||
cachedReporter: opts.CachedReporter, | ||
counters: make(map[string]*counter), | ||
countersSlice: make([]*counter, 0, _defaultInitialSliceSize), | ||
defaultBuckets: opts.DefaultBuckets, | ||
done: make(chan struct{}), | ||
gauges: make(map[string]*gauge), | ||
gaugesSlice: make([]*gauge, 0, _defaultInitialSliceSize), | ||
histograms: make(map[string]*histogram), | ||
histogramsSlice: make([]*histogram, 0, _defaultInitialSliceSize), | ||
prefix: sanitizer.Name(opts.Prefix), | ||
reporter: opts.Reporter, | ||
sanitizer: sanitizer, | ||
separator: sanitizer.Name(opts.Separator), | ||
timers: make(map[string]*timer), | ||
root: true, | ||
counterChangeNotifyCh: make(chan *counter, 1024), | ||
gaugeChangeNotifyCh: make(chan *gauge, 1024), | ||
} | ||
|
||
// NB(r): Take a copy of the tags on creation | ||
|
@@ -196,7 +201,7 @@ | |
s.wg.Add(1) | ||
go func() { | ||
defer s.wg.Done() | ||
s.reportLoop(interval) | ||
s.processLoop(interval) | ||
}() | ||
} | ||
|
||
|
@@ -249,7 +254,7 @@ | |
} | ||
|
||
// reportLoop is used by the root scope for periodic reporting | ||
func (s *scope) reportLoop(interval time.Duration) { | ||
ticker := time.NewTicker(interval) | ||
defer ticker.Stop() | ||
|
||
|
@@ -281,6 +286,48 @@ | |
} | ||
} | ||
|
||
func (s *scope) processLoop(interval time.Duration) { | ||
ticker := time.NewTicker(interval) | ||
counters := make([]*counter, 0, _defaultInitialSliceSize) | ||
gauges := make([]*gauge, 0, _defaultInitialSliceSize) | ||
|
||
defer ticker.Stop() | ||
for { | ||
select { | ||
case c := <-s.counterChangeNotifyCh: | ||
counters = append(counters, c) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, this will have the effect of flushing out any pending accumulated metrics which is desirable. |
||
case g := <-s.gaugeChangeNotifyCh: | ||
gauges = append(gauges, g) | ||
case <-ticker.C: | ||
s.reportChanges(counters, gauges) | ||
s.cachedReporter.Flush() | ||
// Reset the changed counters and gauges | ||
var zeroCounter *counter | ||
for i := range counters { | ||
counters[i] = zeroCounter | ||
} | ||
counters = counters[:0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to set the elements in the slice to the zero value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what we talked about -- https://go.dev/play/p/WIRovDFJhE5 |
||
|
||
var zeroGauge *gauge | ||
for i := range gauges { | ||
gauges[i] = zeroGauge | ||
} | ||
gauges = gauges[:0] | ||
default: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (s *scope) reportChanges(counters []*counter, gauges []*gauge) { | ||
for _, c := range counters { | ||
c.cachedReport() | ||
} | ||
for _, g := range gauges { | ||
g.cachedReport() | ||
} | ||
} | ||
|
||
func (s *scope) Counter(name string) Counter { | ||
name = s.sanitizer.Name(name) | ||
if c, ok := s.counter(name); ok { | ||
|
@@ -295,14 +342,18 @@ | |
} | ||
|
||
var cachedCounter CachedCount | ||
var changeNotifyFn func(c *counter) | ||
if s.cachedReporter != nil { | ||
cachedCounter = s.cachedReporter.AllocateCounter( | ||
s.fullyQualifiedName(name), | ||
s.tags, | ||
) | ||
changeNotifyFn = func(c *counter) { | ||
s.counterChangeNotifyCh <- c | ||
} | ||
} | ||
|
||
c := newCounter(cachedCounter) | ||
c := newCounter(cachedCounter, changeNotifyFn) | ||
s.counters[name] = c | ||
s.countersSlice = append(s.countersSlice, c) | ||
|
||
|
@@ -331,13 +382,17 @@ | |
} | ||
|
||
var cachedGauge CachedGauge | ||
var changeNotifyFn func(g *gauge) | ||
if s.cachedReporter != nil { | ||
cachedGauge = s.cachedReporter.AllocateGauge( | ||
s.fullyQualifiedName(name), s.tags, | ||
) | ||
changeNotifyFn = func(g *gauge) { | ||
s.gaugeChangeNotifyCh <- g | ||
} | ||
} | ||
|
||
g := newGauge(cachedGauge) | ||
g := newGauge(cachedGauge, changeNotifyFn) | ||
s.gauges[name] = g | ||
s.gaugesSlice = append(s.gaugesSlice, g) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm few thoughts about this channel