Debian Codegraph @

bridge/opencensus/internal/ocmetric/metric_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal // import "go.opentelemetry.io/otel/bridge/opencensus/opencensusmetric/internal"
5
6 import (
7 "errors"
8 "fmt"
9 "math"
10 "math/rand/v2"
11 "reflect"
12 "strconv"
13 "testing"
14 "time"
15
16 "github.com/stretchr/testify/assert"
17 ocmetricdata "go.opencensus.io/metric/metricdata"
18 octrace "go.opencensus.io/trace"
19
20 "go.opentelemetry.io/otel/attribute"
21 "go.opentelemetry.io/otel/sdk/metric/metricdata"
22 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
23 )
24
25 func TestConvertMetrics(t *testing.T) {
26 endTime1 := time.Now()
27 exemplarTime := endTime1.Add(-10 * time.Second)
28 endTime2 := endTime1.Add(-time.Millisecond)
29 startTime := endTime2.Add(-time.Minute)
30 for _, tc := range []struct {
31 desc string
32 input []*ocmetricdata.Metric
33 expected []metricdata.Metrics
34 expectedErr error
35 }{
36 {
37 desc: "empty",
38 expected: []metricdata.Metrics{},
39 },
40 {
41 desc: "normal Histogram, summary, gauges, and sums",
42 input: []*ocmetricdata.Metric{
43 {
44 Descriptor: ocmetricdata.Descriptor{
45 Name: "foo.com/histogram-a",
46 Description: "a testing histogram",
47 Unit: ocmetricdata.UnitDimensionless,
48 Type: ocmetricdata.TypeCumulativeDistribution,
49 LabelKeys: []ocmetricdata.LabelKey{
50 {Key: "a"},
51 {Key: "b"},
52 },
53 },
54 TimeSeries: []*ocmetricdata.TimeSeries{
55 {
56 LabelValues: []ocmetricdata.LabelValue{
57 {
58 Value: "hello",
59 Present: true,
60 }, {
61 Value: "world",
62 Present: true,
63 },
64 },
65 Points: []ocmetricdata.Point{
66 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{
67 Count: 8,
68 Sum: 100.0,
69 BucketOptions: &ocmetricdata.BucketOptions{
70 Bounds: []float64{1.0, 2.0, 3.0},
71 },
72 Buckets: []ocmetricdata.Bucket{
73 {
74 Count: 1,
75 Exemplar: &ocmetricdata.Exemplar{
76 Value: 0.8,
77 Timestamp: exemplarTime,
78 Attachments: map[string]any{
79 ocmetricdata.AttachmentKeySpanContext: octrace.SpanContext{
80 TraceID: octrace.TraceID([16]byte{1}),
81 SpanID: octrace.SpanID([8]byte{2}),
82 },
83 "bool": true,
84 },
85 },
86 },
87 {
88 Count: 2,
89 Exemplar: &ocmetricdata.Exemplar{
90 Value: 1.5,
91 Timestamp: exemplarTime,
92 Attachments: map[string]any{
93 ocmetricdata.AttachmentKeySpanContext: octrace.SpanContext{
94 TraceID: octrace.TraceID([16]byte{3}),
95 SpanID: octrace.SpanID([8]byte{4}),
96 },
97 },
98 },
99 },
100 {
101 Count: 5,
102 Exemplar: &ocmetricdata.Exemplar{
103 Value: 2.6,
104 Timestamp: exemplarTime,
105 Attachments: map[string]any{
106 ocmetricdata.AttachmentKeySpanContext: octrace.SpanContext{
107 TraceID: octrace.TraceID([16]byte{5}),
108 SpanID: octrace.SpanID([8]byte{6}),
109 },
110 },
111 },
112 },
113 },
114 }),
115 ocmetricdata.NewDistributionPoint(endTime2, &ocmetricdata.Distribution{
116 Count: 10,
117 Sum: 110.0,
118 BucketOptions: &ocmetricdata.BucketOptions{
119 Bounds: []float64{1.0, 2.0, 3.0},
120 },
121 Buckets: []ocmetricdata.Bucket{
122 {
123 Count: 1,
124 Exemplar: &ocmetricdata.Exemplar{
125 Value: 0.9,
126 Timestamp: exemplarTime,
127 Attachments: map[string]any{
128 ocmetricdata.AttachmentKeySpanContext: octrace.SpanContext{
129 TraceID: octrace.TraceID([16]byte{7}),
130 SpanID: octrace.SpanID([8]byte{8}),
131 },
132 },
133 },
134 },
135 {
136 Count: 4,
137 Exemplar: &ocmetricdata.Exemplar{
138 Value: 1.1,
139 Timestamp: exemplarTime,
140 Attachments: map[string]any{
141 ocmetricdata.AttachmentKeySpanContext: octrace.SpanContext{
142 TraceID: octrace.TraceID([16]byte{9}),
143 SpanID: octrace.SpanID([8]byte{10}),
144 },
145 },
146 },
147 },
148 {
149 Count: 5,
150 Exemplar: &ocmetricdata.Exemplar{
151 Value: 2.7,
152 Timestamp: exemplarTime,
153 Attachments: map[string]any{
154 ocmetricdata.AttachmentKeySpanContext: octrace.SpanContext{
155 TraceID: octrace.TraceID([16]byte{11}),
156 SpanID: octrace.SpanID([8]byte{12}),
157 },
158 },
159 },
160 },
161 },
162 }),
163 },
164 StartTime: startTime,
165 },
166 },
167 }, {
168 Descriptor: ocmetricdata.Descriptor{
169 Name: "foo.com/gauge-a",
170 Description: "an int testing gauge",
171 Unit: ocmetricdata.UnitBytes,
172 Type: ocmetricdata.TypeGaugeInt64,
173 LabelKeys: []ocmetricdata.LabelKey{
174 {Key: "c"},
175 {Key: "d"},
176 },
177 },
178 TimeSeries: []*ocmetricdata.TimeSeries{
179 {
180 LabelValues: []ocmetricdata.LabelValue{
181 {
182 Value: "foo",
183 Present: true,
184 }, {
185 Value: "bar",
186 Present: true,
187 },
188 },
189 Points: []ocmetricdata.Point{
190 ocmetricdata.NewInt64Point(endTime1, 123),
191 ocmetricdata.NewInt64Point(endTime2, 1236),
192 },
193 },
194 },
195 }, {
196 Descriptor: ocmetricdata.Descriptor{
197 Name: "foo.com/gauge-b",
198 Description: "a float testing gauge",
199 Unit: ocmetricdata.UnitBytes,
200 Type: ocmetricdata.TypeGaugeFloat64,
201 LabelKeys: []ocmetricdata.LabelKey{
202 {Key: "cf"},
203 {Key: "df"},
204 },
205 },
206 TimeSeries: []*ocmetricdata.TimeSeries{
207 {
208 LabelValues: []ocmetricdata.LabelValue{
209 {
210 Value: "foof",
211 Present: true,
212 }, {
213 Value: "barf",
214 Present: true,
215 },
216 },
217 Points: []ocmetricdata.Point{
218 ocmetricdata.NewFloat64Point(endTime1, 123.4),
219 ocmetricdata.NewFloat64Point(endTime2, 1236.7),
220 },
221 },
222 },
223 }, {
224 Descriptor: ocmetricdata.Descriptor{
225 Name: "foo.com/sum-a",
226 Description: "an int testing sum",
227 Unit: ocmetricdata.UnitMilliseconds,
228 Type: ocmetricdata.TypeCumulativeInt64,
229 LabelKeys: []ocmetricdata.LabelKey{
230 {Key: "e"},
231 {Key: "f"},
232 },
233 },
234 TimeSeries: []*ocmetricdata.TimeSeries{
235 {
236 LabelValues: []ocmetricdata.LabelValue{
237 {
238 Value: "zig",
239 Present: true,
240 }, {
241 Value: "zag",
242 Present: true,
243 },
244 },
245 Points: []ocmetricdata.Point{
246 ocmetricdata.NewInt64Point(endTime1, 13),
247 ocmetricdata.NewInt64Point(endTime2, 14),
248 },
249 },
250 },
251 }, {
252 Descriptor: ocmetricdata.Descriptor{
253 Name: "foo.com/sum-b",
254 Description: "a float testing sum",
255 Unit: ocmetricdata.UnitMilliseconds,
256 Type: ocmetricdata.TypeCumulativeFloat64,
257 LabelKeys: []ocmetricdata.LabelKey{
258 {Key: "e"},
259 {Key: "f"},
260 },
261 },
262 TimeSeries: []*ocmetricdata.TimeSeries{
263 {
264 LabelValues: []ocmetricdata.LabelValue{
265 {
266 Value: "zig",
267 Present: true,
268 }, {
269 Value: "zag",
270 Present: true,
271 },
272 },
273 Points: []ocmetricdata.Point{
274 ocmetricdata.NewFloat64Point(endTime1, 12.3),
275 ocmetricdata.NewFloat64Point(endTime2, 123.4),
276 },
277 },
278 },
279 }, {
280 Descriptor: ocmetricdata.Descriptor{
281 Name: "foo.com/summary-a",
282 Description: "a testing summary",
283 Unit: ocmetricdata.UnitMilliseconds,
284 Type: ocmetricdata.TypeSummary,
285 LabelKeys: []ocmetricdata.LabelKey{
286 {Key: "g"},
287 {Key: "h"},
288 },
289 },
290 TimeSeries: []*ocmetricdata.TimeSeries{
291 {
292 LabelValues: []ocmetricdata.LabelValue{
293 {
294 Value: "ding",
295 Present: true,
296 }, {
297 Value: "dong",
298 Present: true,
299 },
300 },
301 Points: []ocmetricdata.Point{
302 ocmetricdata.NewSummaryPoint(endTime1, &ocmetricdata.Summary{
303 Count: 10,
304 Sum: 13.2,
305 HasCountAndSum: true,
306 Snapshot: ocmetricdata.Snapshot{
307 Percentiles: map[float64]float64{
308 50.0: 1.0,
309 0.0: 0.1,
310 100.0: 10.4,
311 },
312 },
313 }),
314 ocmetricdata.NewSummaryPoint(endTime2, &ocmetricdata.Summary{
315 Count: 12,
316 Snapshot: ocmetricdata.Snapshot{
317 Percentiles: map[float64]float64{
318 0.0: 0.2,
319 50.0: 1.1,
320 100.0: 10.5,
321 },
322 },
323 }),
324 },
325 },
326 },
327 },
328 },
329 expected: []metricdata.Metrics{
330 {
331 Name: "foo.com/histogram-a",
332 Description: "a testing histogram",
333 Unit: "1",
334 Data: metricdata.Histogram[float64]{
335 DataPoints: []metricdata.HistogramDataPoint[float64]{
336 {
337 Attributes: attribute.NewSet(attribute.KeyValue{
338 Key: attribute.Key("a"),
339 Value: attribute.StringValue("hello"),
340 }, attribute.KeyValue{
341 Key: attribute.Key("b"),
342 Value: attribute.StringValue("world"),
343 }),
344 StartTime: startTime,
345 Time: endTime1,
346 Count: 8,
347 Sum: 100.0,
348 Bounds: []float64{1.0, 2.0, 3.0},
349 BucketCounts: []uint64{1, 2, 5},
350 Exemplars: []metricdata.Exemplar[float64]{
351 {
352 Time: exemplarTime,
353 Value: 0.8,
354 TraceID: []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
355 SpanID: []byte{2, 0, 0, 0, 0, 0, 0, 0},
356 FilteredAttributes: []attribute.KeyValue{
357 attribute.Bool("bool", true),
358 },
359 },
360 {
361 Time: exemplarTime,
362 Value: 1.5,
363 TraceID: []byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
364 SpanID: []byte{4, 0, 0, 0, 0, 0, 0, 0},
365 },
366 {
367 Time: exemplarTime,
368 Value: 2.6,
369 TraceID: []byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
370 SpanID: []byte{6, 0, 0, 0, 0, 0, 0, 0},
371 },
372 },
373 }, {
374 Attributes: attribute.NewSet(attribute.KeyValue{
375 Key: attribute.Key("a"),
376 Value: attribute.StringValue("hello"),
377 }, attribute.KeyValue{
378 Key: attribute.Key("b"),
379 Value: attribute.StringValue("world"),
380 }),
381 StartTime: startTime,
382 Time: endTime2,
383 Count: 10,
384 Sum: 110.0,
385 Bounds: []float64{1.0, 2.0, 3.0},
386 BucketCounts: []uint64{1, 4, 5},
387 Exemplars: []metricdata.Exemplar[float64]{
388 {
389 Time: exemplarTime,
390 Value: 0.9,
391 TraceID: []byte{7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
392 SpanID: []byte{8, 0, 0, 0, 0, 0, 0, 0},
393 },
394 {
395 Time: exemplarTime,
396 Value: 1.1,
397 TraceID: []byte{9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
398 SpanID: []byte{10, 0, 0, 0, 0, 0, 0, 0},
399 },
400 {
401 Time: exemplarTime,
402 Value: 2.7,
403 TraceID: []byte{11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
404 SpanID: []byte{12, 0, 0, 0, 0, 0, 0, 0},
405 },
406 },
407 },
408 },
409 Temporality: metricdata.CumulativeTemporality,
410 },
411 }, {
412 Name: "foo.com/gauge-a",
413 Description: "an int testing gauge",
414 Unit: "By",
415 Data: metricdata.Gauge[int64]{
416 DataPoints: []metricdata.DataPoint[int64]{
417 {
418 Attributes: attribute.NewSet(attribute.KeyValue{
419 Key: attribute.Key("c"),
420 Value: attribute.StringValue("foo"),
421 }, attribute.KeyValue{
422 Key: attribute.Key("d"),
423 Value: attribute.StringValue("bar"),
424 }),
425 Time: endTime1,
426 Value: 123,
427 }, {
428 Attributes: attribute.NewSet(attribute.KeyValue{
429 Key: attribute.Key("c"),
430 Value: attribute.StringValue("foo"),
431 }, attribute.KeyValue{
432 Key: attribute.Key("d"),
433 Value: attribute.StringValue("bar"),
434 }),
435 Time: endTime2,
436 Value: 1236,
437 },
438 },
439 },
440 }, {
441 Name: "foo.com/gauge-b",
442 Description: "a float testing gauge",
443 Unit: "By",
444 Data: metricdata.Gauge[float64]{
445 DataPoints: []metricdata.DataPoint[float64]{
446 {
447 Attributes: attribute.NewSet(attribute.KeyValue{
448 Key: attribute.Key("cf"),
449 Value: attribute.StringValue("foof"),
450 }, attribute.KeyValue{
451 Key: attribute.Key("df"),
452 Value: attribute.StringValue("barf"),
453 }),
454 Time: endTime1,
455 Value: 123.4,
456 }, {
457 Attributes: attribute.NewSet(attribute.KeyValue{
458 Key: attribute.Key("cf"),
459 Value: attribute.StringValue("foof"),
460 }, attribute.KeyValue{
461 Key: attribute.Key("df"),
462 Value: attribute.StringValue("barf"),
463 }),
464 Time: endTime2,
465 Value: 1236.7,
466 },
467 },
468 },
469 }, {
470 Name: "foo.com/sum-a",
471 Description: "an int testing sum",
472 Unit: "ms",
473 Data: metricdata.Sum[int64]{
474 IsMonotonic: true,
475 Temporality: metricdata.CumulativeTemporality,
476 DataPoints: []metricdata.DataPoint[int64]{
477 {
478 Attributes: attribute.NewSet(attribute.KeyValue{
479 Key: attribute.Key("e"),
480 Value: attribute.StringValue("zig"),
481 }, attribute.KeyValue{
482 Key: attribute.Key("f"),
483 Value: attribute.StringValue("zag"),
484 }),
485 Time: endTime1,
486 Value: 13,
487 }, {
488 Attributes: attribute.NewSet(attribute.KeyValue{
489 Key: attribute.Key("e"),
490 Value: attribute.StringValue("zig"),
491 }, attribute.KeyValue{
492 Key: attribute.Key("f"),
493 Value: attribute.StringValue("zag"),
494 }),
495 Time: endTime2,
496 Value: 14,
497 },
498 },
499 },
500 }, {
501 Name: "foo.com/sum-b",
502 Description: "a float testing sum",
503 Unit: "ms",
504 Data: metricdata.Sum[float64]{
505 IsMonotonic: true,
506 Temporality: metricdata.CumulativeTemporality,
507 DataPoints: []metricdata.DataPoint[float64]{
508 {
509 Attributes: attribute.NewSet(attribute.KeyValue{
510 Key: attribute.Key("e"),
511 Value: attribute.StringValue("zig"),
512 }, attribute.KeyValue{
513 Key: attribute.Key("f"),
514 Value: attribute.StringValue("zag"),
515 }),
516 Time: endTime1,
517 Value: 12.3,
518 }, {
519 Attributes: attribute.NewSet(attribute.KeyValue{
520 Key: attribute.Key("e"),
521 Value: attribute.StringValue("zig"),
522 }, attribute.KeyValue{
523 Key: attribute.Key("f"),
524 Value: attribute.StringValue("zag"),
525 }),
526 Time: endTime2,
527 Value: 123.4,
528 },
529 },
530 },
531 }, {
532 Name: "foo.com/summary-a",
533 Description: "a testing summary",
534 Unit: "ms",
535 Data: metricdata.Summary{
536 DataPoints: []metricdata.SummaryDataPoint{
537 {
538 Attributes: attribute.NewSet(attribute.KeyValue{
539 Key: attribute.Key("g"),
540 Value: attribute.StringValue("ding"),
541 }, attribute.KeyValue{
542 Key: attribute.Key("h"),
543 Value: attribute.StringValue("dong"),
544 }),
545 Time: endTime1,
546 Count: 10,
547 Sum: 13.2,
548 QuantileValues: []metricdata.QuantileValue{
549 {
550 Quantile: 0.0,
551 Value: 0.1,
552 },
553 {
554 Quantile: 0.5,
555 Value: 1.0,
556 },
557 {
558 Quantile: 1.0,
559 Value: 10.4,
560 },
561 },
562 }, {
563 Attributes: attribute.NewSet(attribute.KeyValue{
564 Key: attribute.Key("g"),
565 Value: attribute.StringValue("ding"),
566 }, attribute.KeyValue{
567 Key: attribute.Key("h"),
568 Value: attribute.StringValue("dong"),
569 }),
570 Time: endTime2,
571 Count: 12,
572 QuantileValues: []metricdata.QuantileValue{
573 {
574 Quantile: 0.0,
575 Value: 0.2,
576 },
577 {
578 Quantile: 0.5,
579 Value: 1.1,
580 },
581 {
582 Quantile: 1.0,
583 Value: 10.5,
584 },
585 },
586 },
587 },
588 },
589 },
590 },
591 },
592 {
593 desc: "histogram without data points",
594 input: []*ocmetricdata.Metric{
595 {
596 Descriptor: ocmetricdata.Descriptor{
597 Name: "foo.com/histogram-a",
598 Description: "a testing histogram",
599 Unit: ocmetricdata.UnitDimensionless,
600 Type: ocmetricdata.TypeCumulativeDistribution,
601 },
602 },
603 },
604 expected: []metricdata.Metrics{
605 {
606 Name: "foo.com/histogram-a",
607 Description: "a testing histogram",
608 Unit: "1",
609 Data: metricdata.Histogram[float64]{
610 Temporality: metricdata.CumulativeTemporality,
611 DataPoints: []metricdata.HistogramDataPoint[float64]{},
612 },
613 },
614 },
615 },
616 {
617 desc: "sum without data points",
618 input: []*ocmetricdata.Metric{
619 {
620 Descriptor: ocmetricdata.Descriptor{
621 Name: "foo.com/sum-a",
622 Description: "a testing sum",
623 Unit: ocmetricdata.UnitDimensionless,
624 Type: ocmetricdata.TypeCumulativeFloat64,
625 },
626 },
627 },
628 expected: []metricdata.Metrics{
629 {
630 Name: "foo.com/sum-a",
631 Description: "a testing sum",
632 Unit: "1",
633 Data: metricdata.Sum[float64]{
634 IsMonotonic: true,
635 Temporality: metricdata.CumulativeTemporality,
636 DataPoints: []metricdata.DataPoint[float64]{},
637 },
638 },
639 },
640 },
641 {
642 desc: "gauge without data points",
643 input: []*ocmetricdata.Metric{
644 {
645 Descriptor: ocmetricdata.Descriptor{
646 Name: "foo.com/gauge-a",
647 Description: "a testing gauge",
648 Unit: ocmetricdata.UnitDimensionless,
649 Type: ocmetricdata.TypeGaugeInt64,
650 },
651 },
652 },
653 expected: []metricdata.Metrics{
654 {
655 Name: "foo.com/gauge-a",
656 Description: "a testing gauge",
657 Unit: "1",
658 Data: metricdata.Gauge[int64]{
659 DataPoints: []metricdata.DataPoint[int64]{},
660 },
661 },
662 },
663 },
664 {
665 desc: "histogram with negative count",
666 input: []*ocmetricdata.Metric{
667 {
668 Descriptor: ocmetricdata.Descriptor{
669 Name: "foo.com/histogram-a",
670 Description: "a testing histogram",
671 Unit: ocmetricdata.UnitDimensionless,
672 Type: ocmetricdata.TypeCumulativeDistribution,
673 },
674 TimeSeries: []*ocmetricdata.TimeSeries{
675 {
676 Points: []ocmetricdata.Point{
677 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{
678 Count: -8,
679 }),
680 },
681 StartTime: startTime,
682 },
683 },
684 },
685 },
686 expectedErr: errNegativeCount,
687 },
688 {
689 desc: "histogram with negative bucket count",
690 input: []*ocmetricdata.Metric{
691 {
692 Descriptor: ocmetricdata.Descriptor{
693 Name: "foo.com/histogram-a",
694 Description: "a testing histogram",
695 Unit: ocmetricdata.UnitDimensionless,
696 Type: ocmetricdata.TypeCumulativeDistribution,
697 },
698 TimeSeries: []*ocmetricdata.TimeSeries{
699 {
700 Points: []ocmetricdata.Point{
701 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{
702 Buckets: []ocmetricdata.Bucket{
703 {Count: -1},
704 {Count: 2},
705 {Count: 5},
706 },
707 }),
708 },
709 StartTime: startTime,
710 },
711 },
712 },
713 },
714 expectedErr: errNegativeBucketCount,
715 },
716 {
717 desc: "histogram with non-histogram datapoint type",
718 input: []*ocmetricdata.Metric{
719 {
720 Descriptor: ocmetricdata.Descriptor{
721 Name: "foo.com/bad-point",
722 Description: "a bad type",
723 Unit: ocmetricdata.UnitDimensionless,
724 Type: ocmetricdata.TypeCumulativeDistribution,
725 },
726 TimeSeries: []*ocmetricdata.TimeSeries{
727 {
728 Points: []ocmetricdata.Point{
729 ocmetricdata.NewFloat64Point(endTime1, 1.0),
730 },
731 StartTime: startTime,
732 },
733 },
734 },
735 },
736 expectedErr: errMismatchedValueTypes,
737 },
738 {
739 desc: "summary with mismatched attributes",
740 input: []*ocmetricdata.Metric{
741 {
742 Descriptor: ocmetricdata.Descriptor{
743 Name: "foo.com/summary-mismatched",
744 Description: "a mismatched summary",
745 Unit: ocmetricdata.UnitMilliseconds,
746 Type: ocmetricdata.TypeSummary,
747 LabelKeys: []ocmetricdata.LabelKey{
748 {Key: "g"},
749 },
750 },
751 TimeSeries: []*ocmetricdata.TimeSeries{
752 {
753 LabelValues: []ocmetricdata.LabelValue{
754 {
755 Value: "ding",
756 Present: true,
757 }, {
758 Value: "dong",
759 Present: true,
760 },
761 },
762 Points: []ocmetricdata.Point{
763 ocmetricdata.NewSummaryPoint(endTime1, &ocmetricdata.Summary{
764 Count: 10,
765 Sum: 13.2,
766 HasCountAndSum: true,
767 Snapshot: ocmetricdata.Snapshot{
768 Percentiles: map[float64]float64{
769 0.0: 0.1,
770 0.5: 1.0,
771 1.0: 10.4,
772 },
773 },
774 }),
775 },
776 },
777 },
778 },
779 },
780 expectedErr: errMismatchedAttributeKeyValues,
781 },
782 {
783 desc: "summary with negative count",
784 input: []*ocmetricdata.Metric{
785 {
786 Descriptor: ocmetricdata.Descriptor{
787 Name: "foo.com/summary-negative",
788 Description: "a negative count summary",
789 Unit: ocmetricdata.UnitMilliseconds,
790 Type: ocmetricdata.TypeSummary,
791 },
792 TimeSeries: []*ocmetricdata.TimeSeries{
793 {
794 Points: []ocmetricdata.Point{
795 ocmetricdata.NewSummaryPoint(endTime1, &ocmetricdata.Summary{
796 Count: -10,
797 Sum: 13.2,
798 HasCountAndSum: true,
799 Snapshot: ocmetricdata.Snapshot{
800 Percentiles: map[float64]float64{
801 0.0: 0.1,
802 0.5: 1.0,
803 1.0: 10.4,
804 },
805 },
806 }),
807 },
808 },
809 },
810 },
811 },
812 expectedErr: errNegativeCount,
813 },
814 {
815 desc: "histogram with invalid span context exemplar",
816 input: []*ocmetricdata.Metric{
817 {
818 Descriptor: ocmetricdata.Descriptor{
819 Name: "foo.com/histogram-a",
820 Description: "a testing histogram",
821 Unit: ocmetricdata.UnitDimensionless,
822 Type: ocmetricdata.TypeCumulativeDistribution,
823 },
824 TimeSeries: []*ocmetricdata.TimeSeries{
825 {
826 Points: []ocmetricdata.Point{
827 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{
828 Count: 8,
829 Sum: 100.0,
830 BucketOptions: &ocmetricdata.BucketOptions{
831 Bounds: []float64{1.0, 2.0, 3.0},
832 },
833 Buckets: []ocmetricdata.Bucket{
834 {
835 Count: 1,
836 Exemplar: &ocmetricdata.Exemplar{
837 Value: 0.8,
838 Timestamp: exemplarTime,
839 Attachments: map[string]any{
840 ocmetricdata.AttachmentKeySpanContext: "notaspancontext",
841 },
842 },
843 },
844 },
845 }),
846 },
847 StartTime: startTime,
848 },
849 },
850 },
851 },
852 expectedErr: errInvalidExemplarSpanContext,
853 },
854 {
855 desc: "sum with non-sum datapoint type",
856 input: []*ocmetricdata.Metric{
857 {
858 Descriptor: ocmetricdata.Descriptor{
859 Name: "foo.com/bad-point",
860 Description: "a bad type",
861 Unit: ocmetricdata.UnitDimensionless,
862 Type: ocmetricdata.TypeCumulativeFloat64,
863 },
864 TimeSeries: []*ocmetricdata.TimeSeries{
865 {
866 Points: []ocmetricdata.Point{
867 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{}),
868 },
869 StartTime: startTime,
870 },
871 },
872 },
873 },
874 expectedErr: errMismatchedValueTypes,
875 },
876 {
877 desc: "gauge with non-gauge datapoint type",
878 input: []*ocmetricdata.Metric{
879 {
880 Descriptor: ocmetricdata.Descriptor{
881 Name: "foo.com/bad-point",
882 Description: "a bad type",
883 Unit: ocmetricdata.UnitDimensionless,
884 Type: ocmetricdata.TypeGaugeFloat64,
885 },
886 TimeSeries: []*ocmetricdata.TimeSeries{
887 {
888 Points: []ocmetricdata.Point{
889 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{}),
890 },
891 StartTime: startTime,
892 },
893 },
894 },
895 },
896 expectedErr: errMismatchedValueTypes,
897 },
898 {
899 desc: "summary with non-summary datapoint type",
900 input: []*ocmetricdata.Metric{
901 {
902 Descriptor: ocmetricdata.Descriptor{
903 Name: "foo.com/bad-point",
904 Description: "a bad type",
905 Unit: ocmetricdata.UnitDimensionless,
906 Type: ocmetricdata.TypeSummary,
907 },
908 TimeSeries: []*ocmetricdata.TimeSeries{
909 {
910 Points: []ocmetricdata.Point{
911 ocmetricdata.NewDistributionPoint(endTime1, &ocmetricdata.Distribution{}),
912 },
913 StartTime: startTime,
914 },
915 },
916 },
917 },
918 expectedErr: errMismatchedValueTypes,
919 },
920 {
921 desc: "unsupported Gauge Distribution type",
922 input: []*ocmetricdata.Metric{
923 {
924 Descriptor: ocmetricdata.Descriptor{
925 Name: "foo.com/bad-point",
926 Description: "a bad type",
927 Unit: ocmetricdata.UnitDimensionless,
928 Type: ocmetricdata.TypeGaugeDistribution,
929 },
930 },
931 },
932 expectedErr: errAggregationType,
933 },
934 } {
935 t.Run(tc.desc, func(t *testing.T) {
936 output, err := ConvertMetrics(tc.input)
937 if !errors.Is(err, tc.expectedErr) {
938 t.Errorf("ConvertMetrics(%+v) = err(%v), want err(%v)", tc.input, err, tc.expectedErr)
939 }
940 metricdatatest.AssertEqual[metricdata.ScopeMetrics](t,
941 metricdata.ScopeMetrics{Metrics: tc.expected},
942 metricdata.ScopeMetrics{Metrics: output})
943 })
944 }
945 }
946
947 func TestConvertAttributes(t *testing.T) {
948 setWithMultipleKeys := attribute.NewSet(
949 attribute.KeyValue{Key: attribute.Key("first"), Value: attribute.StringValue("1")},
950 attribute.KeyValue{Key: attribute.Key("second"), Value: attribute.StringValue("2")},
951 )
952 for _, tc := range []struct {
953 desc string
954 inputKeys []ocmetricdata.LabelKey
955 inputValues []ocmetricdata.LabelValue
956 expected *attribute.Set
957 expectedErr error
958 }{
959 {
960 desc: "no attributes",
961 expected: attribute.EmptySet(),
962 },
963 {
964 desc: "different numbers of keys and values",
965 inputKeys: []ocmetricdata.LabelKey{{Key: "foo"}},
966 expected: attribute.EmptySet(),
967 expectedErr: errMismatchedAttributeKeyValues,
968 },
969 {
970 desc: "multiple keys and values",
971 inputKeys: []ocmetricdata.LabelKey{{Key: "first"}, {Key: "second"}},
972 inputValues: []ocmetricdata.LabelValue{
973 {Value: "1", Present: true},
974 {Value: "2", Present: true},
975 },
976 expected: &setWithMultipleKeys,
977 },
978 {
979 desc: "multiple keys and values with some not present",
980 inputKeys: []ocmetricdata.LabelKey{{Key: "first"}, {Key: "second"}, {Key: "third"}},
981 inputValues: []ocmetricdata.LabelValue{
982 {Value: "1", Present: true},
983 {Value: "2", Present: true},
984 {Present: false},
985 },
986 expected: &setWithMultipleKeys,
987 },
988 } {
989 t.Run(tc.desc, func(t *testing.T) {
990 output, err := convertAttrs(tc.inputKeys, tc.inputValues)
991 if !errors.Is(err, tc.expectedErr) {
992 t.Errorf(
993 "convertAttrs(keys: %v, values: %v) = err(%v), want err(%v)",
994 tc.inputKeys,
995 tc.inputValues,
996 err,
997 tc.expectedErr,
998 )
999 }
1000 if !output.Equals(tc.expected) {
1001 t.Errorf(
1002 "convertAttrs(keys: %v, values: %v) = %+v, want %+v",
1003 tc.inputKeys,
1004 tc.inputValues,
1005 output.ToSlice(),
1006 tc.expected.ToSlice(),
1007 )
1008 }
1009 })
1010 }
1011 }
1012
1013 type fakeStringer string
1014
1015 func (f fakeStringer) String() string {
1016 return string(f)
1017 }
1018
1019 func TestConvertKV(t *testing.T) {
1020 key := "foo"
1021 for _, tt := range []struct {
1022 value any
1023 expected attribute.Value
1024 }{
1025 {
1026 value: bool(true),
1027 expected: attribute.BoolValue(true),
1028 },
1029 {
1030 value: []bool{true, false},
1031 expected: attribute.BoolSliceValue([]bool{true, false}),
1032 },
1033 {
1034 value: int(10),
1035 expected: attribute.IntValue(10),
1036 },
1037 {
1038 value: []int{10, 20},
1039 expected: attribute.IntSliceValue([]int{10, 20}),
1040 },
1041 {
1042 value: int8(10),
1043 expected: attribute.IntValue(10),
1044 },
1045 {
1046 value: []int8{10, 20},
1047 expected: attribute.IntSliceValue([]int{10, 20}),
1048 },
1049 {
1050 value: int16(10),
1051 expected: attribute.IntValue(10),
1052 },
1053 {
1054 value: []int16{10, 20},
1055 expected: attribute.IntSliceValue([]int{10, 20}),
1056 },
1057 {
1058 value: int32(10),
1059 expected: attribute.IntValue(10),
1060 },
1061 {
1062 value: []int32{10, 20},
1063 expected: attribute.IntSliceValue([]int{10, 20}),
1064 },
1065 {
1066 value: int64(10),
1067 expected: attribute.Int64Value(10),
1068 },
1069 {
1070 value: []int64{10, 20},
1071 expected: attribute.Int64SliceValue([]int64{10, 20}),
1072 },
1073 {
1074 value: uint(10),
1075 expected: attribute.IntValue(10),
1076 },
1077 {
1078 value: uint(math.MaxUint),
1079 expected: attribute.StringValue(fmt.Sprintf("%v", uint(math.MaxUint))),
1080 },
1081 {
1082 value: []uint{10, 20},
1083 expected: attribute.StringSliceValue([]string{"10", "20"}),
1084 },
1085 {
1086 value: uint8(10),
1087 expected: attribute.IntValue(10),
1088 },
1089 {
1090 value: []uint8{10, 20},
1091 expected: attribute.StringSliceValue([]string{"10", "20"}),
1092 },
1093 {
1094 value: uint16(10),
1095 expected: attribute.IntValue(10),
1096 },
1097 {
1098 value: []uint16{10, 20},
1099 expected: attribute.StringSliceValue([]string{"10", "20"}),
1100 },
1101 {
1102 value: uint32(10),
1103 expected: attribute.IntValue(10),
1104 },
1105 {
1106 value: []uint32{10, 20},
1107 expected: attribute.StringSliceValue([]string{"10", "20"}),
1108 },
1109 {
1110 value: uint64(10),
1111 expected: attribute.Int64Value(10),
1112 },
1113 {
1114 value: uint64(math.MaxUint64),
1115 expected: attribute.StringValue("18446744073709551615"),
1116 },
1117 {
1118 value: []uint64{10, 20},
1119 expected: attribute.StringSliceValue([]string{"10", "20"}),
1120 },
1121 {
1122 value: uintptr(10),
1123 expected: attribute.Int64Value(10),
1124 },
1125 {
1126 value: []uintptr{10, 20},
1127 expected: attribute.StringSliceValue([]string{"10", "20"}),
1128 },
1129 {
1130 value: float32(10),
1131 expected: attribute.Float64Value(10),
1132 },
1133 {
1134 value: []float32{10, 20},
1135 expected: attribute.Float64SliceValue([]float64{10, 20}),
1136 },
1137 {
1138 value: float64(10),
1139 expected: attribute.Float64Value(10),
1140 },
1141 {
1142 value: []float64{10, 20},
1143 expected: attribute.Float64SliceValue([]float64{10, 20}),
1144 },
1145 {
1146 value: complex64(10),
1147 expected: attribute.StringValue("(10+0i)"),
1148 },
1149 {
1150 value: []complex64{10, 20},
1151 expected: attribute.StringSliceValue([]string{"(10+0i)", "(20+0i)"}),
1152 },
1153 {
1154 value: complex128(10),
1155 expected: attribute.StringValue("(10+0i)"),
1156 },
1157 {
1158 value: []complex128{10, 20},
1159 expected: attribute.StringSliceValue([]string{"(10+0i)", "(20+0i)"}),
1160 },
1161 {
1162 value: "string",
1163 expected: attribute.StringValue("string"),
1164 },
1165 {
1166 value: []string{"string", "slice"},
1167 expected: attribute.StringSliceValue([]string{"string", "slice"}),
1168 },
1169 {
1170 value: fakeStringer("stringer"),
1171 expected: attribute.StringValue("stringer"),
1172 },
1173 {
1174 value: metricdata.Histogram[float64]{},
1175 expected: attribute.StringValue("unhandled attribute value: {DataPoints:[] Temporality:undefinedTemporality}"),
1176 },
1177 } {
1178 t.Run(fmt.Sprintf("%v(%+v)", reflect.TypeOf(tt.value), tt.value), func(t *testing.T) {
1179 got := convertKV(key, tt.value)
1180 assert.Equal(t, key, string(got.Key))
1181 assert.Equal(t, tt.expected, got.Value)
1182 })
1183 }
1184 }
1185
1186 func BenchmarkConvertExemplar(b *testing.B) {
1187 const attchmentsN = 10
1188 data := make([]*ocmetricdata.Exemplar, b.N)
1189 for i := range data {
1190 a := make(ocmetricdata.Attachments, attchmentsN)
1191 for j := range attchmentsN {
1192 a[strconv.Itoa(j)] = rand.Int64()
1193 }
1194 data[i] = &ocmetricdata.Exemplar{
1195 Value: rand.NormFloat64(),
1196 Timestamp: time.Now(),
1197 Attachments: a,
1198 }
1199 }
1200
1201 var out metricdata.Exemplar[float64]
1202
1203 b.ReportAllocs()
1204 b.ResetTimer()
1205 for n := 0; n < b.N; n++ {
1206 out, _ = convertExemplar(data[n])
1207 }
1208
1209 _ = out
1210 }
1211
1212 func BenchmarkConvertQuantiles(b *testing.B) {
1213 const percentileN = 20
1214 data := make([]ocmetricdata.Snapshot, b.N)
1215 for i := range data {
1216 p := make(map[float64]float64, percentileN)
1217 for range percentileN {
1218 v := rand.Float64()
1219 for v == 0 {
1220 // Convert from [0, 1) interval to (0, 1).
1221 v = rand.Float64()
1222 }
1223 v *= 100 // Convert from (0, 1) interval to (0, 100).
1224 p[v] = rand.ExpFloat64()
1225 }
1226 data[i] = ocmetricdata.Snapshot{Percentiles: p}
1227 }
1228
1229 var out []metricdata.QuantileValue
1230
1231 b.ReportAllocs()
1232 b.ResetTimer()
1233 for n := 0; n < b.N; n++ {
1234 out = convertQuantiles(data[n])
1235 }
1236
1237 _ = out
1238 }