Skip to content

Commit 1538e9b

Browse files
craig[bot]RaduBerinde
andcommitted
Merge #144555
144555: aggmetric: clean up ChildrenStorage interface r=RaduBerinde a=RaduBerinde The interface has a `Do` function which passes an arbitrary `interface{}` object to the function. The only thing the function can do with that object is to pass it to `GetChildMetric`. This dance is strange and unnecessary; we rename `Do` to `ForEach` and change it to pass `ChildMetric` objects directly. Informs: #144504 Epic: none Release note: None Co-authored-by: Radu Berinde <[email protected]>
2 parents 7f3eb70 + df8aa82 commit 1538e9b

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

pkg/util/metric/aggmetric/agg_metric.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ func (cs *childSet) Each(
108108
) {
109109
cs.mu.Lock()
110110
defer cs.mu.Unlock()
111-
cs.mu.children.Do(func(e interface{}) {
112-
cm := cs.mu.children.GetChildMetric(e)
111+
cs.mu.children.ForEach(func(cm ChildMetric) {
113112
pm := cm.ToPrometheusMetric()
114113

115114
childLabels := make([]*io_prometheus_client.LabelPair, 0, len(labels)+len(cs.labels))
@@ -130,8 +129,8 @@ func (cs *childSet) Each(
130129
func (cs *childSet) apply(applyFn func(item MetricItem)) {
131130
cs.mu.Lock()
132131
defer cs.mu.Unlock()
133-
cs.mu.children.Do(func(e interface{}) {
134-
applyFn(cs.mu.children.GetChildMetric(e).(MetricItem))
132+
cs.mu.children.ForEach(func(cm ChildMetric) {
133+
applyFn(cm)
135134
})
136135
}
137136

@@ -192,8 +191,7 @@ func (sm *SQLMetric) Each(
192191
sm.mu.Lock()
193192
defer sm.mu.Unlock()
194193

195-
sm.mu.children.Do(func(e interface{}) {
196-
cm := sm.mu.children.GetChildMetric(e)
194+
sm.mu.children.ForEach(func(cm ChildMetric) {
197195
pm := cm.ToPrometheusMetric()
198196

199197
childLabels := make([]*io_prometheus_client.LabelPair, 0, len(labels)+2)
@@ -317,8 +315,9 @@ type ChildrenStorage interface {
317315
Get(labelVals ...string) (ChildMetric, bool)
318316
Add(metric ChildMetric)
319317
Del(key ChildMetric)
320-
Do(f func(e interface{}))
321-
GetChildMetric(e interface{}) ChildMetric
318+
319+
// ForEach calls f for each child metric, in arbitrary order.
320+
ForEach(f func(metric ChildMetric))
322321
Clear()
323322
}
324323

@@ -329,10 +328,6 @@ type UnorderedCacheWrapper struct {
329328
cache *cache.UnorderedCache
330329
}
331330

332-
func (ucw *UnorderedCacheWrapper) GetChildMetric(e interface{}) ChildMetric {
333-
return e.(*cache.Entry).Value.(ChildMetric)
334-
}
335-
336331
func (ucw *UnorderedCacheWrapper) Get(labelVals ...string) (ChildMetric, bool) {
337332
hashKey := metricKey(labelVals...)
338333
value, ok := ucw.cache.Get(hashKey)
@@ -358,9 +353,9 @@ func (ucw *UnorderedCacheWrapper) Del(metric ChildMetric) {
358353
}
359354
}
360355

361-
func (ucw *UnorderedCacheWrapper) Do(f func(e interface{})) {
356+
func (ucw *UnorderedCacheWrapper) ForEach(f func(metric ChildMetric)) {
362357
ucw.cache.Do(func(e *cache.Entry) {
363-
f(e)
358+
f(e.Value.(ChildMetric))
364359
})
365360
}
366361

@@ -395,17 +390,13 @@ func (b BtreeWrapper) Del(metric ChildMetric) {
395390
}
396391
}
397392

398-
func (b BtreeWrapper) Do(f func(e interface{})) {
393+
func (b BtreeWrapper) ForEach(f func(metric ChildMetric)) {
399394
b.tree.Ascend(func(i btree.Item) bool {
400-
f(i)
395+
f(i.(ChildMetric))
401396
return true
402397
})
403398
}
404399

405-
func (b BtreeWrapper) GetChildMetric(e interface{}) ChildMetric {
406-
return e.(ChildMetric)
407-
}
408-
409400
func (b BtreeWrapper) Clear() {
410401
b.tree.Clear(false)
411402
}

pkg/util/metric/aggmetric/gauge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func NewFunctionalGauge(
4646
values := make([]int64, 0)
4747
g.childSet.mu.Lock()
4848
defer g.childSet.mu.Unlock()
49-
g.childSet.mu.children.Do(func(e interface{}) {
50-
cg := g.childSet.mu.children.GetChildMetric(e).(*Gauge)
49+
g.childSet.mu.children.ForEach(func(metric ChildMetric) {
50+
cg := metric.(*Gauge)
5151
values = append(values, cg.Value())
5252
})
5353
return f(values)

pkg/util/metric/aggmetric/histogram.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ func NewSQLHistogram(opts metric.HistogramOptions) *SQLHistogram {
247247
func (sh *SQLHistogram) apply(applyFn func(childMetric ChildMetric)) {
248248
sh.mu.Lock()
249249
defer sh.mu.Unlock()
250-
sh.mu.children.Do(func(e interface{}) {
251-
applyFn(sh.mu.children.GetChildMetric(e).(*SQLChildHistogram))
250+
sh.mu.children.ForEach(func(metric ChildMetric) {
251+
applyFn(metric.(*SQLChildHistogram))
252252
})
253253
}
254254

0 commit comments

Comments
 (0)