Skip to content

Commit b218e4b

Browse files
dashpoleMrAlias
andauthored
Don't track min and max when disabled (#7306)
Minor optimization for histogram `measure()`. When min/max are disabled for histograms, don't track it during measure(). the `bin` function is split into `bin` and `minMax` to facilitate this. --------- Co-authored-by: Tyler Yahn <[email protected]>
1 parent 810095e commit b218e4b

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1212

1313
- Drop support for [Go 1.23]. (#7274)
1414

15+
### Changed
16+
17+
- Improve performance of histogram `Record` in `go.opentelemetry.io/otel/sdk/metric` when min and max are disabled using `NoMinMax`. (#7306)
18+
1519
<!-- Released section -->
1620
<!-- Don't change this section unless doing release -->
1721

sdk/metric/internal/aggregate/histogram.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ func newBuckets[N int64 | float64](attrs attribute.Set, n int) *buckets[N] {
3131

3232
func (b *buckets[N]) sum(value N) { b.total += value }
3333

34-
func (b *buckets[N]) bin(idx int, value N) {
34+
func (b *buckets[N]) bin(idx int) {
3535
b.counts[idx]++
3636
b.count++
37+
}
38+
39+
func (b *buckets[N]) minMax(value N) {
3740
if value < b.min {
3841
b.min = value
3942
} else if value > b.max {
@@ -44,8 +47,9 @@ func (b *buckets[N]) bin(idx int, value N) {
4447
// histValues summarizes a set of measurements as an histValues with
4548
// explicitly defined buckets.
4649
type histValues[N int64 | float64] struct {
47-
noSum bool
48-
bounds []float64
50+
noMinMax bool
51+
noSum bool
52+
bounds []float64
4953

5054
newRes func(attribute.Set) FilteredExemplarReservoir[N]
5155
limit limiter[*buckets[N]]
@@ -55,7 +59,7 @@ type histValues[N int64 | float64] struct {
5559

5660
func newHistValues[N int64 | float64](
5761
bounds []float64,
58-
noSum bool,
62+
noMinMax, noSum bool,
5963
limit int,
6064
r func(attribute.Set) FilteredExemplarReservoir[N],
6165
) *histValues[N] {
@@ -66,11 +70,12 @@ func newHistValues[N int64 | float64](
6670
b := slices.Clone(bounds)
6771
slices.Sort(b)
6872
return &histValues[N]{
69-
noSum: noSum,
70-
bounds: b,
71-
newRes: r,
72-
limit: newLimiter[*buckets[N]](limit),
73-
values: make(map[attribute.Distinct]*buckets[N]),
73+
noMinMax: noMinMax,
74+
noSum: noSum,
75+
bounds: b,
76+
newRes: r,
77+
limit: newLimiter[*buckets[N]](limit),
78+
values: make(map[attribute.Distinct]*buckets[N]),
7479
}
7580
}
7681

@@ -109,7 +114,10 @@ func (s *histValues[N]) measure(
109114
b.min, b.max = value, value
110115
s.values[attr.Equivalent()] = b
111116
}
112-
b.bin(idx, value)
117+
b.bin(idx)
118+
if !s.noMinMax {
119+
b.minMax(value)
120+
}
113121
if !s.noSum {
114122
b.sum(value)
115123
}
@@ -125,8 +133,7 @@ func newHistogram[N int64 | float64](
125133
r func(attribute.Set) FilteredExemplarReservoir[N],
126134
) *histogram[N] {
127135
return &histogram[N]{
128-
histValues: newHistValues[N](boundaries, noSum, limit, r),
129-
noMinMax: noMinMax,
136+
histValues: newHistValues[N](boundaries, noMinMax, noSum, limit, r),
130137
start: now(),
131138
}
132139
}
@@ -136,8 +143,7 @@ func newHistogram[N int64 | float64](
136143
type histogram[N int64 | float64] struct {
137144
*histValues[N]
138145

139-
noMinMax bool
140-
start time.Time
146+
start time.Time
141147
}
142148

143149
func (s *histogram[N]) delta(

sdk/metric/internal/aggregate/histogram_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,11 @@ func testBucketsBin[N int64 | float64]() func(t *testing.T) {
287287
}
288288

289289
assertB([]uint64{0, 0, 0}, 0, 0, 0)
290-
b.bin(1, 2)
290+
b.bin(1)
291+
b.minMax(2)
291292
assertB([]uint64{0, 1, 0}, 1, 0, 2)
292-
b.bin(0, -1)
293+
b.bin(0)
294+
b.minMax(-1)
293295
assertB([]uint64{1, 1, 0}, 2, -1, 2)
294296
}
295297
}

0 commit comments

Comments
 (0)