1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-10 09:50:58 +02:00

Refactor exponential histogram tests to use existing fixtures (#4747)

* Refactor expo hist test to use existing fixtures

The tests for the exponential histogram create their own testing
fixtures. There is nothing these new fixtures do that cannot already be
done with the existing testing fixtures used by all the other aggregate
functions. Unify the exponential histogram testing to use the existing
fixtures.

* Add alt input for cumulative test
This commit is contained in:
Tyler Yahn 2023-12-09 08:08:24 -08:00 committed by GitHub
parent b5afa704f1
commit 215eae31c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,10 +23,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global" "go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/sdk/metric/metricdata" "go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
) )
type noErrorHandler struct{ t *testing.T } type noErrorHandler struct{ t *testing.T }
@ -739,161 +737,181 @@ func TestSubNormal(t *testing.T) {
} }
func TestExponentialHistogramAggregation(t *testing.T) { func TestExponentialHistogramAggregation(t *testing.T) {
t.Run("Int64", testExponentialHistogramAggregation[int64]) t.Cleanup(mockTime(now))
t.Run("Float64", testExponentialHistogramAggregation[float64])
t.Run("Int64/Delta", testDeltaExpoHist[int64]())
t.Run("Float64/Delta", testDeltaExpoHist[float64]())
t.Run("Int64/Cumulative", testCumulativeExpoHist[int64]())
t.Run("Float64/Cumulative", testCumulativeExpoHist[float64]())
} }
func testExponentialHistogramAggregation[N int64 | float64](t *testing.T) { func testDeltaExpoHist[N int64 | float64]() func(t *testing.T) {
const ( in, out := Builder[N]{
maxSize = 4 Temporality: metricdata.DeltaTemporality,
maxScale = 20 Filter: attrFltr,
noMinMax = false }.ExponentialBucketHistogram(4, 20, false, false)
noSum = false ctx := context.Background()
) return test[N](in, out, []teststep[N]{
tests := []struct {
name string
build func() (Measure[N], ComputeAggregation)
input [][]N
want metricdata.ExponentialHistogram[N]
wantCount int
}{
{ {
name: "Delta Single", input: []arg[N]{},
build: func() (Measure[N], ComputeAggregation) { expect: output{
return Builder[N]{ n: 0,
agg: metricdata.ExponentialHistogram[N]{
Temporality: metricdata.DeltaTemporality, Temporality: metricdata.DeltaTemporality,
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{},
},
input: [][]N{
{4, 4, 4, 2, 16, 1},
},
want: metricdata.ExponentialHistogram[N]{
Temporality: metricdata.DeltaTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
{
Count: 6,
Min: metricdata.NewExtrema[N](1),
Max: metricdata.NewExtrema[N](16),
Sum: 31,
Scale: -1,
PositiveBucket: metricdata.ExponentialBucket{
Offset: -1,
Counts: []uint64{1, 4, 1},
},
},
}, },
}, },
wantCount: 1,
}, },
{ {
name: "Cumulative Single", input: []arg[N]{
build: func() (Measure[N], ComputeAggregation) { {ctx, 4, alice},
return Builder[N]{ {ctx, 4, alice},
Temporality: metricdata.CumulativeTemporality, {ctx, 4, alice},
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) {ctx, 2, alice},
{ctx, 16, alice},
{ctx, 1, alice},
}, },
input: [][]N{ expect: output{
{4, 4, 4, 2, 16, 1}, n: 1,
}, agg: metricdata.ExponentialHistogram[N]{
want: metricdata.ExponentialHistogram[N]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
{
Count: 6,
Min: metricdata.NewExtrema[N](1),
Max: metricdata.NewExtrema[N](16),
Sum: 31,
Scale: -1,
PositiveBucket: metricdata.ExponentialBucket{
Offset: -1,
Counts: []uint64{1, 4, 1},
},
},
},
},
wantCount: 1,
},
{
name: "Delta Multiple",
build: func() (Measure[N], ComputeAggregation) {
return Builder[N]{
Temporality: metricdata.DeltaTemporality, Temporality: metricdata.DeltaTemporality,
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
}, {
input: [][]N{ Attributes: fltrAlice,
{2, 3, 8}, StartTime: staticTime,
{4, 4, 4, 2, 16, 1}, Time: staticTime,
}, Count: 6,
want: metricdata.ExponentialHistogram[N]{ Min: metricdata.NewExtrema[N](1),
Temporality: metricdata.DeltaTemporality, Max: metricdata.NewExtrema[N](16),
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ Sum: 31,
{ Scale: -1,
Count: 6, PositiveBucket: metricdata.ExponentialBucket{
Min: metricdata.NewExtrema[N](1), Offset: -1,
Max: metricdata.NewExtrema[N](16), Counts: []uint64{1, 4, 1},
Sum: 31, },
Scale: -1,
PositiveBucket: metricdata.ExponentialBucket{
Offset: -1,
Counts: []uint64{1, 4, 1},
}, },
}, },
}, },
}, },
wantCount: 1,
}, },
{ {
name: "Cumulative Multiple ", // Delta sums are expected to reset.
build: func() (Measure[N], ComputeAggregation) { input: []arg[N]{},
return Builder[N]{ expect: output{
n: 0,
agg: metricdata.ExponentialHistogram[N]{
Temporality: metricdata.DeltaTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{},
},
},
},
})
}
func testCumulativeExpoHist[N int64 | float64]() func(t *testing.T) {
in, out := Builder[N]{
Temporality: metricdata.CumulativeTemporality,
Filter: attrFltr,
}.ExponentialBucketHistogram(4, 20, false, false)
ctx := context.Background()
return test[N](in, out, []teststep[N]{
{
input: []arg[N]{},
expect: output{
n: 0,
agg: metricdata.ExponentialHistogram[N]{
Temporality: metricdata.CumulativeTemporality, Temporality: metricdata.CumulativeTemporality,
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{},
},
}, },
input: [][]N{ },
{2, 3, 8}, {
{4, 4, 4, 2, 16, 1}, input: []arg[N]{
{ctx, 4, alice},
{ctx, 4, alice},
{ctx, 4, alice},
{ctx, 2, alice},
{ctx, 16, alice},
{ctx, 1, alice},
}, },
want: metricdata.ExponentialHistogram[N]{ expect: output{
Temporality: metricdata.CumulativeTemporality, n: 1,
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ agg: metricdata.ExponentialHistogram[N]{
{ Temporality: metricdata.CumulativeTemporality,
Count: 9, DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
Min: metricdata.NewExtrema[N](1), {
Max: metricdata.NewExtrema[N](16), Attributes: fltrAlice,
Sum: 44, StartTime: staticTime,
Scale: -1, Time: staticTime,
PositiveBucket: metricdata.ExponentialBucket{ Count: 6,
Offset: -1, Min: metricdata.NewExtrema[N](1),
Counts: []uint64{1, 6, 2}, Max: metricdata.NewExtrema[N](16),
Sum: 31,
Scale: -1,
PositiveBucket: metricdata.ExponentialBucket{
Offset: -1,
Counts: []uint64{1, 4, 1},
},
}, },
}, },
}, },
}, },
wantCount: 1,
}, },
} {
input: []arg[N]{
for _, tt := range tests { {ctx, 2, alice},
t.Run(tt.name, func(t *testing.T) { {ctx, 3, alice},
restore := withHandler(t) {ctx, 8, alice},
defer restore() },
in, out := tt.build() expect: output{
ctx := context.Background() n: 1,
agg: metricdata.ExponentialHistogram[N]{
var got metricdata.Aggregation Temporality: metricdata.CumulativeTemporality,
var count int DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
for _, n := range tt.input { {
for _, v := range n { Attributes: fltrAlice,
in(ctx, v, *attribute.EmptySet()) StartTime: staticTime,
} Time: staticTime,
count = out(&got) Count: 9,
} Min: metricdata.NewExtrema[N](1),
Max: metricdata.NewExtrema[N](16),
metricdatatest.AssertAggregationsEqual(t, tt.want, got, metricdatatest.IgnoreTimestamp()) Sum: 44,
assert.Equal(t, tt.wantCount, count) Scale: -1,
}) PositiveBucket: metricdata.ExponentialBucket{
} Offset: -1,
Counts: []uint64{1, 6, 2},
},
},
},
},
},
},
{
input: []arg[N]{},
expect: output{
n: 1,
agg: metricdata.ExponentialHistogram[N]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
{
Attributes: fltrAlice,
StartTime: staticTime,
Time: staticTime,
Count: 9,
Min: metricdata.NewExtrema[N](1),
Max: metricdata.NewExtrema[N](16),
Sum: 44,
Scale: -1,
PositiveBucket: metricdata.ExponentialBucket{
Offset: -1,
Counts: []uint64{1, 6, 2},
},
},
},
},
},
},
})
} }
func FuzzGetBin(f *testing.F) { func FuzzGetBin(f *testing.F) {