Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix guard of measured value to not record empty #4452

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions sdk/metric/internal/aggregate/exponential_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ func newExpoHistogramDataPoint[N int64 | float64](maxSize, maxScale int, noMinMa

// record adds a new measurement to the histogram. It will rescale the buckets if needed.
func (p *expoHistogramDataPoint[N]) record(v N) {
// Ignore NaN and infinity.
if math.IsInf(float64(v), 0) || math.IsNaN(float64(v)) {
return
}

p.count++

if !p.noMinMax {
Expand Down Expand Up @@ -321,6 +316,11 @@ type expoHistogram[N int64 | float64] struct {
}

func (e *expoHistogram[N]) measure(_ context.Context, value N, attr attribute.Set) {
// Ignore NaN and infinity.
if math.IsInf(float64(value), 0) || math.IsNaN(float64(value)) {
return
}

e.valuesMu.Lock()
defer e.valuesMu.Unlock()

Expand Down
18 changes: 10 additions & 8 deletions sdk/metric/internal/aggregate/exponential_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func withHandler(t *testing.T) func() {

func TestExpoHistogramDataPointRecord(t *testing.T) {
t.Run("float64", testExpoHistogramDataPointRecord[float64])
t.Run("float64 MinMaxSum", testExpoHistogramDataPointRecordMinMaxSumFloat64)
t.Run("float64 MinMaxSum", testExpoHistogramMinMaxSumFloat64)
t.Run("float64-2", testExpoHistogramDataPointRecordFloat64)
t.Run("int64", testExpoHistogramDataPointRecord[int64])
t.Run("int64 MinMaxSum", testExpoHistogramDataPointRecordMinMaxSumInt64)
t.Run("int64 MinMaxSum", testExpoHistogramMinMaxSumInt64)
}

// TODO: This can be defined in the test after we drop support for go1.19.
Expand Down Expand Up @@ -171,7 +171,7 @@ type expoHistogramDataPointRecordMinMaxSumTestCase[N int64 | float64] struct {
expected expectedMinMaxSum[N]
}

func testExpoHistogramDataPointRecordMinMaxSumInt64(t *testing.T) {
func testExpoHistogramMinMaxSumInt64(t *testing.T) {
testCases := []expoHistogramDataPointRecordMinMaxSumTestCase[int64]{
{
values: []int64{2, 4, 1},
Expand All @@ -188,10 +188,11 @@ func testExpoHistogramDataPointRecordMinMaxSumInt64(t *testing.T) {
restore := withHandler(t)
defer restore()

dp := newExpoHistogramDataPoint[int64](4, 20, false, false)
h := newExponentialHistogram[int64](4, 20, false, false)
for _, v := range tt.values {
dp.record(v)
h.measure(context.Background(), v, alice)
}
dp := h.values[alice]

assert.Equal(t, tt.expected.max, dp.max)
assert.Equal(t, tt.expected.min, dp.min)
Expand All @@ -200,7 +201,7 @@ func testExpoHistogramDataPointRecordMinMaxSumInt64(t *testing.T) {
}
}

func testExpoHistogramDataPointRecordMinMaxSumFloat64(t *testing.T) {
func testExpoHistogramMinMaxSumFloat64(t *testing.T) {
testCases := []expoHistogramDataPointRecordMinMaxSumTestCase[float64]{
{
values: []float64{2, 4, 1},
Expand Down Expand Up @@ -229,10 +230,11 @@ func testExpoHistogramDataPointRecordMinMaxSumFloat64(t *testing.T) {
restore := withHandler(t)
defer restore()

dp := newExpoHistogramDataPoint[float64](4, 20, false, false)
h := newExponentialHistogram[float64](4, 20, false, false)
for _, v := range tt.values {
dp.record(v)
h.measure(context.Background(), v, alice)
}
dp := h.values[alice]

assert.Equal(t, tt.expected.max, dp.max)
assert.Equal(t, tt.expected.min, dp.min)
Expand Down