| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package aggregate |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"math" |
| 10 |
"sync" |
| 11 |
"sync/atomic" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/internal/global" |
| 19 |
"go.opentelemetry.io/otel/sdk/internal/x" |
| 20 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 21 |
) |
| 22 |
|
| 23 |
type noErrorHandler struct{ t *testing.T } |
| 24 |
|
| 25 |
func (h *noErrorHandler) Handle(e error) { |
| 26 |
require.NoError(h.t, e) |
| 27 |
} |
| 28 |
|
| 29 |
func withHandler(t *testing.T) func() { |
| 30 |
t.Helper() |
| 31 |
h := &noErrorHandler{t: t} |
| 32 |
original := global.GetErrorHandler() |
| 33 |
global.SetErrorHandler(h) |
| 34 |
return func() { global.SetErrorHandler(original) } |
| 35 |
} |
| 36 |
|
| 37 |
func TestExpoHistogramDataPointRecord(t *testing.T) { |
| 38 |
t.Run("float64", testExpoHistogramDataPointRecord[float64]) |
| 39 |
t.Run("float64 MinMaxSum", testExpoHistogramMinMaxSumFloat64) |
| 40 |
t.Run("float64-2", testExpoHistogramDataPointRecordFloat64) |
| 41 |
t.Run("int64", testExpoHistogramDataPointRecord[int64]) |
| 42 |
t.Run("int64 MinMaxSum", testExpoHistogramMinMaxSumInt64) |
| 43 |
} |
| 44 |
|
| 45 |
func testExpoHistogramDataPointRecord[N int64 | float64](t *testing.T) { |
| 46 |
testCases := []struct { |
| 47 |
maxSize int |
| 48 |
values []N |
| 49 |
expectedStart int32 |
| 50 |
expectedCounts []uint64 |
| 51 |
expectedScale int32 |
| 52 |
}{ |
| 53 |
{ |
| 54 |
maxSize: 4, |
| 55 |
values: []N{2, 4, 1}, |
| 56 |
expectedStart: -1, |
| 57 |
expectedCounts: []uint64{1, 1, 1}, |
| 58 |
expectedScale: 0, |
| 59 |
}, |
| 60 |
{ |
| 61 |
maxSize: 4, |
| 62 |
values: []N{4, 4, 4, 2, 16, 1}, |
| 63 |
expectedStart: -1, |
| 64 |
expectedCounts: []uint64{1, 4, 1}, |
| 65 |
expectedScale: -1, |
| 66 |
}, |
| 67 |
{ |
| 68 |
maxSize: 2, |
| 69 |
values: []N{1, 2, 4}, |
| 70 |
expectedStart: -1, |
| 71 |
expectedCounts: []uint64{1, 2}, |
| 72 |
expectedScale: -1, |
| 73 |
}, |
| 74 |
{ |
| 75 |
maxSize: 2, |
| 76 |
values: []N{1, 4, 2}, |
| 77 |
expectedStart: -1, |
| 78 |
expectedCounts: []uint64{1, 2}, |
| 79 |
expectedScale: -1, |
| 80 |
}, |
| 81 |
{ |
| 82 |
maxSize: 2, |
| 83 |
values: []N{2, 4, 1}, |
| 84 |
expectedStart: -1, |
| 85 |
expectedCounts: []uint64{1, 2}, |
| 86 |
expectedScale: -1, |
| 87 |
}, |
| 88 |
{ |
| 89 |
maxSize: 2, |
| 90 |
values: []N{2, 1, 4}, |
| 91 |
expectedStart: -1, |
| 92 |
expectedCounts: []uint64{1, 2}, |
| 93 |
expectedScale: -1, |
| 94 |
}, |
| 95 |
{ |
| 96 |
maxSize: 2, |
| 97 |
values: []N{4, 1, 2}, |
| 98 |
expectedStart: -1, |
| 99 |
expectedCounts: []uint64{1, 2}, |
| 100 |
expectedScale: -1, |
| 101 |
}, |
| 102 |
{ |
| 103 |
maxSize: 2, |
| 104 |
values: []N{4, 2, 1}, |
| 105 |
expectedStart: -1, |
| 106 |
expectedCounts: []uint64{1, 2}, |
| 107 |
expectedScale: -1, |
| 108 |
}, |
| 109 |
} |
| 110 |
for _, tt := range testCases { |
| 111 |
t.Run(fmt.Sprint(tt.values), func(t *testing.T) { |
| 112 |
restore := withHandler(t) |
| 113 |
defer restore() |
| 114 |
|
| 115 |
dp := newExpoHistogramDataPoint[N](alice, tt.maxSize, 20, false, false) |
| 116 |
for _, v := range tt.values { |
| 117 |
dp.record(v) |
| 118 |
dp.record(-v) |
| 119 |
} |
| 120 |
|
| 121 |
assertBuckets(t, tt.expectedStart, tt.expectedCounts, dp.posBuckets, "positive buckets") |
| 122 |
assertBuckets(t, tt.expectedStart, tt.expectedCounts, dp.negBuckets, "negative buckets") |
| 123 |
assert.Equal(t, tt.expectedScale, dp.scale.Load(), "scale") |
| 124 |
}) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// TODO: This can be defined in the test after we drop support for go1.19. |
| 129 |
type expectedMinMaxSum[N int64 | float64] struct { |
| 130 |
min N |
| 131 |
max N |
| 132 |
sum N |
| 133 |
count uint |
| 134 |
} |
| 135 |
type expoHistogramDataPointRecordMinMaxSumTestCase[N int64 | float64] struct { |
| 136 |
values []N |
| 137 |
expected expectedMinMaxSum[N] |
| 138 |
} |
| 139 |
|
| 140 |
func testExpoHistogramMinMaxSumInt64(t *testing.T) { |
| 141 |
testCases := []expoHistogramDataPointRecordMinMaxSumTestCase[int64]{ |
| 142 |
{ |
| 143 |
values: []int64{2, 4, 1}, |
| 144 |
expected: expectedMinMaxSum[int64]{1, 4, 7, 3}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
values: []int64{4, 4, 4, 2, 16, 1}, |
| 148 |
expected: expectedMinMaxSum[int64]{1, 16, 31, 6}, |
| 149 |
}, |
| 150 |
} |
| 151 |
|
| 152 |
for _, tt := range testCases { |
| 153 |
t.Run(fmt.Sprint(tt.values), func(t *testing.T) { |
| 154 |
restore := withHandler(t) |
| 155 |
defer restore() |
| 156 |
|
| 157 |
h := newExponentialHistogram[int64](4, 20, false, false, 0, dropExemplars[int64]) |
| 158 |
for _, v := range tt.values { |
| 159 |
h.measure(t.Context(), v, alice, nil) |
| 160 |
} |
| 161 |
dp := h.values[alice.Equivalent()] |
| 162 |
|
| 163 |
assert.Equal(t, tt.expected.max, dp.minMax.maximum.Load()) |
| 164 |
assert.Equal(t, tt.expected.min, dp.minMax.minimum.Load()) |
| 165 |
assert.InDelta(t, tt.expected.sum, dp.sum.load(), 0.01) |
| 166 |
}) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func testExpoHistogramMinMaxSumFloat64(t *testing.T) { |
| 171 |
testCases := []expoHistogramDataPointRecordMinMaxSumTestCase[float64]{ |
| 172 |
{ |
| 173 |
values: []float64{2, 4, 1}, |
| 174 |
expected: expectedMinMaxSum[float64]{1, 4, 7, 3}, |
| 175 |
}, |
| 176 |
{ |
| 177 |
values: []float64{2, 4, 1, math.Inf(1)}, |
| 178 |
expected: expectedMinMaxSum[float64]{1, 4, 7, 4}, |
| 179 |
}, |
| 180 |
{ |
| 181 |
values: []float64{2, 4, 1, math.Inf(-1)}, |
| 182 |
expected: expectedMinMaxSum[float64]{1, 4, 7, 4}, |
| 183 |
}, |
| 184 |
{ |
| 185 |
values: []float64{2, 4, 1, math.NaN()}, |
| 186 |
expected: expectedMinMaxSum[float64]{1, 4, 7, 4}, |
| 187 |
}, |
| 188 |
{ |
| 189 |
values: []float64{4, 4, 4, 2, 16, 1}, |
| 190 |
expected: expectedMinMaxSum[float64]{1, 16, 31, 6}, |
| 191 |
}, |
| 192 |
} |
| 193 |
|
| 194 |
for _, tt := range testCases { |
| 195 |
t.Run(fmt.Sprint(tt.values), func(t *testing.T) { |
| 196 |
restore := withHandler(t) |
| 197 |
defer restore() |
| 198 |
|
| 199 |
h := newExponentialHistogram[float64](4, 20, false, false, 0, dropExemplars[float64]) |
| 200 |
for _, v := range tt.values { |
| 201 |
h.measure(t.Context(), v, alice, nil) |
| 202 |
} |
| 203 |
dp := h.values[alice.Equivalent()] |
| 204 |
|
| 205 |
assert.Equal(t, tt.expected.max, dp.minMax.maximum.Load()) |
| 206 |
assert.Equal(t, tt.expected.min, dp.minMax.minimum.Load()) |
| 207 |
assert.InDelta(t, tt.expected.sum, dp.sum.load(), 0.01) |
| 208 |
}) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func testExpoHistogramDataPointRecordFloat64(t *testing.T) { |
| 213 |
type TestCase struct { |
| 214 |
maxSize int |
| 215 |
values []float64 |
| 216 |
expectedStart int32 |
| 217 |
expectedCounts []uint64 |
| 218 |
expectedScale int32 |
| 219 |
} |
| 220 |
|
| 221 |
testCases := []TestCase{ |
| 222 |
{ |
| 223 |
maxSize: 4, |
| 224 |
values: []float64{2, 2, 2, 1, 8, 0.5}, |
| 225 |
expectedStart: -1, |
| 226 |
expectedCounts: []uint64{2, 3, 1}, |
| 227 |
expectedScale: -1, |
| 228 |
}, |
| 229 |
{ |
| 230 |
maxSize: 2, |
| 231 |
values: []float64{1, 0.5, 2}, |
| 232 |
expectedStart: -1, |
| 233 |
expectedCounts: []uint64{2, 1}, |
| 234 |
expectedScale: -1, |
| 235 |
}, |
| 236 |
{ |
| 237 |
maxSize: 2, |
| 238 |
values: []float64{1, 2, 0.5}, |
| 239 |
expectedStart: -1, |
| 240 |
expectedCounts: []uint64{2, 1}, |
| 241 |
expectedScale: -1, |
| 242 |
}, |
| 243 |
{ |
| 244 |
maxSize: 2, |
| 245 |
values: []float64{2, 0.5, 1}, |
| 246 |
expectedStart: -1, |
| 247 |
expectedCounts: []uint64{2, 1}, |
| 248 |
expectedScale: -1, |
| 249 |
}, |
| 250 |
{ |
| 251 |
maxSize: 2, |
| 252 |
values: []float64{2, 1, 0.5}, |
| 253 |
expectedStart: -1, |
| 254 |
expectedCounts: []uint64{2, 1}, |
| 255 |
expectedScale: -1, |
| 256 |
}, |
| 257 |
{ |
| 258 |
maxSize: 2, |
| 259 |
values: []float64{0.5, 1, 2}, |
| 260 |
expectedStart: -1, |
| 261 |
expectedCounts: []uint64{2, 1}, |
| 262 |
expectedScale: -1, |
| 263 |
}, |
| 264 |
{ |
| 265 |
maxSize: 2, |
| 266 |
values: []float64{0.5, 2, 1}, |
| 267 |
expectedStart: -1, |
| 268 |
expectedCounts: []uint64{2, 1}, |
| 269 |
expectedScale: -1, |
| 270 |
}, |
| 271 |
} |
| 272 |
for _, tt := range testCases { |
| 273 |
t.Run(fmt.Sprint(tt.values), func(t *testing.T) { |
| 274 |
restore := withHandler(t) |
| 275 |
defer restore() |
| 276 |
|
| 277 |
dp := newExpoHistogramDataPoint[float64](alice, tt.maxSize, 20, false, false) |
| 278 |
for _, v := range tt.values { |
| 279 |
dp.record(v) |
| 280 |
dp.record(-v) |
| 281 |
} |
| 282 |
|
| 283 |
assertBuckets(t, tt.expectedStart, tt.expectedCounts, dp.posBuckets, "positive buckets") |
| 284 |
assertBuckets(t, tt.expectedStart, tt.expectedCounts, dp.negBuckets, "negative buckets") |
| 285 |
assert.Equal(t, tt.expectedScale, dp.scale.Load(), "scale") |
| 286 |
assert.Equal(t, tt.expectedScale, dp.scale.Load()) |
| 287 |
}) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func TestExponentialHistogramDataPointRecordLimits(t *testing.T) { |
| 292 |
// These bins are calculated from the following formula: |
| 293 |
// floor( log2( value) * 2^20 ) using an arbitrary precision calculator. |
| 294 |
|
| 295 |
fdp := newExpoHistogramDataPoint[float64](alice, 4, 20, false, false) |
| 296 |
fdp.record(math.MaxFloat64) |
| 297 |
|
| 298 |
if fdp.posBuckets.startBin != 1073741823 { |
| 299 |
t.Errorf("Expected startBin to be 1073741823, got %d", fdp.posBuckets.startBin) |
| 300 |
} |
| 301 |
|
| 302 |
fdp = newExpoHistogramDataPoint[float64](alice, 4, 20, false, false) |
| 303 |
fdp.record(math.SmallestNonzeroFloat64) |
| 304 |
|
| 305 |
if fdp.posBuckets.startBin != -1126170625 { |
| 306 |
t.Errorf("Expected startBin to be -1126170625, got %d", fdp.posBuckets.startBin) |
| 307 |
} |
| 308 |
|
| 309 |
idp := newExpoHistogramDataPoint[int64](alice, 4, 20, false, false) |
| 310 |
idp.record(math.MaxInt64) |
| 311 |
|
| 312 |
if idp.posBuckets.startBin != 66060287 { |
| 313 |
t.Errorf("Expected startBin to be 66060287, got %d", idp.posBuckets.startBin) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
func newBucket(startBin int32, counts []uint64) *expoBuckets { |
| 318 |
b := &expoBuckets{startBin: startBin, counts: make([]atomic.Uint64, len(counts))} |
| 319 |
for i, v := range counts { |
| 320 |
b.counts[i].Store(v) |
| 321 |
} |
| 322 |
return b |
| 323 |
} |
| 324 |
|
| 325 |
func assertBuckets(t *testing.T, expectedStart int32, expectedCounts []uint64, actual expoBuckets, msg string) { |
| 326 |
t.Helper() |
| 327 |
assert.Equal(t, expectedStart, actual.startBin, "%s: startBin", msg) |
| 328 |
var actualCounts []uint64 |
| 329 |
if len(actual.counts) > 0 { |
| 330 |
actualCounts = make([]uint64, len(actual.counts)) |
| 331 |
for i := range actual.counts { |
| 332 |
actualCounts[i] = actual.counts[i].Load() |
| 333 |
} |
| 334 |
} |
| 335 |
assert.Equal(t, expectedCounts, actualCounts, "%s: counts", msg) |
| 336 |
} |
| 337 |
|
| 338 |
func TestExpoBucketDownscale(t *testing.T) { |
| 339 |
tests := []struct { |
| 340 |
name string |
| 341 |
bucket *expoBuckets |
| 342 |
scale int32 |
| 343 |
wantStart int32 |
| 344 |
wantCounts []uint64 |
| 345 |
}{ |
| 346 |
{ |
| 347 |
name: "Empty bucket", |
| 348 |
bucket: newBucket(0, nil), |
| 349 |
scale: 3, |
| 350 |
wantStart: 0, |
| 351 |
wantCounts: nil, |
| 352 |
}, |
| 353 |
{ |
| 354 |
name: "1 size bucket", |
| 355 |
bucket: newBucket(50, []uint64{7}), |
| 356 |
scale: 4, |
| 357 |
wantStart: 3, |
| 358 |
wantCounts: []uint64{7}, |
| 359 |
}, |
| 360 |
{ |
| 361 |
name: "zero scale", |
| 362 |
bucket: newBucket(50, []uint64{7, 5}), |
| 363 |
scale: 0, |
| 364 |
wantStart: 50, |
| 365 |
wantCounts: []uint64{7, 5}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
name: "aligned bucket scale 1", |
| 369 |
bucket: newBucket(0, []uint64{1, 2, 3, 4, 5, 6}), |
| 370 |
scale: 1, |
| 371 |
wantStart: 0, |
| 372 |
wantCounts: []uint64{3, 7, 11}, |
| 373 |
}, |
| 374 |
{ |
| 375 |
name: "aligned bucket scale 2", |
| 376 |
bucket: newBucket(0, []uint64{1, 2, 3, 4, 5, 6}), |
| 377 |
scale: 2, |
| 378 |
wantStart: 0, |
| 379 |
wantCounts: []uint64{10, 11}, |
| 380 |
}, |
| 381 |
{ |
| 382 |
name: "aligned bucket scale 3", |
| 383 |
bucket: newBucket(0, []uint64{1, 2, 3, 4, 5, 6}), |
| 384 |
scale: 3, |
| 385 |
wantStart: 0, |
| 386 |
wantCounts: []uint64{21}, |
| 387 |
}, |
| 388 |
{ |
| 389 |
name: "unaligned bucket scale 1", |
| 390 |
bucket: newBucket(5, []uint64{1, 2, 3, 4, 5, 6}), // This is equivalent to [0,0,0,0,0,1,2,3,4,5,6] |
| 391 |
scale: 1, |
| 392 |
wantStart: 2, |
| 393 |
wantCounts: []uint64{1, 5, 9, 6}, // This is equivalent to [0,0,1,5,9,6] |
| 394 |
}, |
| 395 |
{ |
| 396 |
name: "unaligned bucket scale 2", |
| 397 |
bucket: newBucket(7, []uint64{1, 2, 3, 4, 5, 6}), // This is equivalent to [0,0,0,0,0,0,0,1,2,3,4,5,6] |
| 398 |
scale: 2, |
| 399 |
wantStart: 1, |
| 400 |
wantCounts: []uint64{1, 14, 6}, // This is equivalent to [0,1,14,6] |
| 401 |
}, |
| 402 |
{ |
| 403 |
name: "unaligned bucket scale 3", |
| 404 |
bucket: newBucket(3, []uint64{1, 2, 3, 4, 5, 6}), // This is equivalent to [0,0,0,1,2,3,4,5,6] |
| 405 |
scale: 3, |
| 406 |
wantStart: 0, |
| 407 |
wantCounts: []uint64{15, 6}, // This is equivalent to [0,15,6] |
| 408 |
}, |
| 409 |
{ |
| 410 |
name: "unaligned bucket scale 1", |
| 411 |
bucket: newBucket(1, []uint64{1, 0, 1}), |
| 412 |
scale: 1, |
| 413 |
wantStart: 0, |
| 414 |
wantCounts: []uint64{1, 1}, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "negative startBin", |
| 418 |
bucket: newBucket(-1, []uint64{1, 0, 3}), |
| 419 |
scale: 1, |
| 420 |
wantStart: -1, |
| 421 |
wantCounts: []uint64{1, 3}, |
| 422 |
}, |
| 423 |
{ |
| 424 |
name: "negative startBin 2", |
| 425 |
bucket: newBucket(-4, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), |
| 426 |
scale: 1, |
| 427 |
wantStart: -2, |
| 428 |
wantCounts: []uint64{3, 7, 11, 15, 19}, |
| 429 |
}, |
| 430 |
} |
| 431 |
for _, tt := range tests { |
| 432 |
t.Run(tt.name, func(t *testing.T) { |
| 433 |
tt.bucket.downscale(tt.scale) |
| 434 |
|
| 435 |
assertBuckets(t, tt.wantStart, tt.wantCounts, *tt.bucket, tt.name) |
| 436 |
}) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
func TestExpoBucketRecord(t *testing.T) { |
| 441 |
tests := []struct { |
| 442 |
name string |
| 443 |
bucket *expoBuckets |
| 444 |
bin int32 |
| 445 |
wantStart int32 |
| 446 |
wantCounts []uint64 |
| 447 |
}{ |
| 448 |
{ |
| 449 |
name: "Empty Bucket creates first count", |
| 450 |
bucket: newBucket(0, nil), |
| 451 |
bin: -5, |
| 452 |
wantStart: -5, |
| 453 |
wantCounts: []uint64{1}, |
| 454 |
}, |
| 455 |
{ |
| 456 |
name: "Bin is in the bucket", |
| 457 |
bucket: newBucket(3, []uint64{1, 2, 3, 4, 5, 6}), |
| 458 |
bin: 5, |
| 459 |
wantStart: 3, |
| 460 |
wantCounts: []uint64{1, 2, 4, 4, 5, 6}, |
| 461 |
}, |
| 462 |
{ |
| 463 |
name: "Bin is before the start of the bucket", |
| 464 |
bucket: newBucket(1, []uint64{1, 2, 3, 4, 5, 6}), |
| 465 |
bin: -2, |
| 466 |
wantStart: -2, |
| 467 |
wantCounts: []uint64{1, 0, 0, 1, 2, 3, 4, 5, 6}, |
| 468 |
}, |
| 469 |
{ |
| 470 |
name: "Bin is after the end of the bucket", |
| 471 |
bucket: newBucket(-2, []uint64{1, 2, 3, 4, 5, 6}), |
| 472 |
bin: 4, |
| 473 |
wantStart: -2, |
| 474 |
wantCounts: []uint64{1, 2, 3, 4, 5, 6, 1}, |
| 475 |
}, |
| 476 |
} |
| 477 |
for _, tt := range tests { |
| 478 |
t.Run(tt.name, func(t *testing.T) { |
| 479 |
tt.bucket.record(tt.bin) |
| 480 |
|
| 481 |
assertBuckets(t, tt.wantStart, tt.wantCounts, *tt.bucket, tt.name) |
| 482 |
}) |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
func TestScaleChange(t *testing.T) { |
| 487 |
type args struct { |
| 488 |
bin int32 |
| 489 |
startBin int32 |
| 490 |
length int |
| 491 |
maxSize int |
| 492 |
} |
| 493 |
tests := []struct { |
| 494 |
name string |
| 495 |
args args |
| 496 |
want int32 |
| 497 |
}{ |
| 498 |
{ |
| 499 |
name: "if length is 0, no rescale is needed", |
| 500 |
// [] -> [5] Length 1 |
| 501 |
args: args{ |
| 502 |
bin: 5, |
| 503 |
startBin: 0, |
| 504 |
length: 0, |
| 505 |
maxSize: 4, |
| 506 |
}, |
| 507 |
want: 0, |
| 508 |
}, |
| 509 |
{ |
| 510 |
name: "if bin is between start, and the end, no rescale needed", |
| 511 |
// [-1, ..., 8] Length 10 -> [-1, ..., 5, ..., 8] Length 10 |
| 512 |
args: args{ |
| 513 |
bin: 5, |
| 514 |
startBin: -1, |
| 515 |
length: 10, |
| 516 |
maxSize: 20, |
| 517 |
}, |
| 518 |
want: 0, |
| 519 |
}, |
| 520 |
{ |
| 521 |
name: "if len([bin,... end]) > maxSize, rescale needed", |
| 522 |
// [8,9,10] Length 3 -> [5, ..., 10] Length 6 |
| 523 |
args: args{ |
| 524 |
bin: 5, |
| 525 |
startBin: 8, |
| 526 |
length: 3, |
| 527 |
maxSize: 5, |
| 528 |
}, |
| 529 |
want: 1, |
| 530 |
}, |
| 531 |
{ |
| 532 |
name: "if len([start, ..., bin]) > maxSize, rescale needed", |
| 533 |
// [2,3,4] Length 3 -> [2, ..., 7] Length 6 |
| 534 |
args: args{ |
| 535 |
bin: 7, |
| 536 |
startBin: 2, |
| 537 |
length: 3, |
| 538 |
maxSize: 5, |
| 539 |
}, |
| 540 |
want: 1, |
| 541 |
}, |
| 542 |
{ |
| 543 |
name: "if len([start, ..., bin]) > maxSize, rescale needed", |
| 544 |
// [2,3,4] Length 3 -> [2, ..., 7] Length 12 |
| 545 |
args: args{ |
| 546 |
bin: 13, |
| 547 |
startBin: 2, |
| 548 |
length: 3, |
| 549 |
maxSize: 5, |
| 550 |
}, |
| 551 |
want: 2, |
| 552 |
}, |
| 553 |
{ |
| 554 |
name: "It should not hang if it will never be able to rescale", |
| 555 |
args: args{ |
| 556 |
bin: 1, |
| 557 |
startBin: -1, |
| 558 |
length: 1, |
| 559 |
maxSize: 1, |
| 560 |
}, |
| 561 |
want: 31, |
| 562 |
}, |
| 563 |
} |
| 564 |
for _, tt := range tests { |
| 565 |
t.Run(tt.name, func(t *testing.T) { |
| 566 |
p := newExpoHistogramDataPoint[float64](alice, tt.args.maxSize, 20, false, false) |
| 567 |
got := p.scaleChange(tt.args.bin, tt.args.startBin, tt.args.length) |
| 568 |
if got != tt.want { |
| 569 |
t.Errorf("scaleChange() = %v, want %v", got, tt.want) |
| 570 |
} |
| 571 |
}) |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
func BenchmarkPrepend(b *testing.B) { |
| 576 |
for i := 0; i < b.N; i++ { |
| 577 |
agg := newExpoHistogramDataPoint[float64](alice, 1024, 20, false, false) |
| 578 |
n := math.MaxFloat64 |
| 579 |
for range 1024 { |
| 580 |
agg.record(n) |
| 581 |
n /= 2 |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
func BenchmarkAppend(b *testing.B) { |
| 587 |
for i := 0; i < b.N; i++ { |
| 588 |
agg := newExpoHistogramDataPoint[float64](alice, 1024, 20, false, false) |
| 589 |
n := smallestNonZeroNormalFloat64 |
| 590 |
for range 1024 { |
| 591 |
agg.record(n) |
| 592 |
n *= 2 |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
func BenchmarkExponentialHistogram(b *testing.B) { |
| 598 |
const ( |
| 599 |
maxSize = 160 |
| 600 |
maxScale = 20 |
| 601 |
noMinMax = false |
| 602 |
noSum = false |
| 603 |
) |
| 604 |
|
| 605 |
b.Run("Int64/Cumulative", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) { |
| 606 |
return Builder[int64]{ |
| 607 |
Temporality: metricdata.CumulativeTemporality, |
| 608 |
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) |
| 609 |
})) |
| 610 |
b.Run("Int64/Delta", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) { |
| 611 |
return Builder[int64]{ |
| 612 |
Temporality: metricdata.DeltaTemporality, |
| 613 |
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) |
| 614 |
})) |
| 615 |
b.Run("Float64/Cumulative", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) { |
| 616 |
return Builder[float64]{ |
| 617 |
Temporality: metricdata.CumulativeTemporality, |
| 618 |
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) |
| 619 |
})) |
| 620 |
b.Run("Float64/Delta", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) { |
| 621 |
return Builder[float64]{ |
| 622 |
Temporality: metricdata.DeltaTemporality, |
| 623 |
}.ExponentialBucketHistogram(maxSize, maxScale, noMinMax, noSum) |
| 624 |
})) |
| 625 |
} |
| 626 |
|
| 627 |
func TestSubNormal(t *testing.T) { |
| 628 |
want := &expoHistogramDataPoint[float64]{ |
| 629 |
attrs: alice, |
| 630 |
maxSize: 4, |
| 631 |
} |
| 632 |
want.minMax.Update(math.SmallestNonzeroFloat64) |
| 633 |
want.sum.add(3 * math.SmallestNonzeroFloat64) |
| 634 |
want.scale.Store(20) |
| 635 |
want.posBuckets = *newBucket(-1126170625, []uint64{3}) |
| 636 |
|
| 637 |
ehdp := newExpoHistogramDataPoint[float64](alice, 4, 20, false, false) |
| 638 |
ehdp.record(math.SmallestNonzeroFloat64) |
| 639 |
ehdp.record(math.SmallestNonzeroFloat64) |
| 640 |
ehdp.record(math.SmallestNonzeroFloat64) |
| 641 |
|
| 642 |
want.startTime = ehdp.startTime |
| 643 |
|
| 644 |
assert.Equal(t, want, ehdp) |
| 645 |
} |
| 646 |
|
| 647 |
func TestExponentialHistogramAggregation(t *testing.T) { |
| 648 |
c := new(clock) |
| 649 |
t.Cleanup(c.Register()) |
| 650 |
|
| 651 |
t.Run("Int64/Delta", testDeltaExpoHist[int64]()) |
| 652 |
c.Reset() |
| 653 |
|
| 654 |
t.Run("Float64/Delta", testDeltaExpoHist[float64]()) |
| 655 |
c.Reset() |
| 656 |
|
| 657 |
t.Run("Int64/Cumulative", func(t *testing.T) { |
| 658 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false") |
| 659 |
assert.False(t, x.PerSeriesStartTimestamps.Enabled()) |
| 660 |
testCumulativeExpoHist[int64]()(t) |
| 661 |
}) |
| 662 |
c.Reset() |
| 663 |
|
| 664 |
t.Run("Int64/Cumulative/PerSeriesStartTimeEnabled", func(t *testing.T) { |
| 665 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true") |
| 666 |
assert.True(t, x.PerSeriesStartTimestamps.Enabled()) |
| 667 |
testCumulativeExpoHist[int64]()(t) |
| 668 |
}) |
| 669 |
c.Reset() |
| 670 |
|
| 671 |
t.Run("Float64/Cumulative", func(t *testing.T) { |
| 672 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false") |
| 673 |
assert.False(t, x.PerSeriesStartTimestamps.Enabled()) |
| 674 |
testCumulativeExpoHist[float64]()(t) |
| 675 |
}) |
| 676 |
c.Reset() |
| 677 |
|
| 678 |
t.Run("Float64/Cumulative/PerSeriesStartTimeEnabled", func(t *testing.T) { |
| 679 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true") |
| 680 |
assert.True(t, x.PerSeriesStartTimestamps.Enabled()) |
| 681 |
testCumulativeExpoHist[float64]()(t) |
| 682 |
}) |
| 683 |
c.Reset() |
| 684 |
} |
| 685 |
|
| 686 |
func testDeltaExpoHist[N int64 | float64]() func(t *testing.T) { |
| 687 |
in, out := Builder[N]{ |
| 688 |
Temporality: metricdata.DeltaTemporality, |
| 689 |
Filter: attrFltr, |
| 690 |
AggregationLimit: 2, |
| 691 |
}.ExponentialBucketHistogram(4, 20, false, false) |
| 692 |
ctx := context.Background() |
| 693 |
return test[N](in, out, []teststep[N]{ |
| 694 |
{ |
| 695 |
input: []arg[N]{}, |
| 696 |
expect: output{ |
| 697 |
n: 0, |
| 698 |
agg: metricdata.ExponentialHistogram[N]{ |
| 699 |
Temporality: metricdata.DeltaTemporality, |
| 700 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{}, |
| 701 |
}, |
| 702 |
}, |
| 703 |
}, |
| 704 |
{ |
| 705 |
input: []arg[N]{ |
| 706 |
{ctx, 4, alice}, |
| 707 |
{ctx, 4, alice}, |
| 708 |
{ctx, 4, alice}, |
| 709 |
{ctx, 2, alice}, |
| 710 |
{ctx, 16, alice}, |
| 711 |
{ctx, 1, alice}, |
| 712 |
{ctx, -1, alice}, |
| 713 |
}, |
| 714 |
expect: output{ |
| 715 |
n: 1, |
| 716 |
agg: metricdata.ExponentialHistogram[N]{ |
| 717 |
Temporality: metricdata.DeltaTemporality, |
| 718 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ |
| 719 |
{ |
| 720 |
Attributes: fltrAlice, |
| 721 |
StartTime: y2kPlus(1), |
| 722 |
Time: y2kPlus(3), |
| 723 |
Count: 7, |
| 724 |
Min: metricdata.NewExtrema[N](-1), |
| 725 |
Max: metricdata.NewExtrema[N](16), |
| 726 |
Sum: 30, |
| 727 |
Scale: -1, |
| 728 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 729 |
Offset: -1, |
| 730 |
Counts: []uint64{1, 4, 1}, |
| 731 |
}, |
| 732 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 733 |
Offset: -1, |
| 734 |
Counts: []uint64{1}, |
| 735 |
}, |
| 736 |
}, |
| 737 |
}, |
| 738 |
}, |
| 739 |
}, |
| 740 |
}, |
| 741 |
{ |
| 742 |
// Delta sums are expected to reset. |
| 743 |
input: []arg[N]{}, |
| 744 |
expect: output{ |
| 745 |
n: 0, |
| 746 |
agg: metricdata.ExponentialHistogram[N]{ |
| 747 |
Temporality: metricdata.DeltaTemporality, |
| 748 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{}, |
| 749 |
}, |
| 750 |
}, |
| 751 |
}, |
| 752 |
{ |
| 753 |
input: []arg[N]{ |
| 754 |
{ctx, 4, alice}, |
| 755 |
{ctx, 4, alice}, |
| 756 |
{ctx, 4, alice}, |
| 757 |
{ctx, 2, alice}, |
| 758 |
{ctx, 16, alice}, |
| 759 |
{ctx, 1, alice}, |
| 760 |
// These will exceed the cardinality limit. |
| 761 |
{ctx, 4, bob}, |
| 762 |
{ctx, 4, bob}, |
| 763 |
{ctx, 4, bob}, |
| 764 |
{ctx, 2, carol}, |
| 765 |
{ctx, 16, carol}, |
| 766 |
{ctx, 1, dave}, |
| 767 |
{ctx, -1, alice}, |
| 768 |
}, |
| 769 |
expect: output{ |
| 770 |
n: 2, |
| 771 |
agg: metricdata.ExponentialHistogram[N]{ |
| 772 |
Temporality: metricdata.DeltaTemporality, |
| 773 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ |
| 774 |
{ |
| 775 |
Attributes: fltrAlice, |
| 776 |
StartTime: y2kPlus(4), |
| 777 |
Time: y2kPlus(7), |
| 778 |
Count: 7, |
| 779 |
Min: metricdata.NewExtrema[N](-1), |
| 780 |
Max: metricdata.NewExtrema[N](16), |
| 781 |
Sum: 30, |
| 782 |
Scale: -1, |
| 783 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 784 |
Offset: -1, |
| 785 |
Counts: []uint64{1, 4, 1}, |
| 786 |
}, |
| 787 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 788 |
Offset: -1, |
| 789 |
Counts: []uint64{1}, |
| 790 |
}, |
| 791 |
}, |
| 792 |
{ |
| 793 |
Attributes: overflowSet, |
| 794 |
StartTime: y2kPlus(4), |
| 795 |
Time: y2kPlus(7), |
| 796 |
Count: 6, |
| 797 |
Min: metricdata.NewExtrema[N](1), |
| 798 |
Max: metricdata.NewExtrema[N](16), |
| 799 |
Sum: 31, |
| 800 |
Scale: -1, |
| 801 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 802 |
Offset: -1, |
| 803 |
Counts: []uint64{1, 4, 1}, |
| 804 |
}, |
| 805 |
}, |
| 806 |
}, |
| 807 |
}, |
| 808 |
}, |
| 809 |
}, |
| 810 |
}) |
| 811 |
} |
| 812 |
|
| 813 |
func testCumulativeExpoHist[N int64 | float64]() func(t *testing.T) { |
| 814 |
in, out := Builder[N]{ |
| 815 |
Temporality: metricdata.CumulativeTemporality, |
| 816 |
Filter: attrFltr, |
| 817 |
AggregationLimit: 2, |
| 818 |
}.ExponentialBucketHistogram(4, 20, false, false) |
| 819 |
|
| 820 |
aliceStartTime := y2kPlus(0) |
| 821 |
overflowStartTime := y2kPlus(0) |
| 822 |
|
| 823 |
if x.PerSeriesStartTimestamps.Enabled() { |
| 824 |
aliceStartTime = y2kPlus(2) |
| 825 |
overflowStartTime = y2kPlus(6) |
| 826 |
} |
| 827 |
|
| 828 |
ctx := context.Background() |
| 829 |
return test[N](in, out, []teststep[N]{ |
| 830 |
{ |
| 831 |
input: []arg[N]{}, |
| 832 |
expect: output{ |
| 833 |
n: 0, |
| 834 |
agg: metricdata.ExponentialHistogram[N]{ |
| 835 |
Temporality: metricdata.CumulativeTemporality, |
| 836 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{}, |
| 837 |
}, |
| 838 |
}, |
| 839 |
}, |
| 840 |
{ |
| 841 |
input: []arg[N]{ |
| 842 |
{ctx, 4, alice}, |
| 843 |
{ctx, 4, alice}, |
| 844 |
{ctx, 4, alice}, |
| 845 |
{ctx, 2, alice}, |
| 846 |
{ctx, 16, alice}, |
| 847 |
{ctx, 1, alice}, |
| 848 |
{ctx, -1, alice}, |
| 849 |
}, |
| 850 |
expect: output{ |
| 851 |
n: 1, |
| 852 |
agg: metricdata.ExponentialHistogram[N]{ |
| 853 |
Temporality: metricdata.CumulativeTemporality, |
| 854 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ |
| 855 |
{ |
| 856 |
Attributes: fltrAlice, |
| 857 |
StartTime: aliceStartTime, |
| 858 |
Time: y2kPlus(3), |
| 859 |
Count: 7, |
| 860 |
Min: metricdata.NewExtrema[N](-1), |
| 861 |
Max: metricdata.NewExtrema[N](16), |
| 862 |
Sum: 30, |
| 863 |
Scale: -1, |
| 864 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 865 |
Offset: -1, |
| 866 |
Counts: []uint64{1, 4, 1}, |
| 867 |
}, |
| 868 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 869 |
Offset: -1, |
| 870 |
Counts: []uint64{1}, |
| 871 |
}, |
| 872 |
}, |
| 873 |
}, |
| 874 |
}, |
| 875 |
}, |
| 876 |
}, |
| 877 |
{ |
| 878 |
input: []arg[N]{ |
| 879 |
{ctx, 2, alice}, |
| 880 |
{ctx, 3, alice}, |
| 881 |
{ctx, 8, alice}, |
| 882 |
}, |
| 883 |
expect: output{ |
| 884 |
n: 1, |
| 885 |
agg: metricdata.ExponentialHistogram[N]{ |
| 886 |
Temporality: metricdata.CumulativeTemporality, |
| 887 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ |
| 888 |
{ |
| 889 |
Attributes: fltrAlice, |
| 890 |
StartTime: aliceStartTime, |
| 891 |
Time: y2kPlus(4), |
| 892 |
Count: 10, |
| 893 |
Min: metricdata.NewExtrema[N](-1), |
| 894 |
Max: metricdata.NewExtrema[N](16), |
| 895 |
Sum: 43, |
| 896 |
Scale: -1, |
| 897 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 898 |
Offset: -1, |
| 899 |
Counts: []uint64{1, 6, 2}, |
| 900 |
}, |
| 901 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 902 |
Offset: -1, |
| 903 |
Counts: []uint64{1}, |
| 904 |
}, |
| 905 |
}, |
| 906 |
}, |
| 907 |
}, |
| 908 |
}, |
| 909 |
}, |
| 910 |
{ |
| 911 |
input: []arg[N]{}, |
| 912 |
expect: output{ |
| 913 |
n: 1, |
| 914 |
agg: metricdata.ExponentialHistogram[N]{ |
| 915 |
Temporality: metricdata.CumulativeTemporality, |
| 916 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ |
| 917 |
{ |
| 918 |
Attributes: fltrAlice, |
| 919 |
StartTime: aliceStartTime, |
| 920 |
Time: y2kPlus(5), |
| 921 |
Count: 10, |
| 922 |
Min: metricdata.NewExtrema[N](-1), |
| 923 |
Max: metricdata.NewExtrema[N](16), |
| 924 |
Sum: 43, |
| 925 |
Scale: -1, |
| 926 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 927 |
Offset: -1, |
| 928 |
Counts: []uint64{1, 6, 2}, |
| 929 |
}, |
| 930 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 931 |
Offset: -1, |
| 932 |
Counts: []uint64{1}, |
| 933 |
}, |
| 934 |
}, |
| 935 |
}, |
| 936 |
}, |
| 937 |
}, |
| 938 |
}, |
| 939 |
{ |
| 940 |
input: []arg[N]{ |
| 941 |
// These will exceed the cardinality limit. |
| 942 |
{ctx, 4, bob}, |
| 943 |
{ctx, 4, bob}, |
| 944 |
{ctx, 4, bob}, |
| 945 |
{ctx, 2, carol}, |
| 946 |
{ctx, 16, carol}, |
| 947 |
{ctx, 1, dave}, |
| 948 |
}, |
| 949 |
expect: output{ |
| 950 |
n: 2, |
| 951 |
agg: metricdata.ExponentialHistogram[N]{ |
| 952 |
Temporality: metricdata.CumulativeTemporality, |
| 953 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{ |
| 954 |
{ |
| 955 |
Attributes: fltrAlice, |
| 956 |
StartTime: aliceStartTime, |
| 957 |
Time: y2kPlus(7), |
| 958 |
Count: 10, |
| 959 |
Min: metricdata.NewExtrema[N](-1), |
| 960 |
Max: metricdata.NewExtrema[N](16), |
| 961 |
Sum: 43, |
| 962 |
Scale: -1, |
| 963 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 964 |
Offset: -1, |
| 965 |
Counts: []uint64{1, 6, 2}, |
| 966 |
}, |
| 967 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 968 |
Offset: -1, |
| 969 |
Counts: []uint64{1}, |
| 970 |
}, |
| 971 |
}, |
| 972 |
{ |
| 973 |
Attributes: overflowSet, |
| 974 |
StartTime: overflowStartTime, |
| 975 |
Time: y2kPlus(7), |
| 976 |
Count: 6, |
| 977 |
Min: metricdata.NewExtrema[N](1), |
| 978 |
Max: metricdata.NewExtrema[N](16), |
| 979 |
Sum: 31, |
| 980 |
Scale: -1, |
| 981 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 982 |
Offset: -1, |
| 983 |
Counts: []uint64{1, 4, 1}, |
| 984 |
}, |
| 985 |
}, |
| 986 |
}, |
| 987 |
}, |
| 988 |
}, |
| 989 |
}, |
| 990 |
}) |
| 991 |
} |
| 992 |
|
| 993 |
func TestExponentialHistogramAggregationConcurrentSafe(t *testing.T) { |
| 994 |
t.Run("Int64/Delta", testDeltaExpoHistConcurrentSafe[int64]()) |
| 995 |
t.Run("Float64/Delta", testDeltaExpoHistConcurrentSafe[float64]()) |
| 996 |
t.Run("Int64/Cumulative", testCumulativeExpoHistConcurrentSafe[int64]()) |
| 997 |
t.Run("Float64/Cumulative", testCumulativeExpoHistConcurrentSafe[float64]()) |
| 998 |
} |
| 999 |
|
| 1000 |
func testDeltaExpoHistConcurrentSafe[N int64 | float64]() func(t *testing.T) { |
| 1001 |
in, out := Builder[N]{ |
| 1002 |
Temporality: metricdata.DeltaTemporality, |
| 1003 |
Filter: attrFltr, |
| 1004 |
AggregationLimit: 3, |
| 1005 |
}.ExponentialBucketHistogram(4, 20, false, false) |
| 1006 |
return testAggregationConcurrentSafe[N](in, out, validateExponentialHistogram[N]) |
| 1007 |
} |
| 1008 |
|
| 1009 |
func testCumulativeExpoHistConcurrentSafe[N int64 | float64]() func(t *testing.T) { |
| 1010 |
in, out := Builder[N]{ |
| 1011 |
Temporality: metricdata.CumulativeTemporality, |
| 1012 |
Filter: attrFltr, |
| 1013 |
AggregationLimit: 3, |
| 1014 |
}.ExponentialBucketHistogram(4, 20, false, false) |
| 1015 |
return testAggregationConcurrentSafe[N](in, out, validateExponentialHistogram[N]) |
| 1016 |
} |
| 1017 |
|
| 1018 |
func validateExponentialHistogram[N int64 | float64](t *testing.T, aggs []metricdata.Aggregation) { |
| 1019 |
sums := make(map[attribute.Set]N) |
| 1020 |
counts := make(map[attribute.Set]uint64) |
| 1021 |
var isDelta bool |
| 1022 |
for i, agg := range aggs { |
| 1023 |
s, ok := agg.(metricdata.ExponentialHistogram[N]) |
| 1024 |
require.True(t, ok) |
| 1025 |
if s.Temporality == metricdata.DeltaTemporality { |
| 1026 |
isDelta = true |
| 1027 |
} |
| 1028 |
require.LessOrEqual(t, len(s.DataPoints), 3, "AggregationLimit of 3 exceeded in a single cycle") |
| 1029 |
for _, dp := range s.DataPoints { |
| 1030 |
assert.False(t, |
| 1031 |
dp.Time.Before(dp.StartTime), |
| 1032 |
"Timestamp %v must not be before start time %v", dp.Time, dp.StartTime, |
| 1033 |
) |
| 1034 |
|
| 1035 |
if s.Temporality == metricdata.DeltaTemporality { |
| 1036 |
sums[dp.Attributes] += dp.Sum |
| 1037 |
counts[dp.Attributes] += dp.Count |
| 1038 |
} else if i == len(aggs)-1 { |
| 1039 |
sums[dp.Attributes] = dp.Sum |
| 1040 |
counts[dp.Attributes] = dp.Count |
| 1041 |
} |
| 1042 |
|
| 1043 |
var totalCount uint64 |
| 1044 |
for _, bc := range dp.PositiveBucket.Counts { |
| 1045 |
totalCount += bc |
| 1046 |
} |
| 1047 |
for _, bc := range dp.NegativeBucket.Counts { |
| 1048 |
totalCount += bc |
| 1049 |
} |
| 1050 |
assert.Equal(t, totalCount, dp.Count) |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
var totalSum N |
| 1055 |
var totalCount uint64 |
| 1056 |
for attr, sum := range sums { |
| 1057 |
totalSum += sum |
| 1058 |
count := counts[attr] |
| 1059 |
totalCount += count |
| 1060 |
|
| 1061 |
expectedSingleSum := expectedConcurrentSum[N]() / N(concurrentNumGoroutines) |
| 1062 |
expectedSingleCount := expectedConcurrentCount / uint64(concurrentNumGoroutines) |
| 1063 |
|
| 1064 |
if !isDelta { |
| 1065 |
if attr == overflowSet { |
| 1066 |
// The overflow set contains all the goroutines that didn't make the limit of 3 |
| 1067 |
assert.Equal(t, uint64(0), count%expectedSingleCount) |
| 1068 |
assert.Equal(t, count/expectedSingleCount*uint64(expectedSingleSum), uint64(sum)) |
| 1069 |
} else { |
| 1070 |
// Individual attributes should have exactly one goroutine's worth of data |
| 1071 |
assert.Equal(t, expectedSingleSum, sum) |
| 1072 |
assert.Equal(t, expectedSingleCount, count) |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
assertSumEqual[N](t, expectedConcurrentSum[N](), totalSum) |
| 1077 |
assert.Equal(t, expectedConcurrentCount, totalCount) |
| 1078 |
} |
| 1079 |
|
| 1080 |
func FuzzGetBin(f *testing.F) { |
| 1081 |
values := []float64{ |
| 1082 |
2.0, |
| 1083 |
0x1p35, |
| 1084 |
0x1.0000000000001p35, |
| 1085 |
0x1.fffffffffffffp34, |
| 1086 |
0x1p300, |
| 1087 |
0x1.0000000000001p300, |
| 1088 |
0x1.fffffffffffffp299, |
| 1089 |
} |
| 1090 |
scales := []int32{0, 15, -5} |
| 1091 |
|
| 1092 |
for _, s := range scales { |
| 1093 |
for _, v := range values { |
| 1094 |
f.Add(v, s) |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
f.Fuzz(func(t *testing.T, v float64, scale int32) { |
| 1099 |
// GetBin only works on positive values. |
| 1100 |
if math.Signbit(v) { |
| 1101 |
v *= -1 |
| 1102 |
} |
| 1103 |
// GetBin Doesn't work on zero. |
| 1104 |
if v == 0.0 { |
| 1105 |
t.Skip("skipping test for zero") |
| 1106 |
} |
| 1107 |
|
| 1108 |
p := newExpoHistogramDataPoint[float64](alice, 4, 20, false, false) |
| 1109 |
// scale range is -10 to 20. |
| 1110 |
scaleValue := (scale%31+31)%31 - 10 |
| 1111 |
p.scale.Store(scaleValue) |
| 1112 |
got := p.getBin(v) |
| 1113 |
if v <= lowerBound(got, scaleValue) { |
| 1114 |
t.Errorf( |
| 1115 |
"v=%x scale =%d had bin %d, but was below lower bound %x", |
| 1116 |
v, |
| 1117 |
scaleValue, |
| 1118 |
got, |
| 1119 |
lowerBound(got, scaleValue), |
| 1120 |
) |
| 1121 |
} |
| 1122 |
if v > lowerBound(got+1, scaleValue) { |
| 1123 |
t.Errorf( |
| 1124 |
"v=%x scale =%d had bin %d, but was above upper bound %x", |
| 1125 |
v, |
| 1126 |
scaleValue, |
| 1127 |
got, |
| 1128 |
lowerBound(got+1, scaleValue), |
| 1129 |
) |
| 1130 |
} |
| 1131 |
}) |
| 1132 |
} |
| 1133 |
|
| 1134 |
func lowerBound(index, scale int32) float64 { |
| 1135 |
// The lowerBound of the index of Math.SmallestNonzeroFloat64 at any scale |
| 1136 |
// is always rounded down to 0.0. |
| 1137 |
// For example lowerBound(getBin(Math.SmallestNonzeroFloat64, 7), 7) == 0.0 |
| 1138 |
// 2 ^ (index * 2 ^ (-scale)) |
| 1139 |
return math.Exp2(math.Ldexp(float64(index), -int(scale))) |
| 1140 |
} |
| 1141 |
|
| 1142 |
func TestExponentialHistogramConcurrentSafeEdgeCases(t *testing.T) { |
| 1143 |
t.Run("Int64/Delta", testExpoHistConcurrentSafeEdgeCases[int64](metricdata.DeltaTemporality)) |
| 1144 |
t.Run("Float64/Delta", testExpoHistConcurrentSafeEdgeCases[float64](metricdata.DeltaTemporality)) |
| 1145 |
t.Run("Int64/Cumulative", testExpoHistConcurrentSafeEdgeCases[int64](metricdata.CumulativeTemporality)) |
| 1146 |
t.Run("Float64/Cumulative", testExpoHistConcurrentSafeEdgeCases[float64](metricdata.CumulativeTemporality)) |
| 1147 |
} |
| 1148 |
|
| 1149 |
func testExpoHistConcurrentSafeEdgeCases[N int64 | float64](temporality metricdata.Temporality) func(t *testing.T) { |
| 1150 |
return func(t *testing.T) { |
| 1151 |
t.Run("ZeroValues", func(t *testing.T) { |
| 1152 |
meas, comp := Builder[N]{ |
| 1153 |
Temporality: temporality, |
| 1154 |
Filter: attrFltr, |
| 1155 |
AggregationLimit: 3, |
| 1156 |
}.ExponentialBucketHistogram(160, 20, false, false) |
| 1157 |
|
| 1158 |
ctx := t.Context() |
| 1159 |
var wg sync.WaitGroup |
| 1160 |
const numGoroutines = 10 |
| 1161 |
const numRecords = 100 |
| 1162 |
wg.Add(numGoroutines) |
| 1163 |
for range numGoroutines { |
| 1164 |
go func() { |
| 1165 |
defer wg.Done() |
| 1166 |
for range numRecords { |
| 1167 |
meas(ctx, 0, alice) |
| 1168 |
} |
| 1169 |
}() |
| 1170 |
} |
| 1171 |
wg.Wait() |
| 1172 |
|
| 1173 |
dest := new(metricdata.Aggregation) |
| 1174 |
comp(dest) |
| 1175 |
h := (*dest).(metricdata.ExponentialHistogram[N]) |
| 1176 |
require.Len(t, h.DataPoints, 1) |
| 1177 |
assert.Equal(t, uint64(numGoroutines*numRecords), h.DataPoints[0].ZeroCount) |
| 1178 |
assert.Equal(t, uint64(numGoroutines*numRecords), h.DataPoints[0].Count) |
| 1179 |
}) |
| 1180 |
|
| 1181 |
t.Run("RescalingStress", func(t *testing.T) { |
| 1182 |
meas, comp := Builder[N]{ |
| 1183 |
Temporality: temporality, |
| 1184 |
Filter: attrFltr, |
| 1185 |
AggregationLimit: 3, |
| 1186 |
}.ExponentialBucketHistogram(160, 20, false, false) |
| 1187 |
|
| 1188 |
ctx := t.Context() |
| 1189 |
var wg sync.WaitGroup |
| 1190 |
const numGoroutines = 10 |
| 1191 |
const numRecords = 100 |
| 1192 |
|
| 1193 |
// To verify exact outcome, we sequentially record the same to a reference. |
| 1194 |
refMeas, refComp := Builder[N]{ |
| 1195 |
Temporality: temporality, |
| 1196 |
Filter: attrFltr, |
| 1197 |
AggregationLimit: 3, |
| 1198 |
}.ExponentialBucketHistogram(160, 20, false, false) |
| 1199 |
var m sync.Mutex |
| 1200 |
|
| 1201 |
wg.Add(numGoroutines) |
| 1202 |
for i := range numGoroutines { |
| 1203 |
go func(id int) { |
| 1204 |
defer wg.Done() |
| 1205 |
for j := range numRecords { |
| 1206 |
// generate a mix of very large and very small powers of 2 |
| 1207 |
valFloat := math.Exp2((float64(j) / float64(numRecords)) * 60.0) |
| 1208 |
if id%2 == 0 { |
| 1209 |
valFloat = -valFloat |
| 1210 |
} |
| 1211 |
val := N(valFloat) |
| 1212 |
// For integers, values less than 1 will truncate to 0. Mix things up. |
| 1213 |
if id%3 == 0 { |
| 1214 |
val = N(float64(id+1) * 100.0) |
| 1215 |
} |
| 1216 |
|
| 1217 |
meas(ctx, val, alice) |
| 1218 |
|
| 1219 |
m.Lock() |
| 1220 |
refMeas(ctx, val, alice) |
| 1221 |
m.Unlock() |
| 1222 |
} |
| 1223 |
}(i) |
| 1224 |
} |
| 1225 |
wg.Wait() |
| 1226 |
|
| 1227 |
dest := new(metricdata.Aggregation) |
| 1228 |
comp(dest) |
| 1229 |
h := (*dest).(metricdata.ExponentialHistogram[N]) |
| 1230 |
require.Len(t, h.DataPoints, 1) |
| 1231 |
|
| 1232 |
refDest := new(metricdata.Aggregation) |
| 1233 |
refComp(refDest) |
| 1234 |
refH := (*refDest).(metricdata.ExponentialHistogram[N]) |
| 1235 |
require.Len(t, refH.DataPoints, 1) |
| 1236 |
|
| 1237 |
// StartTime/Time will differ slightly. |
| 1238 |
h.DataPoints[0].StartTime = refH.DataPoints[0].StartTime |
| 1239 |
h.DataPoints[0].Time = refH.DataPoints[0].Time |
| 1240 |
|
| 1241 |
// Float sums might be slightly slightly off due to summing wildly different magnitudes |
| 1242 |
// in different orders concurrently versus sequentially. |
| 1243 |
actualSum := float64(h.DataPoints[0].Sum) |
| 1244 |
expectedSum := float64(refH.DataPoints[0].Sum) |
| 1245 |
if actualSum != expectedSum { |
| 1246 |
assert.InEpsilon(t, expectedSum, actualSum, 0.5, "Sum") |
| 1247 |
// Force equality for the deep struct comparison below |
| 1248 |
h.DataPoints[0].Sum = refH.DataPoints[0].Sum |
| 1249 |
} |
| 1250 |
|
| 1251 |
// Normalize Exemplars to avoid nil vs empty slice comparison failures |
| 1252 |
h.DataPoints[0].Exemplars = nil |
| 1253 |
refH.DataPoints[0].Exemplars = nil |
| 1254 |
|
| 1255 |
assert.Equal(t, refH, h) |
| 1256 |
}) |
| 1257 |
} |
| 1258 |
} |