You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2026-06-19 21:45:50 +02:00
Improve test coverage for exponential histogram edge cases (#8129)
Some small testing improvements forked from https://github.com/open-telemetry/opentelemetry-go/pull/8077. This also fixes a flake where the order in which sums are added can change the resulting sum. Use assertSumEqual to handle this similar to other places in the test. Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/sdk/internal/x"
|
||||
@@ -1065,7 +1066,7 @@ func validateExponentialHistogram[N int64 | float64](t *testing.T, aggs []metric
|
||||
if attr == overflowSet {
|
||||
// The overflow set contains all the goroutines that didn't make the limit of 3
|
||||
assert.Equal(t, uint64(0), count%expectedSingleCount)
|
||||
assert.Equal(t, count/expectedSingleCount*uint64(expectedSingleSum), uint64(sum))
|
||||
assertSumEqual[N](t, N(count/expectedSingleCount)*expectedSingleSum, sum)
|
||||
} else {
|
||||
// Individual attributes should have exactly one goroutine's worth of data
|
||||
assert.Equal(t, expectedSingleSum, sum)
|
||||
@@ -1256,3 +1257,36 @@ func testExpoHistConcurrentSafeEdgeCases[N int64 | float64](temporality metricda
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpoHistogramRecordUnderflow(t *testing.T) {
|
||||
var errs []error
|
||||
original := global.GetErrorHandler()
|
||||
global.SetErrorHandler(otel.ErrorHandlerFunc(func(e error) {
|
||||
errs = append(errs, e)
|
||||
}))
|
||||
t.Cleanup(func() {
|
||||
global.SetErrorHandler(original)
|
||||
})
|
||||
|
||||
dp := newExpoHistogramDataPoint[float64](attribute.NewSet(), 1, 20, false, false)
|
||||
// Force scale to a low value
|
||||
dp.scale.Store(-10)
|
||||
dp.record(1)
|
||||
dp.record(math.MaxFloat64)
|
||||
require.Len(t, errs, 1)
|
||||
assert.EqualError(t, errs[0], "exponential histogram scale underflow")
|
||||
}
|
||||
|
||||
func TestDeltaExpoHistogramMeasureNaNAndInf(t *testing.T) {
|
||||
h := newExponentialHistogram[float64](4, 20, false, false, 0, dropExemplars[float64])
|
||||
ctx := t.Context()
|
||||
|
||||
h.measure(ctx, math.NaN(), attribute.NewSet(), nil)
|
||||
h.measure(ctx, math.Inf(1), attribute.NewSet(), nil)
|
||||
h.measure(ctx, math.Inf(-1), attribute.NewSet(), nil)
|
||||
|
||||
var dest metricdata.Aggregation
|
||||
h.delta(&dest)
|
||||
eh := dest.(metricdata.ExponentialHistogram[float64])
|
||||
assert.Empty(t, eh.DataPoints)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user