Debian Codegraph @

sdk/metric/internal/aggregate/lastvalue_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/sdk/internal/x"
14 "go.opentelemetry.io/otel/sdk/metric/metricdata"
15 )
16
17 func TestLastValue(t *testing.T) {
18 c := new(clock)
19 t.Cleanup(c.Register())
20
21 t.Run("Int64/DeltaLastValue", testDeltaLastValue[int64]())
22 c.Reset()
23 t.Run("Float64/DeltaLastValue", testDeltaLastValue[float64]())
24 c.Reset()
25
26 t.Run("Int64/CumulativeLastValue", func(t *testing.T) {
27 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
28 assert.False(t, x.PerSeriesStartTimestamps.Enabled())
29 testCumulativeLastValue[int64]()(t)
30 })
31 c.Reset()
32
33 t.Run("Int64/CumulativeLastValue/PerSeriesStartTimeEnabled", func(t *testing.T) {
34 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
35 assert.True(t, x.PerSeriesStartTimestamps.Enabled())
36 testCumulativeLastValue[int64]()(t)
37 })
38 c.Reset()
39
40 t.Run("Float64/CumulativeLastValue", func(t *testing.T) {
41 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
42 assert.False(t, x.PerSeriesStartTimestamps.Enabled())
43 testCumulativeLastValue[float64]()(t)
44 })
45 c.Reset()
46
47 t.Run("Float64/CumulativeLastValue/PerSeriesStartTimeEnabled", func(t *testing.T) {
48 t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
49 assert.True(t, x.PerSeriesStartTimestamps.Enabled())
50 testCumulativeLastValue[float64]()(t)
51 })
52 c.Reset()
53
54 t.Run("Int64/DeltaPrecomputedLastValue", testDeltaPrecomputedLastValue[int64]())
55 c.Reset()
56 t.Run("Float64/DeltaPrecomputedLastValue", testDeltaPrecomputedLastValue[float64]())
57 c.Reset()
58
59 t.Run("Int64/CumulativePrecomputedLastValue", testCumulativePrecomputedLastValue[int64]())
60 c.Reset()
61 t.Run("Float64/CumulativePrecomputedLastValue", testCumulativePrecomputedLastValue[float64]())
62 }
63
64 func testDeltaLastValue[N int64 | float64]() func(*testing.T) {
65 in, out := Builder[N]{
66 Temporality: metricdata.DeltaTemporality,
67 Filter: attrFltr,
68 AggregationLimit: 3,
69 }.LastValue()
70 ctx := context.Background()
71 return test[N](in, out, []teststep[N]{
72 {
73 // Empty output if nothing is measured.
74 input: []arg[N]{},
75 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
76 }, {
77 input: []arg[N]{
78 {ctx, 1, alice},
79 {ctx, -1, bob},
80 {ctx, 1, fltrAlice},
81 {ctx, 2, alice},
82 {ctx, -10, bob},
83 },
84 expect: output{
85 n: 2,
86 agg: metricdata.Gauge[N]{
87 DataPoints: []metricdata.DataPoint[N]{
88 {
89 Attributes: fltrAlice,
90 StartTime: y2kPlus(1),
91 Time: y2kPlus(4),
92 Value: 2,
93 },
94 {
95 Attributes: fltrBob,
96 StartTime: y2kPlus(1),
97 Time: y2kPlus(4),
98 Value: -10,
99 },
100 },
101 },
102 },
103 }, {
104 // Everything resets, do not report old measurements.
105 input: []arg[N]{},
106 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
107 }, {
108 input: []arg[N]{
109 {ctx, 10, alice},
110 {ctx, 3, bob},
111 },
112 expect: output{
113 n: 2,
114 agg: metricdata.Gauge[N]{
115 DataPoints: []metricdata.DataPoint[N]{
116 {
117 Attributes: fltrAlice,
118 StartTime: y2kPlus(5),
119 Time: y2kPlus(8),
120 Value: 10,
121 },
122 {
123 Attributes: fltrBob,
124 StartTime: y2kPlus(5),
125 Time: y2kPlus(8),
126 Value: 3,
127 },
128 },
129 },
130 },
131 }, {
132 input: []arg[N]{
133 {ctx, 1, alice},
134 {ctx, 1, bob},
135 // These will exceed cardinality limit.
136 {ctx, 1, carol},
137 {ctx, 1, dave},
138 },
139 expect: output{
140 n: 3,
141 agg: metricdata.Gauge[N]{
142 DataPoints: []metricdata.DataPoint[N]{
143 {
144 Attributes: fltrAlice,
145 StartTime: y2kPlus(8),
146 Time: y2kPlus(12),
147 Value: 1,
148 },
149 {
150 Attributes: fltrBob,
151 StartTime: y2kPlus(8),
152 Time: y2kPlus(12),
153 Value: 1,
154 },
155 {
156 Attributes: overflowSet,
157 StartTime: y2kPlus(8),
158 Time: y2kPlus(12),
159 Value: 1,
160 },
161 },
162 },
163 },
164 },
165 })
166 }
167
168 func testCumulativeLastValue[N int64 | float64]() func(*testing.T) {
169 in, out := Builder[N]{
170 Temporality: metricdata.CumulativeTemporality,
171 Filter: attrFltr,
172 AggregationLimit: 3,
173 }.LastValue()
174
175 aliceStartTime := y2kPlus(0)
176 bobStartTime := y2kPlus(0)
177 overflowStartTime := y2kPlus(0)
178
179 if x.PerSeriesStartTimestamps.Enabled() {
180 aliceStartTime = y2kPlus(2)
181 bobStartTime = y2kPlus(3)
182 overflowStartTime = y2kPlus(7)
183 }
184
185 ctx := context.Background()
186 return test[N](in, out, []teststep[N]{
187 {
188 // Empty output if nothing is measured.
189 input: []arg[N]{},
190 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
191 }, {
192 input: []arg[N]{
193 {ctx, 1, alice},
194 {ctx, -1, bob},
195 {ctx, 1, fltrAlice},
196 {ctx, 2, alice},
197 {ctx, -10, bob},
198 },
199 expect: output{
200 n: 2,
201 agg: metricdata.Gauge[N]{
202 DataPoints: []metricdata.DataPoint[N]{
203 {
204 Attributes: fltrAlice,
205 StartTime: aliceStartTime,
206 Time: y2kPlus(4),
207 Value: 2,
208 },
209 {
210 Attributes: fltrBob,
211 StartTime: bobStartTime,
212 Time: y2kPlus(4),
213 Value: -10,
214 },
215 },
216 },
217 },
218 }, {
219 // Cumulative temporality means no resets.
220 input: []arg[N]{},
221 expect: output{
222 n: 2,
223 agg: metricdata.Gauge[N]{
224 DataPoints: []metricdata.DataPoint[N]{
225 {
226 Attributes: fltrAlice,
227 StartTime: aliceStartTime,
228 Time: y2kPlus(5),
229 Value: 2,
230 },
231 {
232 Attributes: fltrBob,
233 StartTime: bobStartTime,
234 Time: y2kPlus(5),
235 Value: -10,
236 },
237 },
238 },
239 },
240 }, {
241 input: []arg[N]{
242 {ctx, 10, alice},
243 {ctx, 3, bob},
244 },
245 expect: output{
246 n: 2,
247 agg: metricdata.Gauge[N]{
248 DataPoints: []metricdata.DataPoint[N]{
249 {
250 Attributes: fltrAlice,
251 StartTime: aliceStartTime,
252 Time: y2kPlus(6),
253 Value: 10,
254 },
255 {
256 Attributes: fltrBob,
257 StartTime: bobStartTime,
258 Time: y2kPlus(6),
259 Value: 3,
260 },
261 },
262 },
263 },
264 }, {
265 input: []arg[N]{
266 {ctx, 1, alice},
267 {ctx, 1, bob},
268 // These will exceed cardinality limit.
269 {ctx, 1, carol},
270 {ctx, 1, dave},
271 },
272 expect: output{
273 n: 3,
274 agg: metricdata.Gauge[N]{
275 DataPoints: []metricdata.DataPoint[N]{
276 {
277 Attributes: fltrAlice,
278 StartTime: aliceStartTime,
279 Time: y2kPlus(8),
280 Value: 1,
281 },
282 {
283 Attributes: fltrBob,
284 StartTime: bobStartTime,
285 Time: y2kPlus(8),
286 Value: 1,
287 },
288 {
289 Attributes: overflowSet,
290 StartTime: overflowStartTime,
291 Time: y2kPlus(8),
292 Value: 1,
293 },
294 },
295 },
296 },
297 },
298 })
299 }
300
301 func testDeltaPrecomputedLastValue[N int64 | float64]() func(*testing.T) {
302 in, out := Builder[N]{
303 Temporality: metricdata.DeltaTemporality,
304 Filter: attrFltr,
305 AggregationLimit: 3,
306 }.PrecomputedLastValue()
307 ctx := context.Background()
308 return test[N](in, out, []teststep[N]{
309 {
310 // Empty output if nothing is measured.
311 input: []arg[N]{},
312 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
313 }, {
314 input: []arg[N]{
315 {ctx, 1, alice},
316 {ctx, -1, bob},
317 {ctx, 1, fltrAlice},
318 {ctx, 2, alice},
319 {ctx, -10, bob},
320 },
321 expect: output{
322 n: 2,
323 agg: metricdata.Gauge[N]{
324 DataPoints: []metricdata.DataPoint[N]{
325 {
326 Attributes: fltrAlice,
327 StartTime: y2kPlus(1),
328 Time: y2kPlus(4),
329 Value: 2,
330 },
331 {
332 Attributes: fltrBob,
333 StartTime: y2kPlus(1),
334 Time: y2kPlus(4),
335 Value: -10,
336 },
337 },
338 },
339 },
340 }, {
341 // Everything resets, do not report old measurements.
342 input: []arg[N]{},
343 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
344 }, {
345 input: []arg[N]{
346 {ctx, 10, alice},
347 {ctx, 3, bob},
348 },
349 expect: output{
350 n: 2,
351 agg: metricdata.Gauge[N]{
352 DataPoints: []metricdata.DataPoint[N]{
353 {
354 Attributes: fltrAlice,
355 StartTime: y2kPlus(5),
356 Time: y2kPlus(8),
357 Value: 10,
358 },
359 {
360 Attributes: fltrBob,
361 StartTime: y2kPlus(5),
362 Time: y2kPlus(8),
363 Value: 3,
364 },
365 },
366 },
367 },
368 }, {
369 input: []arg[N]{
370 {ctx, 1, alice},
371 {ctx, 1, bob},
372 // These will exceed cardinality limit.
373 {ctx, 1, carol},
374 {ctx, 1, dave},
375 },
376 expect: output{
377 n: 3,
378 agg: metricdata.Gauge[N]{
379 DataPoints: []metricdata.DataPoint[N]{
380 {
381 Attributes: fltrAlice,
382 StartTime: y2kPlus(8),
383 Time: y2kPlus(12),
384 Value: 1,
385 },
386 {
387 Attributes: fltrBob,
388 StartTime: y2kPlus(8),
389 Time: y2kPlus(12),
390 Value: 1,
391 },
392 {
393 Attributes: overflowSet,
394 StartTime: y2kPlus(8),
395 Time: y2kPlus(12),
396 Value: 1,
397 },
398 },
399 },
400 },
401 },
402 })
403 }
404
405 func testCumulativePrecomputedLastValue[N int64 | float64]() func(*testing.T) {
406 in, out := Builder[N]{
407 Temporality: metricdata.CumulativeTemporality,
408 Filter: attrFltr,
409 AggregationLimit: 3,
410 }.PrecomputedLastValue()
411 ctx := context.Background()
412 return test[N](in, out, []teststep[N]{
413 {
414 // Empty output if nothing is measured.
415 input: []arg[N]{},
416 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
417 }, {
418 input: []arg[N]{
419 {ctx, 1, alice},
420 {ctx, -1, bob},
421 {ctx, 1, fltrAlice},
422 {ctx, 2, alice},
423 {ctx, -10, bob},
424 },
425 expect: output{
426 n: 2,
427 agg: metricdata.Gauge[N]{
428 DataPoints: []metricdata.DataPoint[N]{
429 {
430 Attributes: fltrAlice,
431 StartTime: y2kPlus(0),
432 Time: y2kPlus(4),
433 Value: 2,
434 },
435 {
436 Attributes: fltrBob,
437 StartTime: y2kPlus(0),
438 Time: y2kPlus(4),
439 Value: -10,
440 },
441 },
442 },
443 },
444 }, {
445 // Everything resets, do not report old measurements.
446 input: []arg[N]{},
447 expect: output{n: 0, agg: metricdata.Gauge[N]{}},
448 }, {
449 input: []arg[N]{
450 {ctx, 10, alice},
451 {ctx, 3, bob},
452 },
453 expect: output{
454 n: 2,
455 agg: metricdata.Gauge[N]{
456 DataPoints: []metricdata.DataPoint[N]{
457 {
458 Attributes: fltrAlice,
459 StartTime: y2kPlus(0),
460 Time: y2kPlus(8),
461 Value: 10,
462 },
463 {
464 Attributes: fltrBob,
465 StartTime: y2kPlus(0),
466 Time: y2kPlus(8),
467 Value: 3,
468 },
469 },
470 },
471 },
472 }, {
473 input: []arg[N]{
474 {ctx, 1, alice},
475 {ctx, 1, bob},
476 // These will exceed cardinality limit.
477 {ctx, 1, carol},
478 {ctx, 1, dave},
479 },
480 expect: output{
481 n: 3,
482 agg: metricdata.Gauge[N]{
483 DataPoints: []metricdata.DataPoint[N]{
484 {
485 Attributes: fltrAlice,
486 StartTime: y2kPlus(0),
487 Time: y2kPlus(12),
488 Value: 1,
489 },
490 {
491 Attributes: fltrBob,
492 StartTime: y2kPlus(0),
493 Time: y2kPlus(12),
494 Value: 1,
495 },
496 {
497 Attributes: overflowSet,
498 StartTime: y2kPlus(0),
499 Time: y2kPlus(12),
500 Value: 1,
501 },
502 },
503 },
504 },
505 },
506 })
507 }
508
509 func TestLastValueConcurrentSafe(t *testing.T) {
510 t.Run("Int64/DeltaLastValue", testDeltaLastValueConcurrentSafe[int64]())
511 t.Run("Float64/DeltaLastValue", testDeltaLastValueConcurrentSafe[float64]())
512 t.Run("Int64/CumulativeLastValue", testCumulativeLastValueConcurrentSafe[int64]())
513 t.Run("Float64/CumulativeLastValue", testCumulativeLastValueConcurrentSafe[float64]())
514 t.Run("Int64/DeltaPrecomputedLastValue", testDeltaPrecomputedLastValueConcurrentSafe[int64]())
515 t.Run("Float64/DeltaPrecomputedLastValue", testDeltaPrecomputedLastValueConcurrentSafe[float64]())
516 t.Run("Int64/CumulativePrecomputedLastValue", testCumulativePrecomputedLastValueConcurrentSafe[int64]())
517 t.Run("Float64/CumulativePrecomputedLastValue", testCumulativePrecomputedLastValueConcurrentSafe[float64]())
518 }
519
520 func validateGauge[N int64 | float64](t *testing.T, aggs []metricdata.Aggregation) {
521 // A gauge takes the *last* recorded value.
522 // During high concurrency, reading the Gauge can snap any value in the
523 // iteration cycle of the corresponding Goroutines.
524 valid := make(map[N]bool)
525 for _, v := range getConcurrentVals[N]() {
526 valid[v] = true
527 }
528
529 for _, agg := range aggs {
530 s, ok := agg.(metricdata.Gauge[N])
531 require.True(t, ok)
532 require.LessOrEqual(t, len(s.DataPoints), 3, "AggregationLimit of 3 exceeded")
533 for _, dp := range s.DataPoints {
534 assert.True(t, valid[dp.Value], "Unexpected gauge value: %v", dp.Value)
535 }
536 }
537 }
538
539 func testCumulativeLastValueConcurrentSafe[N int64 | float64]() func(*testing.T) {
540 in, out := Builder[N]{
541 Temporality: metricdata.CumulativeTemporality,
542 Filter: attrFltr,
543 AggregationLimit: 3,
544 }.LastValue()
545 return testAggregationConcurrentSafe[N](in, out, validateGauge[N])
546 }
547
548 func testDeltaLastValueConcurrentSafe[N int64 | float64]() func(*testing.T) {
549 in, out := Builder[N]{
550 Temporality: metricdata.DeltaTemporality,
551 Filter: attrFltr,
552 AggregationLimit: 3,
553 }.LastValue()
554 return testAggregationConcurrentSafe[N](in, out, validateGauge[N])
555 }
556
557 func testDeltaPrecomputedLastValueConcurrentSafe[N int64 | float64]() func(*testing.T) {
558 in, out := Builder[N]{
559 Temporality: metricdata.DeltaTemporality,
560 Filter: attrFltr,
561 AggregationLimit: 3,
562 }.PrecomputedLastValue()
563 return testAggregationConcurrentSafe[N](in, out, validateGauge[N])
564 }
565
566 func testCumulativePrecomputedLastValueConcurrentSafe[N int64 | float64]() func(*testing.T) {
567 in, out := Builder[N]{
568 Temporality: metricdata.CumulativeTemporality,
569 Filter: attrFltr,
570 AggregationLimit: 3,
571 }.PrecomputedLastValue()
572 return testAggregationConcurrentSafe[N](in, out, validateGauge[N])
573 }
574
575 func BenchmarkLastValue(b *testing.B) {
576 b.Run("Int64", benchmarkAggregate(Builder[int64]{}.PrecomputedLastValue))
577 b.Run("Float64", benchmarkAggregate(Builder[float64]{}.PrecomputedLastValue))
578 }