1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00
Files
opentelemetry-go/sdk/metric/internal/exemplar/rand_test.go
T
Tyler Yahn dcfec0c2fa Add the random fixed size exemplar reservoir (#4852)
* Add the random fixed size exemplar reservoir

* Rename fixed.go to storage.go

* Update sdk/metric/internal/exemplar/rand.go

Co-authored-by: David Ashpole <dashpole@google.com>

* Remove stale ref to spec recommendation

* Add comments to clarify the reset/advance/Collect methods

* Apply comment from feedback

* Add random func to gen rand float64 on (0,1)

* Use random in TestFixedSizeSamplingCorrectness

* Add clarifying algorithm comments

Include a high-level overview of the algorithm implemented and clarify
parameter names to be consistent.

* Fix duplicate word

* Update sdk/metric/internal/exemplar/rand.go

* Comment TestFixedSizeSamplingCorrectness

* Update test delta

* Test collect less than cap

* Remove measurement.Valid method

---------

Co-authored-by: David Ashpole <dashpole@google.com>
Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
2024-01-29 07:26:30 -08:00

63 lines
1.7 KiB
Go

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exemplar
import (
"context"
"math"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixedSize(t *testing.T) {
t.Run("Int64", ReservoirTest[int64](func(n int) (Reservoir[int64], int) {
return FixedSize[int64](n), n
}))
t.Run("Float64", ReservoirTest[float64](func(n int) (Reservoir[float64], int) {
return FixedSize[float64](n), n
}))
}
func TestFixedSizeSamplingCorrectness(t *testing.T) {
intensity := 0.1
sampleSize := 1000
data := make([]float64, sampleSize*1000)
for i := range data {
// Generate exponentially distributed data.
data[i] = (-1.0 / intensity) * math.Log(random())
}
// Sort to test position bias.
sort.Float64s(data)
r := FixedSize[float64](sampleSize)
for _, value := range data {
r.Offer(context.Background(), staticTime, value, nil)
}
var sum float64
for _, m := range r.(*randRes[float64]).store {
sum += m.Value
}
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σ.
}