mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-03-29 21:47:00 +02:00
Instead of using a global random number generator for all `randRes`, have each value use its own. This removes the need for locking and managing concurrent safe access to the global. Also, the field, given the `Reservoir` type is not concurrent safe and the metric pipeline guards this, does not need a `sync.Mutex` to guard it. Supersedes https://github.com/open-telemetry/opentelemetry-go/pull/5815 Fix #5814 ### Performance Analysis This change has approximately equivalent performance as the existing code based on existing benchmarks. ```terminal goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/metric cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ Exemplars/Int64Counter/8-8 14.00µ ± 3% 13.44µ ± 4% -3.98% (p=0.001 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ Exemplars/Int64Counter/8-8 3.791Ki ± 0% 3.791Ki ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ Exemplars/Int64Counter/8-8 84.00 ± 0% 84.00 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal ```
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package exemplar
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"math/rand"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFixedSize(t *testing.T) {
|
|
t.Run("Int64", ReservoirTest[int64](func(n int) (Reservoir, int) {
|
|
return FixedSize(n), n
|
|
}))
|
|
|
|
t.Run("Float64", ReservoirTest[float64](func(n int) (Reservoir, int) {
|
|
return FixedSize(n), n
|
|
}))
|
|
}
|
|
|
|
func TestFixedSizeSamplingCorrectness(t *testing.T) {
|
|
intensity := 0.1
|
|
sampleSize := 1000
|
|
|
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
data := make([]float64, sampleSize*1000)
|
|
for i := range data {
|
|
// Generate exponentially distributed data.
|
|
data[i] = (-1.0 / intensity) * math.Log(rng.Float64())
|
|
}
|
|
// Sort to test position bias.
|
|
slices.Sort(data)
|
|
|
|
r := FixedSize(sampleSize)
|
|
for _, value := range data {
|
|
r.Offer(context.Background(), staticTime, NewValue(value), nil)
|
|
}
|
|
|
|
var sum float64
|
|
for _, m := range r.(*randRes).store {
|
|
sum += m.Value.Float64()
|
|
}
|
|
mean := sum / float64(sampleSize)
|
|
|
|
// Check the intensity/rate of the sampled distribution is preserved
|
|
// ensuring no bias in our random sampling algorithm.
|
|
assert.InDelta(t, 1/mean, intensity, 0.02) // Within 5σ.
|
|
}
|