Skip to content

Commit b61a0e4

Browse files
committed
exphist: defer computing count until collect
1 parent f57bf14 commit b61a0e4

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

sdk/metric/internal/aggregate/exponential_histogram.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ type expoHistogramDataPoint[N int64 | float64] struct {
3232
attrs attribute.Set
3333
res FilteredExemplarReservoir[N]
3434

35-
count uint64
36-
min N
37-
max N
38-
sum N
35+
min N
36+
max N
37+
sum N
3938

4039
maxSize int
4140
noMinMax bool
@@ -74,8 +73,6 @@ func newExpoHistogramDataPoint[N int64 | float64](
7473

7574
// record adds a new measurement to the histogram. It will rescale the buckets if needed.
7675
func (p *expoHistogramDataPoint[N]) record(v N) {
77-
p.count++
78-
7976
if !p.noMinMax {
8077
if v < p.min {
8178
p.min = v
@@ -193,6 +190,10 @@ func (p *expoHistogramDataPoint[N]) scaleChange(bin, startBin int32, length int)
193190
return count
194191
}
195192

193+
func (p *expoHistogramDataPoint[N]) count() uint64 {
194+
return p.posBuckets.count() + p.negBuckets.count() + p.zeroCount
195+
}
196+
196197
// expoBuckets is a set of buckets in an exponential histogram.
197198
type expoBuckets struct {
198199
startBin int32
@@ -285,6 +286,14 @@ func (b *expoBuckets) downscale(delta int32) {
285286
b.startBin >>= delta
286287
}
287288

289+
func (b *expoBuckets) count() uint64 {
290+
var total uint64
291+
for _, count := range b.counts {
292+
total += count
293+
}
294+
return total
295+
}
296+
288297
// newExponentialHistogram returns an Aggregator that summarizes a set of
289298
// measurements as an exponential histogram. Each histogram is scoped by attributes
290299
// and the aggregation cycle the measurements were made in.
@@ -376,7 +385,7 @@ func (e *expoHistogram[N]) delta(
376385
hDPts[i].Attributes = val.attrs
377386
hDPts[i].StartTime = e.start
378387
hDPts[i].Time = t
379-
hDPts[i].Count = val.count
388+
hDPts[i].Count = val.count()
380389
hDPts[i].Scale = val.scale
381390
hDPts[i].ZeroCount = val.zeroCount
382391
hDPts[i].ZeroThreshold = 0.0
@@ -439,7 +448,7 @@ func (e *expoHistogram[N]) cumulative(
439448
hDPts[i].Attributes = val.attrs
440449
hDPts[i].StartTime = e.start
441450
hDPts[i].Time = t
442-
hDPts[i].Count = val.count
451+
hDPts[i].Count = val.count()
443452
hDPts[i].Scale = val.scale
444453
hDPts[i].ZeroCount = val.zeroCount
445454
hDPts[i].ZeroThreshold = 0.0

sdk/metric/internal/aggregate/exponential_histogram_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ func TestSubNormal(t *testing.T) {
706706
want := &expoHistogramDataPoint[float64]{
707707
attrs: alice,
708708
maxSize: 4,
709-
count: 3,
710709
min: math.SmallestNonzeroFloat64,
711710
max: math.SmallestNonzeroFloat64,
712711
sum: 3 * math.SmallestNonzeroFloat64,

0 commit comments

Comments
 (0)