Debian Codegraph @

sdk/metric/internal/aggregate/sum_test.go

raw
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 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/otel/attribute"
14 "go.opentelemetry.io/otel/sdk/internal/x"
15 "go.opentelemetry.io/otel/sdk/metric/metricdata"
16 )
17
18 func TestSum(t *testing.T) {
19 c := new(clock)
20 t.Cleanup(c.Register())
21
22 t.Run("Int64/DeltaSum", testDeltaSum[int64]())
23 c.Reset()
24
25 t.Run("Float64/DeltaSum", testDeltaSum[float64]())
26 c.Reset()
27
28 t.Run("Int64/CumulativeSum", func(t *testing.T) {
29 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
30 assert.False(t, x.PerSeriesStartTimestamps.Enabled())
31 testCumulativeSum[int64]()(t)
32 })
33 c.Reset()
34
35 t.Run("Int64/CumulativeSum/PerSeriesStartTimeEnabled", func(t *testing.T) {
36 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
37 assert.True(t, x.PerSeriesStartTimestamps.Enabled())
38 testCumulativeSum[int64]()(t)
39 })
40 c.Reset()
41
42 t.Run("Float64/CumulativeSum", func(t *testing.T) {
43 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
44 assert.False(t, x.PerSeriesStartTimestamps.Enabled())
45 testCumulativeSum[float64]()(t)
46 })
47 c.Reset()
48
49 t.Run("Float64/CumulativeSum/PerSeriesStartTimeEnabled", func(t *testing.T) {
50 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
51 assert.True(t, x.PerSeriesStartTimestamps.Enabled())
52 testCumulativeSum[float64]()(t)
53 })
54 c.Reset()
55
56 t.Run("Int64/DeltaPrecomputedSum", testDeltaPrecomputedSum[int64]())
57 c.Reset()
58
59 t.Run("Float64/DeltaPrecomputedSum", testDeltaPrecomputedSum[float64]())
60 c.Reset()
61
62 t.Run("Int64/CumulativePrecomputedSum", testCumulativePrecomputedSum[int64]())
63 c.Reset()
64
65 t.Run("Float64/CumulativePrecomputedSum", testCumulativePrecomputedSum[float64]())
66 }
67
68 func testDeltaSum[N int64 | float64]() func(t *testing.T) {
69 mono := false
70 in, out := Builder[N]{
71 Temporality: metricdata.DeltaTemporality,
72 Filter: attrFltr,
73 AggregationLimit: 3,
74 }.Sum(mono)
75 ctx := context.Background()
76 return test[N](in, out, []teststep[N]{
77 {
78 input: []arg[N]{},
79 expect: output{
80 n: 0,
81 agg: metricdata.Sum[N]{
82 IsMonotonic: mono,
83 Temporality: metricdata.DeltaTemporality,
84 DataPoints: []metricdata.DataPoint[N]{},
85 },
86 },
87 },
88 {
89 input: []arg[N]{
90 {ctx, 1, alice},
91 {ctx, -1, bob},
92 {ctx, 1, alice},
93 {ctx, 2, alice},
94 {ctx, -10, bob},
95 },
96 expect: output{
97 n: 2,
98 agg: metricdata.Sum[N]{
99 IsMonotonic: mono,
100 Temporality: metricdata.DeltaTemporality,
101 DataPoints: []metricdata.DataPoint[N]{
102 {
103 Attributes: fltrAlice,
104 StartTime: y2kPlus(1),
105 Time: y2kPlus(4),
106 Value: 4,
107 },
108 {
109 Attributes: fltrBob,
110 StartTime: y2kPlus(1),
111 Time: y2kPlus(4),
112 Value: -11,
113 },
114 },
115 },
116 },
117 },
118 {
119 input: []arg[N]{
120 {ctx, 10, alice},
121 {ctx, 3, bob},
122 },
123 expect: output{
124 n: 2,
125 agg: metricdata.Sum[N]{
126 IsMonotonic: mono,
127 Temporality: metricdata.DeltaTemporality,
128 DataPoints: []metricdata.DataPoint[N]{
129 {
130 Attributes: fltrAlice,
131 StartTime: y2kPlus(4),
132 Time: y2kPlus(7),
133 Value: 10,
134 },
135 {
136 Attributes: fltrBob,
137 StartTime: y2kPlus(4),
138 Time: y2kPlus(7),
139 Value: 3,
140 },
141 },
142 },
143 },
144 },
145 {
146 input: []arg[N]{},
147 // Delta sums are expected to reset.
148 expect: output{
149 n: 0,
150 agg: metricdata.Sum[N]{
151 IsMonotonic: mono,
152 Temporality: metricdata.DeltaTemporality,
153 DataPoints: []metricdata.DataPoint[N]{},
154 },
155 },
156 },
157 {
158 input: []arg[N]{
159 {ctx, 1, alice},
160 {ctx, 1, bob},
161 // These will exceed cardinality limit.
162 {ctx, 1, carol},
163 {ctx, 1, dave},
164 },
165 expect: output{
166 n: 3,
167 agg: metricdata.Sum[N]{
168 IsMonotonic: mono,
169 Temporality: metricdata.DeltaTemporality,
170 DataPoints: []metricdata.DataPoint[N]{
171 {
172 Attributes: fltrAlice,
173 StartTime: y2kPlus(8),
174 Time: y2kPlus(12),
175 Value: 1,
176 },
177 {
178 Attributes: fltrBob,
179 StartTime: y2kPlus(8),
180 Time: y2kPlus(12),
181 Value: 1,
182 },
183 {
184 Attributes: overflowSet,
185 StartTime: y2kPlus(8),
186 Time: y2kPlus(12),
187 Value: 2,
188 },
189 },
190 },
191 },
192 },
193 })
194 }
195
196 func testCumulativeSum[N int64 | float64]() func(t *testing.T) {
197 mono := false
198 in, out := Builder[N]{
199 Temporality: metricdata.CumulativeTemporality,
200 Filter: attrFltr,
201 AggregationLimit: 3,
202 }.Sum(mono)
203
204 aliceStartTime := y2kPlus(0)
205 bobStartTime := y2kPlus(0)
206 overflowStartTime := y2kPlus(0)
207
208 if x.PerSeriesStartTimestamps.Enabled() {
209 aliceStartTime = y2kPlus(2)
210 bobStartTime = y2kPlus(3)
211 overflowStartTime = y2kPlus(6)
212 }
213
214 ctx := context.Background()
215 return test[N](in, out, []teststep[N]{
216 {
217 input: []arg[N]{},
218 expect: output{
219 n: 0,
220 agg: metricdata.Sum[N]{
221 IsMonotonic: mono,
222 Temporality: metricdata.CumulativeTemporality,
223 DataPoints: []metricdata.DataPoint[N]{},
224 },
225 },
226 },
227 {
228 input: []arg[N]{
229 {ctx, 1, alice},
230 {ctx, -1, bob},
231 {ctx, 1, alice},
232 {ctx, 2, alice},
233 {ctx, -10, bob},
234 },
235 expect: output{
236 n: 2,
237 agg: metricdata.Sum[N]{
238 IsMonotonic: mono,
239 Temporality: metricdata.CumulativeTemporality,
240 DataPoints: []metricdata.DataPoint[N]{
241 {
242 Attributes: fltrAlice,
243 StartTime: aliceStartTime,
244 Time: y2kPlus(4),
245 Value: 4,
246 },
247 {
248 Attributes: fltrBob,
249 StartTime: bobStartTime,
250 Time: y2kPlus(4),
251 Value: -11,
252 },
253 },
254 },
255 },
256 },
257 {
258 input: []arg[N]{
259 {ctx, 10, alice},
260 {ctx, 3, bob},
261 },
262 expect: output{
263 n: 2,
264 agg: metricdata.Sum[N]{
265 IsMonotonic: mono,
266 Temporality: metricdata.CumulativeTemporality,
267 DataPoints: []metricdata.DataPoint[N]{
268 {
269 Attributes: fltrAlice,
270 StartTime: aliceStartTime,
271 Time: y2kPlus(5),
272 Value: 14,
273 },
274 {
275 Attributes: fltrBob,
276 StartTime: bobStartTime,
277 Time: y2kPlus(5),
278 Value: -8,
279 },
280 },
281 },
282 },
283 },
284 {
285 input: []arg[N]{
286 // These will exceed cardinality limit.
287 {ctx, 1, carol},
288 {ctx, 1, dave},
289 },
290 expect: output{
291 n: 3,
292 agg: metricdata.Sum[N]{
293 IsMonotonic: mono,
294 Temporality: metricdata.CumulativeTemporality,
295 DataPoints: []metricdata.DataPoint[N]{
296 {
297 Attributes: fltrAlice,
298 StartTime: aliceStartTime,
299 Time: y2kPlus(7),
300 Value: 14,
301 },
302 {
303 Attributes: fltrBob,
304 StartTime: bobStartTime,
305 Time: y2kPlus(7),
306 Value: -8,
307 },
308 {
309 Attributes: overflowSet,
310 StartTime: overflowStartTime,
311 Time: y2kPlus(7),
312 Value: 2,
313 },
314 },
315 },
316 },
317 },
318 })
319 }
320
321 func testDeltaPrecomputedSum[N int64 | float64]() func(t *testing.T) {
322 mono := false
323 in, out := Builder[N]{
324 Temporality: metricdata.DeltaTemporality,
325 Filter: attrFltr,
326 AggregationLimit: 3,
327 }.PrecomputedSum(mono)
328 ctx := context.Background()
329 return test[N](in, out, []teststep[N]{
330 {
331 input: []arg[N]{},
332 expect: output{
333 n: 0,
334 agg: metricdata.Sum[N]{
335 IsMonotonic: mono,
336 Temporality: metricdata.DeltaTemporality,
337 DataPoints: []metricdata.DataPoint[N]{},
338 },
339 },
340 },
341 {
342 input: []arg[N]{
343 {ctx, 1, alice},
344 {ctx, -1, bob},
345 {ctx, 1, fltrAlice},
346 {ctx, 2, alice},
347 {ctx, -10, bob},
348 },
349 expect: output{
350 n: 2,
351 agg: metricdata.Sum[N]{
352 IsMonotonic: mono,
353 Temporality: metricdata.DeltaTemporality,
354 DataPoints: []metricdata.DataPoint[N]{
355 {
356 Attributes: fltrAlice,
357 StartTime: y2kPlus(1),
358 Time: y2kPlus(4),
359 Value: 4,
360 },
361 {
362 Attributes: fltrBob,
363 StartTime: y2kPlus(1),
364 Time: y2kPlus(4),
365 Value: -11,
366 },
367 },
368 },
369 },
370 },
371 {
372 input: []arg[N]{
373 {ctx, 1, fltrAlice},
374 {ctx, 10, alice},
375 {ctx, 3, bob},
376 },
377 expect: output{
378 n: 2,
379 agg: metricdata.Sum[N]{
380 IsMonotonic: mono,
381 Temporality: metricdata.DeltaTemporality,
382 DataPoints: []metricdata.DataPoint[N]{
383 {
384 Attributes: fltrAlice,
385 StartTime: y2kPlus(4),
386 Time: y2kPlus(7),
387 Value: 7,
388 },
389 {
390 Attributes: fltrBob,
391 StartTime: y2kPlus(4),
392 Time: y2kPlus(7),
393 Value: 14,
394 },
395 },
396 },
397 },
398 },
399 {
400 input: []arg[N]{},
401 // Precomputed sums are expected to reset.
402 expect: output{
403 n: 0,
404 agg: metricdata.Sum[N]{
405 IsMonotonic: mono,
406 Temporality: metricdata.DeltaTemporality,
407 DataPoints: []metricdata.DataPoint[N]{},
408 },
409 },
410 },
411 {
412 input: []arg[N]{
413 {ctx, 1, alice},
414 {ctx, 1, bob},
415 // These will exceed cardinality limit.
416 {ctx, 1, carol},
417 {ctx, 1, dave},
418 },
419 expect: output{
420 n: 3,
421 agg: metricdata.Sum[N]{
422 IsMonotonic: mono,
423 Temporality: metricdata.DeltaTemporality,
424 DataPoints: []metricdata.DataPoint[N]{
425 {
426 Attributes: fltrAlice,
427 StartTime: y2kPlus(8),
428 Time: y2kPlus(12),
429 Value: 1,
430 },
431 {
432 Attributes: fltrBob,
433 StartTime: y2kPlus(8),
434 Time: y2kPlus(12),
435 Value: 1,
436 },
437 {
438 Attributes: overflowSet,
439 StartTime: y2kPlus(8),
440 Time: y2kPlus(12),
441 Value: 2,
442 },
443 },
444 },
445 },
446 },
447 })
448 }
449
450 func testCumulativePrecomputedSum[N int64 | float64]() func(t *testing.T) {
451 mono := false
452 in, out := Builder[N]{
453 Temporality: metricdata.CumulativeTemporality,
454 Filter: attrFltr,
455 AggregationLimit: 3,
456 }.PrecomputedSum(mono)
457 ctx := context.Background()
458 return test[N](in, out, []teststep[N]{
459 {
460 input: []arg[N]{},
461 expect: output{
462 n: 0,
463 agg: metricdata.Sum[N]{
464 IsMonotonic: mono,
465 Temporality: metricdata.CumulativeTemporality,
466 DataPoints: []metricdata.DataPoint[N]{},
467 },
468 },
469 },
470 {
471 input: []arg[N]{
472 {ctx, 1, alice},
473 {ctx, -1, bob},
474 {ctx, 1, fltrAlice},
475 {ctx, 2, alice},
476 {ctx, -10, bob},
477 },
478 expect: output{
479 n: 2,
480 agg: metricdata.Sum[N]{
481 IsMonotonic: mono,
482 Temporality: metricdata.CumulativeTemporality,
483 DataPoints: []metricdata.DataPoint[N]{
484 {
485 Attributes: fltrAlice,
486 StartTime: y2kPlus(0),
487 Time: y2kPlus(4),
488 Value: 4,
489 },
490 {
491 Attributes: fltrBob,
492 StartTime: y2kPlus(0),
493 Time: y2kPlus(4),
494 Value: -11,
495 },
496 },
497 },
498 },
499 },
500 {
501 input: []arg[N]{
502 {ctx, 1, fltrAlice},
503 {ctx, 10, alice},
504 {ctx, 3, bob},
505 },
506 expect: output{
507 n: 2,
508 agg: metricdata.Sum[N]{
509 IsMonotonic: mono,
510 Temporality: metricdata.CumulativeTemporality,
511 DataPoints: []metricdata.DataPoint[N]{
512 {
513 Attributes: fltrAlice,
514 StartTime: y2kPlus(0),
515 Time: y2kPlus(7),
516 Value: 11,
517 },
518 {
519 Attributes: fltrBob,
520 StartTime: y2kPlus(0),
521 Time: y2kPlus(7),
522 Value: 3,
523 },
524 },
525 },
526 },
527 },
528 {
529 input: []arg[N]{},
530 // Precomputed sums are expected to reset.
531 expect: output{
532 n: 0,
533 agg: metricdata.Sum[N]{
534 IsMonotonic: mono,
535 Temporality: metricdata.CumulativeTemporality,
536 DataPoints: []metricdata.DataPoint[N]{},
537 },
538 },
539 },
540 {
541 input: []arg[N]{
542 {ctx, 1, alice},
543 {ctx, 1, bob},
544 // These will exceed cardinality limit.
545 {ctx, 1, carol},
546 {ctx, 1, dave},
547 },
548 expect: output{
549 n: 3,
550 agg: metricdata.Sum[N]{
551 IsMonotonic: mono,
552 Temporality: metricdata.CumulativeTemporality,
553 DataPoints: []metricdata.DataPoint[N]{
554 {
555 Attributes: fltrAlice,
556 StartTime: y2kPlus(0),
557 Time: y2kPlus(12),
558 Value: 1,
559 },
560 {
561 Attributes: fltrBob,
562 StartTime: y2kPlus(0),
563 Time: y2kPlus(12),
564 Value: 1,
565 },
566 {
567 Attributes: overflowSet,
568 StartTime: y2kPlus(0),
569 Time: y2kPlus(12),
570 Value: 2,
571 },
572 },
573 },
574 },
575 },
576 })
577 }
578
579 func TestSumConcurrentSafe(t *testing.T) {
580 t.Run("Int64/DeltaSum", testDeltaSumConcurrentSafe[int64]())
581 t.Run("Float64/DeltaSum", testDeltaSumConcurrentSafe[float64]())
582 t.Run("Int64/CumulativeSum", testCumulativeSumConcurrentSafe[int64]())
583 t.Run("Float64/CumulativeSum", testCumulativeSumConcurrentSafe[float64]())
584 t.Run("Int64/DeltaPrecomputedSum", testDeltaPrecomputedSumConcurrentSafe[int64]())
585 t.Run("Float64/DeltaPrecomputedSum", testDeltaPrecomputedSumConcurrentSafe[float64]())
586 t.Run("Int64/CumulativePrecomputedSum", testCumulativePrecomputedSumConcurrentSafe[int64]())
587 t.Run("Float64/CumulativePrecomputedSum", testCumulativePrecomputedSumConcurrentSafe[float64]())
588 }
589
590 //nolint:revive // isPrecomputed is used for configuring validation
591 func validateSum[N int64 | float64](isPrecomputed bool) func(t *testing.T, aggs []metricdata.Aggregation) {
592 return func(t *testing.T, aggs []metricdata.Aggregation) {
593 sums := make(map[attribute.Set]N)
594 for i, agg := range aggs {
595 s, ok := agg.(metricdata.Sum[N])
596 require.True(t, ok)
597 require.LessOrEqual(t, len(s.DataPoints), 3, "AggregationLimit of 3 exceeded in a single cycle")
598 for _, dp := range s.DataPoints {
599 if s.Temporality == metricdata.DeltaTemporality {
600 sums[dp.Attributes] += dp.Value
601 } else if i == len(aggs)-1 {
602 sums[dp.Attributes] = dp.Value
603 }
604 }
605 }
606
607 if isPrecomputed {
608 // Precomputed Sums clear the state when collected concurrently. Due to hot/cold overlap
609 // during flush, the sum drops intermediate updates, so the final calculation won't cleanly
610 // add up to the total number of operations performed by the workers. Therefore, skip exact
611 // invariant check, verifying only that limits and map updates occurred safely.
612 return
613 }
614
615 var total N
616 for _, val := range sums {
617 total += val
618 }
619
620 assertSumEqual[N](t, expectedConcurrentSum[N](), total)
621 }
622 }
623
624 func testDeltaSumConcurrentSafe[N int64 | float64]() func(t *testing.T) {
625 in, out := Builder[N]{
626 Temporality: metricdata.DeltaTemporality,
627 Filter: attrFltr,
628 AggregationLimit: 3,
629 }.Sum(false)
630 return testAggregationConcurrentSafe[N](in, out, validateSum[N](false))
631 }
632
633 func testCumulativeSumConcurrentSafe[N int64 | float64]() func(t *testing.T) {
634 in, out := Builder[N]{
635 Temporality: metricdata.CumulativeTemporality,
636 Filter: attrFltr,
637 AggregationLimit: 3,
638 }.Sum(false)
639 return testAggregationConcurrentSafe[N](in, out, validateSum[N](false))
640 }
641
642 func testDeltaPrecomputedSumConcurrentSafe[N int64 | float64]() func(t *testing.T) {
643 in, out := Builder[N]{
644 Temporality: metricdata.DeltaTemporality,
645 Filter: attrFltr,
646 AggregationLimit: 3,
647 }.PrecomputedSum(false)
648 return testAggregationConcurrentSafe[N](in, out, validateSum[N](true))
649 }
650
651 func testCumulativePrecomputedSumConcurrentSafe[N int64 | float64]() func(t *testing.T) {
652 in, out := Builder[N]{
653 Temporality: metricdata.CumulativeTemporality,
654 Filter: attrFltr,
655 AggregationLimit: 3,
656 }.PrecomputedSum(false)
657 return testAggregationConcurrentSafe[N](in, out, validateSum[N](true))
658 }
659
660 func BenchmarkSum(b *testing.B) {
661 // The monotonic argument is only used to annotate the Sum returned from
662 // the Aggregation method. It should not have an effect on operational
663 // performance, therefore, only monotonic=false is benchmarked here.
664 b.Run("Int64/Cumulative", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) {
665 return Builder[int64]{
666 Temporality: metricdata.CumulativeTemporality,
667 }.Sum(false)
668 }))
669 b.Run("Int64/Delta", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) {
670 return Builder[int64]{
671 Temporality: metricdata.DeltaTemporality,
672 }.Sum(false)
673 }))
674 b.Run("Float64/Cumulative", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) {
675 return Builder[float64]{
676 Temporality: metricdata.CumulativeTemporality,
677 }.Sum(false)
678 }))
679 b.Run("Float64/Delta", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) {
680 return Builder[float64]{
681 Temporality: metricdata.DeltaTemporality,
682 }.Sum(false)
683 }))
684
685 b.Run("Precomputed/Int64/Cumulative", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) {
686 return Builder[int64]{
687 Temporality: metricdata.CumulativeTemporality,
688 }.PrecomputedSum(false)
689 }))
690 b.Run("Precomputed/Int64/Delta", benchmarkAggregate(func() (Measure[int64], ComputeAggregation) {
691 return Builder[int64]{
692 Temporality: metricdata.DeltaTemporality,
693 }.PrecomputedSum(false)
694 }))
695 b.Run("Precomputed/Float64/Cumulative", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) {
696 return Builder[float64]{
697 Temporality: metricdata.CumulativeTemporality,
698 }.PrecomputedSum(false)
699 }))
700 b.Run("Precomputed/Float64/Delta", benchmarkAggregate(func() (Measure[float64], ComputeAggregation) {
701 return Builder[float64]{
702 Temporality: metricdata.DeltaTemporality,
703 }.PrecomputedSum(false)
704 }))
705 }