diff --git a/CHANGELOG.md b/CHANGELOG.md index 1409325b27d..b863c47c3a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/stdout/stdoutmetric`. (#7492) - Improve the concurrent performance of `HistogramReservoir` in `go.opentelemetry.io/otel/sdk/metric/exemplar` by 4x. (#7443) - Improve performance of concurrent synchronous gauge measurements in `go.opentelemetry.io/otel/sdk/metric`. (#7478) +- Improve performance of concurrent exponential histogram measurements in `go.opentelemetry.io/otel/sdk/metric`. (#7702) diff --git a/sdk/metric/internal/aggregate/exponential_histogram.go b/sdk/metric/internal/aggregate/exponential_histogram.go index 5b3a19c067d..2aeba437894 100644 --- a/sdk/metric/internal/aggregate/exponential_histogram.go +++ b/sdk/metric/internal/aggregate/exponential_histogram.go @@ -32,10 +32,9 @@ type expoHistogramDataPoint[N int64 | float64] struct { attrs attribute.Set res FilteredExemplarReservoir[N] - count uint64 - min N - max N - sum N + min N + max N + sum N maxSize int noMinMax bool @@ -74,8 +73,6 @@ func newExpoHistogramDataPoint[N int64 | float64]( // record adds a new measurement to the histogram. It will rescale the buckets if needed. func (p *expoHistogramDataPoint[N]) record(v N) { - p.count++ - if !p.noMinMax { if v < p.min { p.min = v @@ -193,6 +190,10 @@ func (p *expoHistogramDataPoint[N]) scaleChange(bin, startBin int32, length int) return count } +func (p *expoHistogramDataPoint[N]) count() uint64 { + return p.posBuckets.count() + p.negBuckets.count() + p.zeroCount +} + // expoBuckets is a set of buckets in an exponential histogram. type expoBuckets struct { startBin int32 @@ -285,6 +286,14 @@ func (b *expoBuckets) downscale(delta int32) { b.startBin >>= delta } +func (b *expoBuckets) count() uint64 { + var total uint64 + for _, count := range b.counts { + total += count + } + return total +} + // newExponentialHistogram returns an Aggregator that summarizes a set of // measurements as an exponential histogram. Each histogram is scoped by attributes // and the aggregation cycle the measurements were made in. @@ -376,7 +385,7 @@ func (e *expoHistogram[N]) delta( hDPts[i].Attributes = val.attrs hDPts[i].StartTime = e.start hDPts[i].Time = t - hDPts[i].Count = val.count + hDPts[i].Count = val.count() hDPts[i].Scale = val.scale hDPts[i].ZeroCount = val.zeroCount hDPts[i].ZeroThreshold = 0.0 @@ -439,7 +448,7 @@ func (e *expoHistogram[N]) cumulative( hDPts[i].Attributes = val.attrs hDPts[i].StartTime = e.start hDPts[i].Time = t - hDPts[i].Count = val.count + hDPts[i].Count = val.count() hDPts[i].Scale = val.scale hDPts[i].ZeroCount = val.zeroCount hDPts[i].ZeroThreshold = 0.0 diff --git a/sdk/metric/internal/aggregate/exponential_histogram_test.go b/sdk/metric/internal/aggregate/exponential_histogram_test.go index a27bdc73a54..a4519eafbb4 100644 --- a/sdk/metric/internal/aggregate/exponential_histogram_test.go +++ b/sdk/metric/internal/aggregate/exponential_histogram_test.go @@ -706,7 +706,6 @@ func TestSubNormal(t *testing.T) { want := &expoHistogramDataPoint[float64]{ attrs: alice, maxSize: 4, - count: 3, min: math.SmallestNonzeroFloat64, max: math.SmallestNonzeroFloat64, sum: 3 * math.SmallestNonzeroFloat64,