| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"sort" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
"go.opentelemetry.io/otel/sdk/internal/x" |
| 17 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 18 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
bounds = []float64{1, 5} |
| 23 |
noMinMax = false |
| 24 |
) |
| 25 |
|
| 26 |
func TestHistogram(t *testing.T) { |
| 27 |
c := new(clock) |
| 28 |
t.Cleanup(c.Register()) |
| 29 |
|
| 30 |
t.Run("Int64/Delta/Sum", testDeltaHist[int64](conf[int64]{hPt: hPointSummed[int64]})) |
| 31 |
c.Reset() |
| 32 |
t.Run("Int64/Delta/NoSum", testDeltaHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64]})) |
| 33 |
c.Reset() |
| 34 |
t.Run("Float64/Delta/Sum", testDeltaHist[float64](conf[float64]{hPt: hPointSummed[float64]})) |
| 35 |
c.Reset() |
| 36 |
t.Run("Float64/Delta/NoSum", testDeltaHist[float64](conf[float64]{noSum: true, hPt: hPoint[float64]})) |
| 37 |
c.Reset() |
| 38 |
|
| 39 |
t.Run("Int64/Cumulative/Sum", func(t *testing.T) { |
| 40 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false") |
| 41 |
assert.False(t, x.PerSeriesStartTimestamps.Enabled()) |
| 42 |
testCumulativeHist[int64](conf[int64]{hPt: hPointSummed[int64]})(t) |
| 43 |
}) |
| 44 |
c.Reset() |
| 45 |
|
| 46 |
t.Run("Int64/Cumulative/Sum/PerSeriesStartTimeEnabled", func(t *testing.T) { |
| 47 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true") |
| 48 |
assert.True(t, x.PerSeriesStartTimestamps.Enabled()) |
| 49 |
testCumulativeHist[int64](conf[int64]{hPt: hPointSummed[int64]})(t) |
| 50 |
}) |
| 51 |
c.Reset() |
| 52 |
|
| 53 |
t.Run("Int64/Cumulative/NoSum", func(t *testing.T) { |
| 54 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false") |
| 55 |
assert.False(t, x.PerSeriesStartTimestamps.Enabled()) |
| 56 |
testCumulativeHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64]})(t) |
| 57 |
}) |
| 58 |
c.Reset() |
| 59 |
|
| 60 |
t.Run("Int64/Cumulative/NoSum/PerSeriesStartTimeEnabled", func(t *testing.T) { |
| 61 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true") |
| 62 |
assert.True(t, x.PerSeriesStartTimestamps.Enabled()) |
| 63 |
testCumulativeHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64]})(t) |
| 64 |
}) |
| 65 |
c.Reset() |
| 66 |
|
| 67 |
t.Run("Float64/Cumulative/Sum", func(t *testing.T) { |
| 68 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false") |
| 69 |
assert.False(t, x.PerSeriesStartTimestamps.Enabled()) |
| 70 |
testCumulativeHist[float64](conf[float64]{hPt: hPointSummed[float64]})(t) |
| 71 |
}) |
| 72 |
c.Reset() |
| 73 |
|
| 74 |
t.Run("Float64/Cumulative/Sum/PerSeriesStartTimeEnabled", func(t *testing.T) { |
| 75 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true") |
| 76 |
assert.True(t, x.PerSeriesStartTimestamps.Enabled()) |
| 77 |
testCumulativeHist[float64](conf[float64]{hPt: hPointSummed[float64]})(t) |
| 78 |
}) |
| 79 |
c.Reset() |
| 80 |
|
| 81 |
t.Run("Float64/Cumulative/NoSum", func(t *testing.T) { |
| 82 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false") |
| 83 |
assert.False(t, x.PerSeriesStartTimestamps.Enabled()) |
| 84 |
testCumulativeHist[float64]( |
| 85 |
conf[float64]{noSum: true, hPt: hPoint[float64]}, |
| 86 |
)( |
| 87 |
t, |
| 88 |
) |
| 89 |
}) |
| 90 |
c.Reset() |
| 91 |
|
| 92 |
t.Run("Float64/Cumulative/NoSum/PerSeriesStartTimeEnabled", func(t *testing.T) { |
| 93 |
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true") |
| 94 |
assert.True(t, x.PerSeriesStartTimestamps.Enabled()) |
| 95 |
testCumulativeHist[float64]( |
| 96 |
conf[float64]{noSum: true, hPt: hPoint[float64]}, |
| 97 |
)( |
| 98 |
t, |
| 99 |
) |
| 100 |
}) |
| 101 |
c.Reset() |
| 102 |
} |
| 103 |
|
| 104 |
type conf[N int64 | float64] struct { |
| 105 |
noSum bool |
| 106 |
hPt func(attribute.Set, N, uint64, time.Time, time.Time) metricdata.HistogramDataPoint[N] |
| 107 |
} |
| 108 |
|
| 109 |
func testDeltaHist[N int64 | float64](c conf[N]) func(t *testing.T) { |
| 110 |
in, out := Builder[N]{ |
| 111 |
Temporality: metricdata.DeltaTemporality, |
| 112 |
Filter: attrFltr, |
| 113 |
AggregationLimit: 3, |
| 114 |
}.ExplicitBucketHistogram(bounds, noMinMax, c.noSum) |
| 115 |
ctx := context.Background() |
| 116 |
return test[N](in, out, []teststep[N]{ |
| 117 |
{ |
| 118 |
input: []arg[N]{}, |
| 119 |
expect: output{ |
| 120 |
n: 0, |
| 121 |
agg: metricdata.Histogram[N]{ |
| 122 |
Temporality: metricdata.DeltaTemporality, |
| 123 |
DataPoints: []metricdata.HistogramDataPoint[N]{}, |
| 124 |
}, |
| 125 |
}, |
| 126 |
}, |
| 127 |
{ |
| 128 |
input: []arg[N]{ |
| 129 |
{ctx, 2, alice}, |
| 130 |
{ctx, 10, bob}, |
| 131 |
{ctx, 2, alice}, |
| 132 |
{ctx, 2, alice}, |
| 133 |
{ctx, 10, bob}, |
| 134 |
}, |
| 135 |
expect: output{ |
| 136 |
n: 2, |
| 137 |
agg: metricdata.Histogram[N]{ |
| 138 |
Temporality: metricdata.DeltaTemporality, |
| 139 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 140 |
c.hPt(fltrAlice, 2, 3, y2kPlus(1), y2kPlus(2)), |
| 141 |
c.hPt(fltrBob, 10, 2, y2kPlus(1), y2kPlus(2)), |
| 142 |
}, |
| 143 |
}, |
| 144 |
}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
input: []arg[N]{ |
| 148 |
{ctx, 10, alice}, |
| 149 |
{ctx, 3, bob}, |
| 150 |
}, |
| 151 |
expect: output{ |
| 152 |
n: 2, |
| 153 |
agg: metricdata.Histogram[N]{ |
| 154 |
Temporality: metricdata.DeltaTemporality, |
| 155 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 156 |
c.hPt(fltrAlice, 10, 1, y2kPlus(2), y2kPlus(3)), |
| 157 |
c.hPt(fltrBob, 3, 1, y2kPlus(2), y2kPlus(3)), |
| 158 |
}, |
| 159 |
}, |
| 160 |
}, |
| 161 |
}, |
| 162 |
{ |
| 163 |
input: []arg[N]{}, |
| 164 |
// Delta histograms are expected to reset. |
| 165 |
expect: output{ |
| 166 |
n: 0, |
| 167 |
agg: metricdata.Histogram[N]{ |
| 168 |
Temporality: metricdata.DeltaTemporality, |
| 169 |
DataPoints: []metricdata.HistogramDataPoint[N]{}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
input: []arg[N]{ |
| 175 |
{ctx, 1, alice}, |
| 176 |
{ctx, 1, bob}, |
| 177 |
// These will exceed cardinality limit. |
| 178 |
{ctx, 1, carol}, |
| 179 |
{ctx, 1, dave}, |
| 180 |
}, |
| 181 |
expect: output{ |
| 182 |
n: 3, |
| 183 |
agg: metricdata.Histogram[N]{ |
| 184 |
Temporality: metricdata.DeltaTemporality, |
| 185 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 186 |
c.hPt(fltrAlice, 1, 1, y2kPlus(4), y2kPlus(5)), |
| 187 |
c.hPt(fltrBob, 1, 1, y2kPlus(4), y2kPlus(5)), |
| 188 |
c.hPt(overflowSet, 1, 2, y2kPlus(4), y2kPlus(5)), |
| 189 |
}, |
| 190 |
}, |
| 191 |
}, |
| 192 |
}, |
| 193 |
}) |
| 194 |
} |
| 195 |
|
| 196 |
func testCumulativeHist[N int64 | float64](c conf[N]) func(t *testing.T) { |
| 197 |
in, out := Builder[N]{ |
| 198 |
Temporality: metricdata.CumulativeTemporality, |
| 199 |
Filter: attrFltr, |
| 200 |
AggregationLimit: 3, |
| 201 |
}.ExplicitBucketHistogram(bounds, noMinMax, c.noSum) |
| 202 |
|
| 203 |
aliceStartTime := y2kPlus(0) |
| 204 |
bobStartTime := y2kPlus(0) |
| 205 |
overflowStartTime := y2kPlus(0) |
| 206 |
|
| 207 |
if x.PerSeriesStartTimestamps.Enabled() { |
| 208 |
aliceStartTime = y2kPlus(2) |
| 209 |
bobStartTime = y2kPlus(3) |
| 210 |
overflowStartTime = y2kPlus(7) |
| 211 |
} |
| 212 |
|
| 213 |
ctx := context.Background() |
| 214 |
return test[N](in, out, []teststep[N]{ |
| 215 |
{ |
| 216 |
input: []arg[N]{}, |
| 217 |
expect: output{ |
| 218 |
n: 0, |
| 219 |
agg: metricdata.Histogram[N]{ |
| 220 |
Temporality: metricdata.CumulativeTemporality, |
| 221 |
DataPoints: []metricdata.HistogramDataPoint[N]{}, |
| 222 |
}, |
| 223 |
}, |
| 224 |
}, |
| 225 |
{ |
| 226 |
input: []arg[N]{ |
| 227 |
{ctx, 2, alice}, |
| 228 |
{ctx, 10, bob}, |
| 229 |
{ctx, 2, alice}, |
| 230 |
{ctx, 2, alice}, |
| 231 |
{ctx, 10, bob}, |
| 232 |
}, |
| 233 |
expect: output{ |
| 234 |
n: 2, |
| 235 |
agg: metricdata.Histogram[N]{ |
| 236 |
Temporality: metricdata.CumulativeTemporality, |
| 237 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 238 |
c.hPt(fltrAlice, 2, 3, aliceStartTime, y2kPlus(4)), |
| 239 |
c.hPt(fltrBob, 10, 2, bobStartTime, y2kPlus(4)), |
| 240 |
}, |
| 241 |
}, |
| 242 |
}, |
| 243 |
}, |
| 244 |
{ |
| 245 |
input: []arg[N]{ |
| 246 |
{ctx, 2, alice}, |
| 247 |
{ctx, 10, bob}, |
| 248 |
}, |
| 249 |
expect: output{ |
| 250 |
n: 2, |
| 251 |
agg: metricdata.Histogram[N]{ |
| 252 |
Temporality: metricdata.CumulativeTemporality, |
| 253 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 254 |
c.hPt(fltrAlice, 2, 4, aliceStartTime, y2kPlus(5)), |
| 255 |
c.hPt(fltrBob, 10, 3, bobStartTime, y2kPlus(5)), |
| 256 |
}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
}, |
| 260 |
{ |
| 261 |
input: []arg[N]{}, |
| 262 |
expect: output{ |
| 263 |
n: 2, |
| 264 |
agg: metricdata.Histogram[N]{ |
| 265 |
Temporality: metricdata.CumulativeTemporality, |
| 266 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 267 |
c.hPt(fltrAlice, 2, 4, aliceStartTime, y2kPlus(6)), |
| 268 |
c.hPt(fltrBob, 10, 3, bobStartTime, y2kPlus(6)), |
| 269 |
}, |
| 270 |
}, |
| 271 |
}, |
| 272 |
}, |
| 273 |
{ |
| 274 |
input: []arg[N]{ |
| 275 |
// These will exceed cardinality limit. |
| 276 |
{ctx, 1, carol}, |
| 277 |
{ctx, 1, dave}, |
| 278 |
}, |
| 279 |
expect: output{ |
| 280 |
n: 3, |
| 281 |
agg: metricdata.Histogram[N]{ |
| 282 |
Temporality: metricdata.CumulativeTemporality, |
| 283 |
DataPoints: []metricdata.HistogramDataPoint[N]{ |
| 284 |
c.hPt(fltrAlice, 2, 4, aliceStartTime, y2kPlus(8)), |
| 285 |
c.hPt(fltrBob, 10, 3, bobStartTime, y2kPlus(8)), |
| 286 |
c.hPt(overflowSet, 1, 2, overflowStartTime, y2kPlus(8)), |
| 287 |
}, |
| 288 |
}, |
| 289 |
}, |
| 290 |
}, |
| 291 |
}) |
| 292 |
} |
| 293 |
|
| 294 |
func TestHistogramConcurrentSafe(t *testing.T) { |
| 295 |
t.Run("Int64/Delta", testDeltaHistConcurrentSafe[int64]()) |
| 296 |
t.Run("Float64/Delta", testDeltaHistConcurrentSafe[float64]()) |
| 297 |
t.Run("Int64/Cumulative", testCumulativeHistConcurrentSafe[int64]()) |
| 298 |
t.Run("Float64/Cumulative", testCumulativeHistConcurrentSafe[float64]()) |
| 299 |
} |
| 300 |
|
| 301 |
func validateHistogram[N int64 | float64](t *testing.T, aggs []metricdata.Aggregation) { |
| 302 |
sums := make(map[attribute.Set]N) |
| 303 |
counts := make(map[attribute.Set]uint64) |
| 304 |
bucketCounts := make(map[attribute.Set][]uint64) |
| 305 |
|
| 306 |
for i, agg := range aggs { |
| 307 |
s, ok := agg.(metricdata.Histogram[N]) |
| 308 |
require.True(t, ok) |
| 309 |
require.LessOrEqual(t, len(s.DataPoints), 3, "AggregationLimit of 3 exceeded in a single cycle") |
| 310 |
for _, dp := range s.DataPoints { |
| 311 |
if s.Temporality == metricdata.DeltaTemporality { |
| 312 |
sums[dp.Attributes] += dp.Sum |
| 313 |
counts[dp.Attributes] += dp.Count |
| 314 |
if bucketCounts[dp.Attributes] == nil { |
| 315 |
bucketCounts[dp.Attributes] = make([]uint64, len(dp.BucketCounts)) |
| 316 |
} |
| 317 |
for idx, c := range dp.BucketCounts { |
| 318 |
bucketCounts[dp.Attributes][idx] += c |
| 319 |
} |
| 320 |
} else if i == len(aggs)-1 { |
| 321 |
sums[dp.Attributes] = dp.Sum |
| 322 |
counts[dp.Attributes] = dp.Count |
| 323 |
bucketCounts[dp.Attributes] = make([]uint64, len(dp.BucketCounts)) |
| 324 |
copy(bucketCounts[dp.Attributes], dp.BucketCounts) |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
var totalSum N |
| 330 |
var totalCount uint64 |
| 331 |
totalBuckets := make([]uint64, 4) |
| 332 |
|
| 333 |
for _, val := range sums { |
| 334 |
totalSum += val |
| 335 |
} |
| 336 |
for _, val := range counts { |
| 337 |
totalCount += val |
| 338 |
} |
| 339 |
for _, bc := range bucketCounts { |
| 340 |
for idx, c := range bc { |
| 341 |
if idx < len(totalBuckets) { |
| 342 |
totalBuckets[idx] += c |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
assertSumEqual[N](t, expectedConcurrentSum[N](), totalSum) |
| 348 |
assert.Equal(t, expectedConcurrentCount, totalCount) |
| 349 |
|
| 350 |
var expectedBuckets []uint64 |
| 351 |
switch any(*new(N)).(type) { |
| 352 |
case float64: |
| 353 |
// Float sequence: 2.5, 6.1, 4.4, 10.0, 22.0, -3.5, -6.5, 3.0, -6.0 |
| 354 |
// Bounds {0, 2, 4}: |
| 355 |
// (-inf, 0]: -3.5, -6.5, -6.0 (3x) |
| 356 |
// (0, 2]: none (0x) |
| 357 |
// (2, 4]: 2.5, 3.0 (2x) |
| 358 |
// (4, +inf): 6.1, 4.4, 10.0, 22.0 (4x) |
| 359 |
// 10 full loops per goroutine * 10 goroutines = 100x |
| 360 |
expectedBuckets = []uint64{300, 0, 200, 400} |
| 361 |
default: |
| 362 |
// Int sequence: 2, 6, 4, 10, 22, -3, -6, 3, -6 |
| 363 |
// Bounds {0, 2, 4}: |
| 364 |
// (-inf, 0]: -3, -6, -6 (3x) |
| 365 |
// (0, 2]: 2 (1x) |
| 366 |
// (2, 4]: 4, 3 (2x) |
| 367 |
// (4, +inf): 6, 10, 22 (3x) |
| 368 |
// 10 full loops per goroutine * 10 goroutines = 100x |
| 369 |
expectedBuckets = []uint64{300, 100, 200, 300} |
| 370 |
} |
| 371 |
assert.Equal(t, expectedBuckets, totalBuckets) |
| 372 |
} |
| 373 |
|
| 374 |
func testCumulativeHistConcurrentSafe[N int64 | float64]() func(*testing.T) { |
| 375 |
in, out := Builder[N]{ |
| 376 |
Temporality: metricdata.CumulativeTemporality, |
| 377 |
Filter: attrFltr, |
| 378 |
AggregationLimit: 3, |
| 379 |
}.ExplicitBucketHistogram([]float64{0, 2, 4}, false, false) |
| 380 |
return testAggregationConcurrentSafe[N](in, out, validateHistogram[N]) |
| 381 |
} |
| 382 |
|
| 383 |
func testDeltaHistConcurrentSafe[N int64 | float64]() func(*testing.T) { |
| 384 |
in, out := Builder[N]{ |
| 385 |
Temporality: metricdata.DeltaTemporality, |
| 386 |
Filter: attrFltr, |
| 387 |
AggregationLimit: 3, |
| 388 |
}.ExplicitBucketHistogram([]float64{0, 2, 4}, false, false) |
| 389 |
return testAggregationConcurrentSafe[N](in, out, validateHistogram[N]) |
| 390 |
} |
| 391 |
|
| 392 |
// hPointSummed returns an HistogramDataPoint that started and ended now with |
| 393 |
// multi number of measurements values v. It includes a min and max (set to v). |
| 394 |
func hPointSummed[N int64 | float64]( |
| 395 |
a attribute.Set, |
| 396 |
v N, |
| 397 |
multi uint64, |
| 398 |
start, t time.Time, |
| 399 |
) metricdata.HistogramDataPoint[N] { |
| 400 |
idx := sort.SearchFloat64s(bounds, float64(v)) |
| 401 |
counts := make([]uint64, len(bounds)+1) |
| 402 |
counts[idx] += multi |
| 403 |
return metricdata.HistogramDataPoint[N]{ |
| 404 |
Attributes: a, |
| 405 |
StartTime: start, |
| 406 |
Time: t, |
| 407 |
Count: multi, |
| 408 |
Bounds: bounds, |
| 409 |
BucketCounts: counts, |
| 410 |
Min: metricdata.NewExtrema(v), |
| 411 |
Max: metricdata.NewExtrema(v), |
| 412 |
Sum: v * N(multi), |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
// hPoint returns an HistogramDataPoint that started and ended now with multi |
| 417 |
// number of measurements values v. It includes a min and max (set to v). |
| 418 |
func hPoint[N int64 | float64]( |
| 419 |
a attribute.Set, |
| 420 |
v N, |
| 421 |
multi uint64, |
| 422 |
start, t time.Time, |
| 423 |
) metricdata.HistogramDataPoint[N] { |
| 424 |
idx := sort.SearchFloat64s(bounds, float64(v)) |
| 425 |
counts := make([]uint64, len(bounds)+1) |
| 426 |
counts[idx] += multi |
| 427 |
return metricdata.HistogramDataPoint[N]{ |
| 428 |
Attributes: a, |
| 429 |
StartTime: start, |
| 430 |
Time: t, |
| 431 |
Count: multi, |
| 432 |
Bounds: bounds, |
| 433 |
BucketCounts: counts, |
| 434 |
Min: metricdata.NewExtrema(v), |
| 435 |
Max: metricdata.NewExtrema(v), |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
func TestHistogramImmutableBounds(t *testing.T) { |
| 440 |
b := []float64{0, 1, 2} |
| 441 |
cpB := make([]float64, len(b)) |
| 442 |
copy(cpB, b) |
| 443 |
|
| 444 |
h := newCumulativeHistogram[int64](b, false, false, 0, dropExemplars[int64]) |
| 445 |
require.Equal(t, cpB, h.bounds) |
| 446 |
|
| 447 |
b[0] = 10 |
| 448 |
assert.Equal(t, cpB, h.bounds, "modifying the bounds argument should not change the bounds") |
| 449 |
|
| 450 |
h.measure(t.Context(), 5, alice, nil) |
| 451 |
|
| 452 |
var data metricdata.Aggregation = metricdata.Histogram[int64]{} |
| 453 |
h.collect(&data) |
| 454 |
hdp := data.(metricdata.Histogram[int64]).DataPoints[0] |
| 455 |
hdp.Bounds[1] = 10 |
| 456 |
assert.Equal(t, cpB, h.bounds, "modifying the Aggregation bounds should not change the bounds") |
| 457 |
} |
| 458 |
|
| 459 |
func TestCumulativeHistogramImmutableCounts(t *testing.T) { |
| 460 |
h := newCumulativeHistogram[int64](bounds, noMinMax, false, 0, dropExemplars[int64]) |
| 461 |
h.measure(t.Context(), 5, alice, nil) |
| 462 |
|
| 463 |
var data metricdata.Aggregation = metricdata.Histogram[int64]{} |
| 464 |
h.collect(&data) |
| 465 |
hdp := data.(metricdata.Histogram[int64]).DataPoints[0] |
| 466 |
|
| 467 |
hPt, ok := h.values.Load(alice.Equivalent()) |
| 468 |
require.True(t, ok) |
| 469 |
hcHistPt := hPt.(*hotColdHistogramPoint[int64]) |
| 470 |
readIdx := hcHistPt.hcwg.swapHotAndWait() |
| 471 |
var bucketCounts []uint64 |
| 472 |
hcHistPt.hotColdPoint[readIdx].loadCountsInto(&bucketCounts) |
| 473 |
require.Equal(t, hdp.BucketCounts, bucketCounts) |
| 474 |
hotIdx := (readIdx + 1) % 2 |
| 475 |
hcHistPt.hotColdPoint[readIdx].mergeIntoAndReset(&hcHistPt.hotColdPoint[hotIdx], noMinMax, false) |
| 476 |
|
| 477 |
cpCounts := make([]uint64, len(hdp.BucketCounts)) |
| 478 |
copy(cpCounts, hdp.BucketCounts) |
| 479 |
hdp.BucketCounts[0] = 10 |
| 480 |
hPt, ok = h.values.Load(alice.Equivalent()) |
| 481 |
require.True(t, ok) |
| 482 |
hcHistPt = hPt.(*hotColdHistogramPoint[int64]) |
| 483 |
readIdx = hcHistPt.hcwg.swapHotAndWait() |
| 484 |
hcHistPt.hotColdPoint[readIdx].loadCountsInto(&bucketCounts) |
| 485 |
assert.Equal( |
| 486 |
t, |
| 487 |
cpCounts, |
| 488 |
bucketCounts, |
| 489 |
"modifying the Aggregator bucket counts should not change the Aggregator", |
| 490 |
) |
| 491 |
} |
| 492 |
|
| 493 |
func TestDeltaHistogramReset(t *testing.T) { |
| 494 |
orig := now |
| 495 |
now = func() time.Time { return y2k } |
| 496 |
t.Cleanup(func() { now = orig }) |
| 497 |
|
| 498 |
h := newDeltaHistogram[int64](bounds, noMinMax, false, 0, dropExemplars[int64]) |
| 499 |
|
| 500 |
var data metricdata.Aggregation = metricdata.Histogram[int64]{} |
| 501 |
require.Equal(t, 0, h.collect(&data)) |
| 502 |
require.Empty(t, data.(metricdata.Histogram[int64]).DataPoints) |
| 503 |
|
| 504 |
h.measure(t.Context(), 1, alice, nil) |
| 505 |
|
| 506 |
expect := metricdata.Histogram[int64]{Temporality: metricdata.DeltaTemporality} |
| 507 |
expect.DataPoints = []metricdata.HistogramDataPoint[int64]{hPointSummed[int64](alice, 1, 1, now(), now())} |
| 508 |
h.collect(&data) |
| 509 |
metricdatatest.AssertAggregationsEqual(t, expect, data) |
| 510 |
|
| 511 |
// The attr set should be forgotten once Aggregations is called. |
| 512 |
expect.DataPoints = nil |
| 513 |
assert.Equal(t, 0, h.collect(&data)) |
| 514 |
assert.Empty(t, data.(metricdata.Histogram[int64]).DataPoints) |
| 515 |
|
| 516 |
// Aggregating another set should not affect the original (alice). |
| 517 |
h.measure(t.Context(), 1, bob, nil) |
| 518 |
expect.DataPoints = []metricdata.HistogramDataPoint[int64]{hPointSummed[int64](bob, 1, 1, now(), now())} |
| 519 |
h.collect(&data) |
| 520 |
metricdatatest.AssertAggregationsEqual(t, expect, data) |
| 521 |
} |
| 522 |
|
| 523 |
func BenchmarkHistogram(b *testing.B) { |
| 524 |
b.Run("Int64/Cumulative", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) { |
| 525 |
return Builder[int64]{ |
| 526 |
Temporality: metricdata.CumulativeTemporality, |
| 527 |
}.ExplicitBucketHistogram(bounds, noMinMax, false) |
| 528 |
})) |
| 529 |
b.Run("Int64/Delta", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) { |
| 530 |
return Builder[int64]{ |
| 531 |
Temporality: metricdata.DeltaTemporality, |
| 532 |
}.ExplicitBucketHistogram(bounds, noMinMax, false) |
| 533 |
})) |
| 534 |
b.Run("Float64/Cumulative", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) { |
| 535 |
return Builder[float64]{ |
| 536 |
Temporality: metricdata.CumulativeTemporality, |
| 537 |
}.ExplicitBucketHistogram(bounds, noMinMax, false) |
| 538 |
})) |
| 539 |
b.Run("Float64/Delta", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) { |
| 540 |
return Builder[float64]{ |
| 541 |
Temporality: metricdata.DeltaTemporality, |
| 542 |
}.ExplicitBucketHistogram(bounds, noMinMax, false) |
| 543 |
})) |
| 544 |
} |