-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmetrics.go
850 lines (764 loc) · 29.8 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pmetrictest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
import (
"errors"
"fmt"
"reflect"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal"
)
func CompareMetrics(expected, actual pmetric.Metrics, options ...CompareMetricsOption) error {
exp, act := pmetric.NewMetrics(), pmetric.NewMetrics()
expected.CopyTo(exp)
actual.CopyTo(act)
for _, option := range options {
option.applyOnMetrics(exp, act)
}
expectedMetrics, actualMetrics := exp.ResourceMetrics(), act.ResourceMetrics()
if expectedMetrics.Len() != actualMetrics.Len() {
return fmt.Errorf("number of resources doesn't match expected: %d, actual: %d", expectedMetrics.Len(),
actualMetrics.Len())
}
numResources := expectedMetrics.Len()
// Keep track of matching resources so that each can only be matched once
matchingResources := make(map[pmetric.ResourceMetrics]pmetric.ResourceMetrics, numResources)
var errs error
var outOfOrderErrs error
for e := 0; e < numResources; e++ {
er := expectedMetrics.At(e)
var foundMatch bool
for a := 0; a < numResources; a++ {
ar := actualMetrics.At(a)
if _, ok := matchingResources[ar]; ok {
continue
}
if reflect.DeepEqual(er.Resource().Attributes().AsRaw(), ar.Resource().Attributes().AsRaw()) {
foundMatch = true
matchingResources[ar] = er
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`resources are out of order: resource "%v" expected at index %d, found at index %d`,
er.Resource().Attributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected resource: %v", er.Resource().Attributes().AsRaw()))
}
}
for i := 0; i < numResources; i++ {
if _, ok := matchingResources[actualMetrics.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected resource: %v",
actualMetrics.At(i).Resource().Attributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for ar, er := range matchingResources {
errPrefix := fmt.Sprintf(`resource "%v"`, ar.Resource().Attributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareResourceMetrics(er, ar)))
}
return errs
}
func CompareResourceMetrics(expected, actual pmetric.ResourceMetrics) error {
errs := multierr.Combine(
internal.CompareResource(expected.Resource(), actual.Resource()),
internal.CompareSchemaURL(expected.SchemaUrl(), actual.SchemaUrl()),
)
esms := expected.ScopeMetrics()
asms := actual.ScopeMetrics()
if esms.Len() != asms.Len() {
errs = multierr.Append(errs, fmt.Errorf("number of scopes doesn't match expected: %d, actual: %d", esms.Len(),
asms.Len()))
return errs
}
numScopeMetrics := expected.ScopeMetrics().Len()
// Keep track of matching resources so that each can only be matched once
matchingResources := make(map[pmetric.ScopeMetrics]pmetric.ScopeMetrics, numScopeMetrics)
var outOfOrderErrs error
for e := 0; e < numScopeMetrics; e++ {
esm := esms.At(e)
var foundMatch bool
for a := 0; a < numScopeMetrics; a++ {
asm := asms.At(a)
if _, ok := matchingResources[asm]; ok {
continue
}
if esm.Scope().Name() == asm.Scope().Name() {
foundMatch = true
matchingResources[asm] = esm
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`scopes are out of order: scope "%s" expected at index %d, found at index %d`,
esm.Scope().Name(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected scope: %s", esm.Scope().Name()))
}
}
for i := 0; i < numScopeMetrics; i++ {
if _, ok := matchingResources[actual.ScopeMetrics().At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected scope: %s",
actual.ScopeMetrics().At(i).Scope().Name()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for i := 0; i < esms.Len(); i++ {
errPrefix := fmt.Sprintf(`scope "%s"`, esms.At(i).Scope().Name())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareScopeMetrics(esms.At(i), asms.At(i))))
}
return errs
}
// CompareScopeMetrics compares each part of two given ScopeMetrics and returns
// an error if they don't match. The error describes what didn't match. The
// expected and actual values are clones before options are applied.
func CompareScopeMetrics(expected, actual pmetric.ScopeMetrics) error {
errs := multierr.Combine(
internal.CompareInstrumentationScope(expected.Scope(), actual.Scope()),
internal.CompareSchemaURL(expected.SchemaUrl(), actual.SchemaUrl()),
)
ems := expected.Metrics()
ams := actual.Metrics()
if ems.Len() != ams.Len() {
errs = multierr.Append(errs, fmt.Errorf("number of metrics doesn't match expected: %d, actual: %d",
ems.Len(), ams.Len()))
return errs
}
numMetrics := ems.Len()
// Keep track of matching records so that each record can only be matched once
matchingMetrics := make(map[pmetric.Metric]pmetric.Metric, numMetrics)
var outOfOrderErrs error
for e := 0; e < numMetrics; e++ {
em := ems.At(e)
var foundMatch bool
for a := 0; a < numMetrics; a++ {
am := ams.At(a)
if _, ok := matchingMetrics[am]; ok {
continue
}
if em.Name() == am.Name() {
foundMatch = true
matchingMetrics[am] = em
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`metrics are out of order: metric "%s" expected at index %d, found at index %d`,
em.Name(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected metric: %s", em.Name()))
}
}
for i := 0; i < numMetrics; i++ {
if _, ok := matchingMetrics[ams.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected metric: %s", ams.At(i).Name()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for i := 0; i < numMetrics; i++ {
errPrefix := fmt.Sprintf(`metric "%s"`, ems.At(i).Name())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareMetric(ems.At(i), ams.At(i))))
}
return errs
}
func CompareMetric(expected pmetric.Metric, actual pmetric.Metric) error {
var errs error
if actual.Name() != expected.Name() {
errs = multierr.Append(errs, fmt.Errorf("name doesn't match expected: %s, actual: %s", expected.Name(),
actual.Name()))
}
if actual.Description() != expected.Description() {
errs = multierr.Append(errs, fmt.Errorf("description doesn't match expected: %s, actual: %s",
expected.Description(), actual.Description()))
}
if actual.Unit() != expected.Unit() {
errs = multierr.Append(errs, fmt.Errorf("unit doesn't match expected: %s, actual: %s", expected.Unit(),
actual.Unit()))
}
if actual.Type() != expected.Type() {
errs = multierr.Append(errs, fmt.Errorf("type doesn't match expected: %s, actual: %s", expected.Type(),
actual.Type()))
return errs
}
//exhaustive:enforce
switch actual.Type() {
case pmetric.MetricTypeGauge:
errs = multierr.Append(errs, compareNumberDataPointSlices(expected.Gauge().DataPoints(),
actual.Gauge().DataPoints()))
case pmetric.MetricTypeSum:
if actual.Sum().AggregationTemporality() != expected.Sum().AggregationTemporality() {
errs = multierr.Append(errs, fmt.Errorf("aggregation temporality doesn't match expected: %s, actual: %s",
expected.Sum().AggregationTemporality(), actual.Sum().AggregationTemporality()))
}
if actual.Sum().IsMonotonic() != expected.Sum().IsMonotonic() {
errs = multierr.Append(errs, fmt.Errorf("is monotonic doesn't match expected: %t, actual: %t",
expected.Sum().IsMonotonic(), actual.Sum().IsMonotonic()))
}
errs = multierr.Append(errs, compareNumberDataPointSlices(expected.Sum().DataPoints(), actual.Sum().DataPoints()))
case pmetric.MetricTypeHistogram:
if actual.Histogram().AggregationTemporality() != expected.Histogram().AggregationTemporality() {
errs = multierr.Append(errs, fmt.Errorf("aggregation temporality doesn't match expected: %s, actual: %s",
expected.Histogram().AggregationTemporality(), actual.Histogram().AggregationTemporality()))
}
errs = multierr.Append(errs, compareHistogramDataPointSlices(expected.Histogram().DataPoints(),
actual.Histogram().DataPoints()))
case pmetric.MetricTypeExponentialHistogram:
if actual.ExponentialHistogram().AggregationTemporality() != expected.ExponentialHistogram().AggregationTemporality() {
errs = multierr.Append(errs, fmt.Errorf("aggregation temporality doesn't match expected: %s, actual: %s",
expected.ExponentialHistogram().AggregationTemporality(),
actual.ExponentialHistogram().AggregationTemporality()))
}
errs = multierr.Append(errs, compareExponentialHistogramDataPointSlice(expected.ExponentialHistogram().DataPoints(),
actual.ExponentialHistogram().DataPoints()))
case pmetric.MetricTypeSummary:
errs = multierr.Append(errs, compareSummaryDataPointSlices(expected.Summary().DataPoints(),
actual.Summary().DataPoints()))
case pmetric.MetricTypeEmpty:
}
return errs
}
// compareNumberDataPointSlices compares each part of two given NumberDataPointSlices and returns
// an error if they don't match. The error describes what didn't match.
func compareNumberDataPointSlices(expected, actual pmetric.NumberDataPointSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of datapoints doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())
}
numPoints := expected.Len()
// Keep track of matching data points so that each point can only be matched once
matchingDPS := make(map[pmetric.NumberDataPoint]pmetric.NumberDataPoint, numPoints)
var errs error
var outOfOrderErrs error
for e := 0; e < numPoints; e++ {
edp := expected.At(e)
var foundMatch bool
for a := 0; a < numPoints; a++ {
adp := actual.At(a)
if _, ok := matchingDPS[adp]; ok {
continue
}
if reflect.DeepEqual(edp.Attributes().AsRaw(), adp.Attributes().AsRaw()) {
foundMatch = true
matchingDPS[adp] = edp
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf("datapoints are out of order: "+
`datapoint "%v" expected at index %d, found at index %d`, edp.Attributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected datapoint: %v", edp.Attributes().AsRaw()))
}
}
for i := 0; i < numPoints; i++ {
if _, ok := matchingDPS[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected datapoint: %v", actual.At(i).Attributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for adp, edp := range matchingDPS {
errPrefix := fmt.Sprintf(`datapoint "%v"`, adp.Attributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareNumberDataPoint(edp, adp)))
}
return errs
}
// CompareNumberDataPoint compares each part of two given NumberDataPoints and returns
// an error if they don't match. The error describes what didn't match.
func CompareNumberDataPoint(expected, actual pmetric.NumberDataPoint) error {
errs := internal.CompareAttributes(expected.Attributes(), actual.Attributes())
if expected.StartTimestamp() != actual.StartTimestamp() {
errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, "+
"actual: %d", expected.StartTimestamp(), actual.StartTimestamp()))
}
if expected.Timestamp() != actual.Timestamp() {
errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d",
expected.Timestamp(), actual.Timestamp()))
}
if expected.ValueType() != actual.ValueType() {
errs = multierr.Append(errs, fmt.Errorf("value type doesn't match expected: %s, actual: %s",
expected.ValueType(), actual.ValueType()))
} else {
if expected.IntValue() != actual.IntValue() {
errs = multierr.Append(errs, fmt.Errorf("int value doesn't match expected: %d, actual: %d",
expected.IntValue(), actual.IntValue()))
}
if expected.DoubleValue() != actual.DoubleValue() {
errs = multierr.Append(errs, fmt.Errorf("double value doesn't match expected: %f, actual: %f",
expected.DoubleValue(), actual.DoubleValue()))
}
}
if expected.Flags() != actual.Flags() {
errs = multierr.Append(errs, fmt.Errorf("flags don't match expected: %d, actual: %d",
expected.Flags(), actual.Flags()))
}
errs = multierr.Append(errs, compareExemplarSlice(expected.Exemplars(), actual.Exemplars()))
return errs
}
// compareExemplarSlice compares each part of two given ExemplarSlice and returns
// an error if they don't match. The error describes what didn't match.
func compareExemplarSlice(expected, actual pmetric.ExemplarSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of exemplars doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())
}
numExemplars := expected.Len()
// Keep track of matching exemplars so that each exemplar can only be matched once
matchingExs := make(map[pmetric.Exemplar]pmetric.Exemplar, numExemplars)
var errs error
var outOfOrderErrs error
for e := 0; e < numExemplars; e++ {
eex := expected.At(e)
var foundMatch bool
for a := 0; a < numExemplars; a++ {
aex := actual.At(a)
if _, ok := matchingExs[aex]; ok {
continue
}
if reflect.DeepEqual(eex.FilteredAttributes().AsRaw(), aex.FilteredAttributes().AsRaw()) {
foundMatch = true
matchingExs[aex] = eex
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf("exemplars are out of order: "+
"exemplar %v expected at index %d, found at index %d", eex.FilteredAttributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected exemplar: %v", eex.FilteredAttributes().AsRaw()))
}
}
for i := 0; i < numExemplars; i++ {
if _, ok := matchingExs[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected exemplar: %v",
actual.At(i).FilteredAttributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for aex, eex := range matchingExs {
errPrefix := fmt.Sprintf(`exemplar %v`, eex.FilteredAttributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareExemplar(eex, aex)))
}
return errs
}
// CompareExemplar compares each part of two given pmetric.Exemplar and returns
// an error if they don't match. The error describes what didn't match.
func CompareExemplar(expected, actual pmetric.Exemplar) error {
var errs error
if expected.ValueType() != actual.ValueType() {
errs = multierr.Append(errs, fmt.Errorf("value type doesn't match: expected type: %s, actual type: %s",
expected.ValueType(), actual.ValueType()))
} else {
if expected.DoubleValue() != actual.DoubleValue() {
errs = multierr.Append(errs, fmt.Errorf("double value doesn't match expected: %f, actual: %f",
expected.DoubleValue(), actual.DoubleValue()))
}
if expected.IntValue() != actual.IntValue() {
errs = multierr.Append(errs, fmt.Errorf("int value doesn't match expected: %d, actual: %d",
expected.IntValue(), actual.IntValue()))
}
}
if expected.Timestamp() != actual.Timestamp() {
errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d",
expected.Timestamp(), actual.Timestamp()))
}
if expected.TraceID() != actual.TraceID() {
errs = multierr.Append(errs, fmt.Errorf("trace ID doesn't match expected: %s, actual: %s",
expected.TraceID(), actual.TraceID()))
}
if expected.SpanID() != actual.SpanID() {
errs = multierr.Append(errs, fmt.Errorf("span ID doesn't match expected: %s, actual: %s",
expected.SpanID(), actual.SpanID()))
}
return errs
}
// compareHistogramDataPointSlices compares each part of two given HistogramDataPointSlices and returns
// an error if they don't match. The error describes what didn't match.
func compareHistogramDataPointSlices(expected, actual pmetric.HistogramDataPointSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of datapoints doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())
}
numPoints := expected.Len()
// Keep track of matching data points so that each point can only be matched once
matchingDPS := make(map[pmetric.HistogramDataPoint]pmetric.HistogramDataPoint, numPoints)
var errs error
var outOfOrderErrs error
for e := 0; e < numPoints; e++ {
edp := expected.At(e)
var foundMatch bool
for a := 0; a < numPoints; a++ {
adp := actual.At(a)
if _, ok := matchingDPS[adp]; ok {
continue
}
if reflect.DeepEqual(edp.Attributes().AsRaw(), adp.Attributes().AsRaw()) {
foundMatch = true
matchingDPS[adp] = edp
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`datapoint "%v" expected at index %d, found at index %d`, edp.Attributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected datapoint: %v", edp.Attributes().AsRaw()))
}
}
for i := 0; i < numPoints; i++ {
if _, ok := matchingDPS[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected datapoint: %v", actual.At(i).Attributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for adp, edp := range matchingDPS {
errPrefix := fmt.Sprintf(`datapoint "%v"`, adp.Attributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareHistogramDataPoints(edp, adp)))
}
return errs
}
// CompareHistogramDataPoints compares each part of two given HistogramDataPoints and returns
// an error if they don't match. The error describes what didn't match.
func CompareHistogramDataPoints(expected, actual pmetric.HistogramDataPoint) error {
errs := internal.CompareAttributes(expected.Attributes(), actual.Attributes())
if expected.HasSum() != actual.HasSum() || expected.Sum() != actual.Sum() {
errStr := "sum doesn't match expected: "
if expected.HasSum() {
errStr += fmt.Sprintf("%f", expected.Sum())
} else {
errStr += "<unset>"
}
errStr += ", actual: "
if actual.HasSum() {
errStr += fmt.Sprintf("%f", actual.Sum())
} else {
errStr += "<unset>"
}
errs = multierr.Append(errs, errors.New(errStr))
}
if expected.HasMin() != actual.HasMin() || expected.Min() != actual.Min() {
errStr := "min doesn't match expected: "
if expected.HasMin() {
errStr += fmt.Sprintf("%f", expected.Min())
} else {
errStr += "<unset>"
}
errStr += ", actual: "
if actual.HasMin() {
errStr += fmt.Sprintf("%f", actual.Min())
} else {
errStr += "<unset>"
}
errs = multierr.Append(errs, errors.New(errStr))
}
if expected.HasMax() != actual.HasMax() || expected.Max() != actual.Max() {
errStr := "max doesn't match expected: "
if expected.HasMax() {
errStr += fmt.Sprintf("%f", expected.Min())
} else {
errStr += "<unset>"
}
errStr += ", actual: "
if actual.HasMax() {
errStr += fmt.Sprintf("%f", actual.Min())
} else {
errStr += "<unset>"
}
errs = multierr.Append(errs, errors.New(errStr))
}
if expected.Count() != actual.Count() {
errs = multierr.Append(errs, fmt.Errorf("count doesn't match expected: %d, actual: %d",
expected.Count(), actual.Count()))
}
if expected.StartTimestamp() != actual.StartTimestamp() {
errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, actual: %d",
expected.StartTimestamp(), actual.StartTimestamp()))
}
if expected.Timestamp() != actual.Timestamp() {
errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d",
expected.Timestamp(), actual.Timestamp()))
}
if expected.Flags() != actual.Flags() {
errs = multierr.Append(errs, fmt.Errorf("flags don't match expected: %d, actual: %d",
expected.Flags(), actual.Flags()))
}
if !reflect.DeepEqual(expected.BucketCounts(), actual.BucketCounts()) {
errs = multierr.Append(errs, fmt.Errorf("bucket counts don't match expected: %v, actual: %v",
expected.BucketCounts().AsRaw(), actual.BucketCounts().AsRaw()))
}
if !reflect.DeepEqual(expected.ExplicitBounds(), actual.ExplicitBounds()) {
errs = multierr.Append(errs, fmt.Errorf("explicit bounds don't match expected: %v, "+
"actual: %v", expected.ExplicitBounds().AsRaw(), actual.ExplicitBounds().AsRaw()))
}
errs = multierr.Append(errs, compareExemplarSlice(expected.Exemplars(), actual.Exemplars()))
return errs
}
// compareExponentialHistogramDataPointSlice compares each part of two given ExponentialHistogramDataPointSlices and
// returns an error if they don't match. The error describes what didn't match.
func compareExponentialHistogramDataPointSlice(expected, actual pmetric.ExponentialHistogramDataPointSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of datapoints doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())
}
numPoints := expected.Len()
// Keep track of matching data points so that each point can only be matched once
matchingDPS := make(map[pmetric.ExponentialHistogramDataPoint]pmetric.ExponentialHistogramDataPoint, numPoints)
var errs error
var outOfOrderErrs error
for e := 0; e < numPoints; e++ {
edp := expected.At(e)
var foundMatch bool
for a := 0; a < numPoints; a++ {
adp := actual.At(a)
if _, ok := matchingDPS[adp]; ok {
continue
}
if reflect.DeepEqual(edp.Attributes().AsRaw(), adp.Attributes().AsRaw()) {
foundMatch = true
matchingDPS[adp] = edp
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`datapoint "%v" expected at index %d, found at index %d`, edp.Attributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected datapoint: %v", edp.Attributes().AsRaw()))
}
}
for i := 0; i < numPoints; i++ {
if _, ok := matchingDPS[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected datapoint: %v", actual.At(i).Attributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for adp, edp := range matchingDPS {
errPrefix := fmt.Sprintf(`datapoint "%v"`, adp.Attributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareExponentialHistogramDataPoint(edp, adp)))
}
return errs
}
// CompareExponentialHistogramDataPoint compares each part of two given ExponentialHistogramDataPoints and returns
// an error if they don't match. The error describes what didn't match.
func CompareExponentialHistogramDataPoint(expected, actual pmetric.ExponentialHistogramDataPoint) error {
errs := internal.CompareAttributes(expected.Attributes(), actual.Attributes())
if expected.HasSum() != actual.HasSum() || expected.Sum() != actual.Sum() {
errStr := "sum doesn't match expected: "
if expected.HasSum() {
errStr += fmt.Sprintf("%f", expected.Sum())
} else {
errStr += "<unset>"
}
errStr += ", actual: "
if actual.HasSum() {
errStr += fmt.Sprintf("%f", actual.Sum())
} else {
errStr += "<unset>"
}
errs = multierr.Append(errs, errors.New(errStr))
}
if expected.HasMin() != actual.HasMin() || expected.Min() != actual.Min() {
errStr := "min doesn't match expected: "
if expected.HasMin() {
errStr += fmt.Sprintf("%f", expected.Min())
} else {
errStr += "<unset>"
}
errStr += ", actual: "
if actual.HasMin() {
errStr += fmt.Sprintf("%f", actual.Min())
} else {
errStr += "<unset>"
}
errs = multierr.Append(errs, errors.New(errStr))
}
if expected.HasMax() != actual.HasMax() || expected.Max() != actual.Max() {
errStr := "max doesn't match expected: "
if expected.HasMax() {
errStr += fmt.Sprintf("%f", expected.Min())
} else {
errStr += "<unset>"
}
errStr += ", actual: "
if actual.HasMax() {
errStr += fmt.Sprintf("%f", actual.Min())
} else {
errStr += "<unset>"
}
errs = multierr.Append(errs, errors.New(errStr))
}
if expected.Count() != actual.Count() {
errs = multierr.Append(errs, fmt.Errorf("count doesn't match expected: %d, actual: %d",
expected.Count(), actual.Count()))
}
if expected.ZeroCount() != actual.ZeroCount() {
errs = multierr.Append(errs, fmt.Errorf("zero count doesn't match expected: %d, actual: %d",
expected.ZeroCount(), actual.ZeroCount()))
}
if expected.StartTimestamp() != actual.StartTimestamp() {
errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, "+
"actual: %d", expected.StartTimestamp(), actual.StartTimestamp()))
}
if expected.Timestamp() != actual.Timestamp() {
errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d",
expected.Timestamp(), actual.Timestamp()))
}
if expected.Flags() != actual.Flags() {
errs = multierr.Append(errs, fmt.Errorf("flags don't match expected: %d, actual: %d",
expected.Flags(), actual.Flags()))
}
if expected.Scale() != actual.Scale() {
errs = multierr.Append(errs, fmt.Errorf("scale doesn't match expected: %v, actual: %v",
expected.Scale(), actual.Scale()))
}
if expected.Negative().Offset() != actual.Negative().Offset() {
errs = multierr.Append(errs, fmt.Errorf("negative offset doesn't match expected: %v, "+
"actual: %v", expected.Negative().Offset(), actual.Negative().Offset()))
}
if !reflect.DeepEqual(expected.Negative().BucketCounts(), actual.Negative().BucketCounts()) {
errs = multierr.Append(errs, fmt.Errorf("negative bucket counts don't match expected: %v, "+
"actual: %v", expected.Negative().BucketCounts().AsRaw(), actual.Negative().BucketCounts().AsRaw()))
}
if expected.Positive().Offset() != actual.Positive().Offset() {
errs = multierr.Append(errs, fmt.Errorf("positive offset doesn't match expected: %v, "+
"actual: %v", expected.Positive().Offset(), actual.Positive().Offset()))
}
if !reflect.DeepEqual(expected.Positive().BucketCounts(), actual.Positive().BucketCounts()) {
errs = multierr.Append(errs, fmt.Errorf("positive bucket counts don't match expected: %v, "+
"actual: %v", expected.Positive().BucketCounts().AsRaw(), actual.Positive().BucketCounts().AsRaw()))
}
errs = multierr.Append(errs, compareExemplarSlice(expected.Exemplars(), actual.Exemplars()))
return errs
}
// compareSummaryDataPointSlices compares each part of two given SummaryDataPoint slices and returns
// an error if they don't match. The error describes what didn't match.
func compareSummaryDataPointSlices(expected, actual pmetric.SummaryDataPointSlice) error {
numPoints := expected.Len()
if numPoints != actual.Len() {
return fmt.Errorf("metric datapoint slice length doesn't match expected: %d, actual: %d", numPoints, actual.Len())
}
matchingDPS := map[pmetric.SummaryDataPoint]pmetric.SummaryDataPoint{}
var errs error
var outOfOrderErrs error
for e := 0; e < numPoints; e++ {
edp := expected.At(e)
var foundMatch bool
for a := 0; a < numPoints; a++ {
adp := actual.At(a)
if _, ok := matchingDPS[adp]; ok {
continue
}
if reflect.DeepEqual(edp.Attributes().AsRaw(), adp.Attributes().AsRaw()) {
foundMatch = true
matchingDPS[adp] = edp
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`datapoint "%v" expected at index %d, found at index %d`, edp.Attributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected datapoint: %v", edp.Attributes().AsRaw()))
}
}
for i := 0; i < numPoints; i++ {
if _, ok := matchingDPS[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected datapoint: %v", actual.At(i).Attributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for adp, edp := range matchingDPS {
errPrefix := fmt.Sprintf(`datapoint "%v"`, adp.Attributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareSummaryDataPoint(edp, adp)))
}
return errs
}
// CompareSummaryDataPoint compares each part of two given SummaryDataPoint and returns
// an error if they don't match. The error describes what didn't match.
func CompareSummaryDataPoint(expected, actual pmetric.SummaryDataPoint) error {
errs := internal.CompareAttributes(expected.Attributes(), actual.Attributes())
if expected.Count() != actual.Count() {
errs = multierr.Append(errs, fmt.Errorf("count doesn't match expected: %d, actual: %d",
expected.Count(), actual.Count()))
}
if expected.Sum() != actual.Sum() {
errs = multierr.Append(errs, fmt.Errorf("sum doesn't match expected: %f, actual: %f",
expected.Sum(), actual.Sum()))
}
if expected.StartTimestamp() != actual.StartTimestamp() {
errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, "+
"actual: %d", expected.StartTimestamp(), actual.StartTimestamp()))
}
if expected.Timestamp() != actual.Timestamp() {
errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d",
expected.Timestamp(), actual.Timestamp()))
}
if expected.Flags() != actual.Flags() {
errs = multierr.Append(errs, fmt.Errorf("flags don't match expected: %d, actual: %d",
expected.Flags(), actual.Flags()))
}
if expected.QuantileValues().Len() != actual.QuantileValues().Len() {
errs = multierr.Append(errs, fmt.Errorf("quantile values length doesn't match expected: %d, "+
"actual: %d", expected.QuantileValues().Len(), actual.QuantileValues().Len()))
return errs
}
for i := 0; i < expected.QuantileValues().Len(); i++ {
eqv, acv := expected.QuantileValues().At(i), actual.QuantileValues().At(i)
if eqv.Quantile() != acv.Quantile() {
errs = multierr.Append(errs, fmt.Errorf("quantile doesn't match expected: %f, "+
"actual: %f", eqv.Quantile(), acv.Quantile()))
}
if eqv.Value() != acv.Value() {
errs = multierr.Append(errs, fmt.Errorf("value at quantile %f doesn't match expected: %f, actual: %f",
eqv.Quantile(), eqv.Value(), acv.Value()))
}
}
return errs
}