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
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
import (
"bytes"
"fmt"
"reflect"
"slices"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)
// equalResourceMetrics returns reasons ResourceMetrics are not equal. If they
// are equal, the returned reasons will be empty.
//
// The ScopeMetrics each ResourceMetrics contains are compared based on
// containing the same ScopeMetrics, not the order they are stored in.
func equalResourceMetrics(a, b metricdata.ResourceMetrics, cfg config) (reasons []string) {
if !a.Resource.Equal(b.Resource) {
reasons = append(reasons, notEqualStr("Resources", a.Resource, b.Resource))
}
r := compareDiff(diffSlices(
a.ScopeMetrics,
b.ScopeMetrics,
func(a, b metricdata.ScopeMetrics) bool {
r := equalScopeMetrics(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("ResourceMetrics ScopeMetrics not equal:\n%s", r))
}
return reasons
}
// equalScopeMetrics returns reasons ScopeMetrics are not equal. If they are
// equal, the returned reasons will be empty.
//
// The Metrics each ScopeMetrics contains are compared based on containing the
// same Metrics, not the order they are stored in.
func equalScopeMetrics(a, b metricdata.ScopeMetrics, cfg config) (reasons []string) {
if a.Scope != b.Scope {
reasons = append(reasons, notEqualStr("Scope", a.Scope, b.Scope))
}
r := compareDiff(diffSlices(
a.Metrics,
b.Metrics,
func(a, b metricdata.Metrics) bool {
r := equalMetrics(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("ScopeMetrics Metrics not equal:\n%s", r))
}
return reasons
}
// equalMetrics returns reasons Metrics are not equal. If they are equal, the
// returned reasons will be empty.
func equalMetrics(a, b metricdata.Metrics, cfg config) (reasons []string) {
if a.Name != b.Name {
reasons = append(reasons, notEqualStr("Name", a.Name, b.Name))
}
if a.Description != b.Description {
reasons = append(reasons, notEqualStr("Description", a.Description, b.Description))
}
if a.Unit != b.Unit {
reasons = append(reasons, notEqualStr("Unit", a.Unit, b.Unit))
}
r := equalAggregations(a.Data, b.Data, cfg)
if len(r) > 0 {
reasons = append(reasons, "Metrics Data not equal:")
reasons = append(reasons, r...)
}
return reasons
}
// equalAggregations returns reasons a and b are not equal. If they are equal,
// the returned reasons will be empty.
func equalAggregations(a, b metricdata.Aggregation, cfg config) (reasons []string) {
if a == nil || b == nil {
if a != b {
return []string{notEqualStr("Aggregation", a, b)}
}
return reasons
}
if reflect.TypeOf(a) != reflect.TypeOf(b) {
return []string{fmt.Sprintf("Aggregation types not equal:\nexpected: %T\nactual: %T", a, b)}
}
switch v := a.(type) {
case metricdata.Gauge[int64]:
r := equalGauges(v, b.(metricdata.Gauge[int64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "Gauge[int64] not equal:")
reasons = append(reasons, r...)
}
case metricdata.Gauge[float64]:
r := equalGauges(v, b.(metricdata.Gauge[float64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "Gauge[float64] not equal:")
reasons = append(reasons, r...)
}
case metricdata.Sum[int64]:
r := equalSums(v, b.(metricdata.Sum[int64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "Sum[int64] not equal:")
reasons = append(reasons, r...)
}
case metricdata.Sum[float64]:
r := equalSums(v, b.(metricdata.Sum[float64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "Sum[float64] not equal:")
reasons = append(reasons, r...)
}
case metricdata.Histogram[int64]:
r := equalHistograms(v, b.(metricdata.Histogram[int64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "Histogram not equal:")
reasons = append(reasons, r...)
}
case metricdata.Histogram[float64]:
r := equalHistograms(v, b.(metricdata.Histogram[float64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "Histogram not equal:")
reasons = append(reasons, r...)
}
case metricdata.ExponentialHistogram[int64]:
r := equalExponentialHistograms(v, b.(metricdata.ExponentialHistogram[int64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "ExponentialHistogram not equal:")
reasons = append(reasons, r...)
}
case metricdata.ExponentialHistogram[float64]:
r := equalExponentialHistograms(v, b.(metricdata.ExponentialHistogram[float64]), cfg)
if len(r) > 0 {
reasons = append(reasons, "ExponentialHistogram not equal:")
reasons = append(reasons, r...)
}
case metricdata.Summary:
r := equalSummary(v, b.(metricdata.Summary), cfg)
if len(r) > 0 {
reasons = append(reasons, "Summary not equal:")
reasons = append(reasons, r...)
}
default:
reasons = append(reasons, fmt.Sprintf("Aggregation of unknown types %T", a))
}
return reasons
}
// equalGauges returns reasons Gauges are not equal. If they are equal, the
// returned reasons will be empty.
//
// The DataPoints each Gauge contains are compared based on containing the
// same DataPoints, not the order they are stored in.
func equalGauges[N int64 | float64](a, b metricdata.Gauge[N], cfg config) (reasons []string) {
r := compareDiff(diffSlices(
a.DataPoints,
b.DataPoints,
func(a, b metricdata.DataPoint[N]) bool {
r := equalDataPoints(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Gauge DataPoints not equal:\n%s", r))
}
return reasons
}
// equalSums returns reasons Sums are not equal. If they are equal, the
// returned reasons will be empty.
//
// The DataPoints each Sum contains are compared based on containing the same
// DataPoints, not the order they are stored in.
func equalSums[N int64 | float64](a, b metricdata.Sum[N], cfg config) (reasons []string) {
if a.Temporality != b.Temporality {
reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality))
}
if a.IsMonotonic != b.IsMonotonic {
reasons = append(reasons, notEqualStr("IsMonotonic", a.IsMonotonic, b.IsMonotonic))
}
r := compareDiff(diffSlices(
a.DataPoints,
b.DataPoints,
func(a, b metricdata.DataPoint[N]) bool {
r := equalDataPoints(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Sum DataPoints not equal:\n%s", r))
}
return reasons
}
// equalHistograms returns reasons Histograms are not equal. If they are
// equal, the returned reasons will be empty.
//
// The DataPoints each Histogram contains are compared based on containing the
// same HistogramDataPoint, not the order they are stored in.
func equalHistograms[N int64 | float64](a, b metricdata.Histogram[N], cfg config) (reasons []string) {
if a.Temporality != b.Temporality {
reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality))
}
r := compareDiff(diffSlices(
a.DataPoints,
b.DataPoints,
func(a, b metricdata.HistogramDataPoint[N]) bool {
r := equalHistogramDataPoints(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Histogram DataPoints not equal:\n%s", r))
}
return reasons
}
// equalDataPoints returns reasons DataPoints are not equal. If they are
// equal, the returned reasons will be empty.
func equalDataPoints[N int64 | float64](a, b metricdata.DataPoint[N], cfg config) (reasons []string) { // nolint: revive // Intentional internal control flag
if !a.Attributes.Equals(&b.Attributes) {
reasons = append(reasons, notEqualStr(
"Attributes",
a.Attributes.Encoded(attribute.DefaultEncoder()),
b.Attributes.Encoded(attribute.DefaultEncoder()),
))
}
if !cfg.ignoreTimestamp {
if !a.StartTime.Equal(b.StartTime) {
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano()))
}
if !a.Time.Equal(b.Time) {
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano()))
}
}
if !cfg.ignoreValue {
if a.Value != b.Value {
reasons = append(reasons, notEqualStr("Value", a.Value, b.Value))
}
}
if !cfg.ignoreExemplars {
r := compareDiff(diffSlices(
a.Exemplars,
b.Exemplars,
func(a, b metricdata.Exemplar[N]) bool {
r := equalExemplars(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Exemplars not equal:\n%s", r))
}
}
return reasons
}
// equalHistogramDataPoints returns reasons HistogramDataPoints are not equal.
// If they are equal, the returned reasons will be empty.
func equalHistogramDataPoints[N int64 | float64](a, b metricdata.HistogramDataPoint[N], cfg config) (reasons []string) { // nolint: revive // Intentional internal control flag
if !a.Attributes.Equals(&b.Attributes) {
reasons = append(reasons, notEqualStr(
"Attributes",
a.Attributes.Encoded(attribute.DefaultEncoder()),
b.Attributes.Encoded(attribute.DefaultEncoder()),
))
}
if !cfg.ignoreTimestamp {
if !a.StartTime.Equal(b.StartTime) {
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano()))
}
if !a.Time.Equal(b.Time) {
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano()))
}
}
if !cfg.ignoreValue {
if a.Count != b.Count {
reasons = append(reasons, notEqualStr("Count", a.Count, b.Count))
}
if !slices.Equal(a.Bounds, b.Bounds) {
reasons = append(reasons, notEqualStr("Bounds", a.Bounds, b.Bounds))
}
if !slices.Equal(a.BucketCounts, b.BucketCounts) {
reasons = append(reasons, notEqualStr("BucketCounts", a.BucketCounts, b.BucketCounts))
}
if !eqExtrema(a.Min, b.Min) {
reasons = append(reasons, notEqualStr("Min", a.Min, b.Min))
}
if !eqExtrema(a.Max, b.Max) {
reasons = append(reasons, notEqualStr("Max", a.Max, b.Max))
}
if a.Sum != b.Sum {
reasons = append(reasons, notEqualStr("Sum", a.Sum, b.Sum))
}
}
if !cfg.ignoreExemplars {
r := compareDiff(diffSlices(
a.Exemplars,
b.Exemplars,
func(a, b metricdata.Exemplar[N]) bool {
r := equalExemplars(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Exemplars not equal:\n%s", r))
}
}
return reasons
}
// equalExponentialHistograms returns reasons exponential Histograms are not equal. If they are
// equal, the returned reasons will be empty.
//
// The DataPoints each Histogram contains are compared based on containing the
// same HistogramDataPoint, not the order they are stored in.
func equalExponentialHistograms[N int64 | float64](a, b metricdata.ExponentialHistogram[N], cfg config) (reasons []string) {
if a.Temporality != b.Temporality {
reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality))
}
r := compareDiff(diffSlices(
a.DataPoints,
b.DataPoints,
func(a, b metricdata.ExponentialHistogramDataPoint[N]) bool {
r := equalExponentialHistogramDataPoints(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Histogram DataPoints not equal:\n%s", r))
}
return reasons
}
// equalExponentialHistogramDataPoints returns reasons HistogramDataPoints are not equal.
// If they are equal, the returned reasons will be empty.
func equalExponentialHistogramDataPoints[N int64 | float64](a, b metricdata.ExponentialHistogramDataPoint[N], cfg config) (reasons []string) { // nolint: revive // Intentional internal control flag
if !a.Attributes.Equals(&b.Attributes) {
reasons = append(reasons, notEqualStr(
"Attributes",
a.Attributes.Encoded(attribute.DefaultEncoder()),
b.Attributes.Encoded(attribute.DefaultEncoder()),
))
}
if !cfg.ignoreTimestamp {
if !a.StartTime.Equal(b.StartTime) {
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano()))
}
if !a.Time.Equal(b.Time) {
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano()))
}
}
if !cfg.ignoreValue {
if a.Count != b.Count {
reasons = append(reasons, notEqualStr("Count", a.Count, b.Count))
}
if !eqExtrema(a.Min, b.Min) {
reasons = append(reasons, notEqualStr("Min", a.Min, b.Min))
}
if !eqExtrema(a.Max, b.Max) {
reasons = append(reasons, notEqualStr("Max", a.Max, b.Max))
}
if a.Sum != b.Sum {
reasons = append(reasons, notEqualStr("Sum", a.Sum, b.Sum))
}
if a.Scale != b.Scale {
reasons = append(reasons, notEqualStr("Scale", a.Scale, b.Scale))
}
if a.ZeroCount != b.ZeroCount {
reasons = append(reasons, notEqualStr("ZeroCount", a.ZeroCount, b.ZeroCount))
}
r := equalExponentialBuckets(a.PositiveBucket, b.PositiveBucket, cfg)
if len(r) > 0 {
reasons = append(reasons, r...)
}
r = equalExponentialBuckets(a.NegativeBucket, b.NegativeBucket, cfg)
if len(r) > 0 {
reasons = append(reasons, r...)
}
}
if !cfg.ignoreExemplars {
r := compareDiff(diffSlices(
a.Exemplars,
b.Exemplars,
func(a, b metricdata.Exemplar[N]) bool {
r := equalExemplars(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Exemplars not equal:\n%s", r))
}
}
return reasons
}
func equalExponentialBuckets(a, b metricdata.ExponentialBucket, _ config) (reasons []string) {
if a.Offset != b.Offset {
reasons = append(reasons, notEqualStr("Offset", a.Offset, b.Offset))
}
if !slices.Equal(a.Counts, b.Counts) {
reasons = append(reasons, notEqualStr("Counts", a.Counts, b.Counts))
}
return reasons
}
func equalSummary(a, b metricdata.Summary, cfg config) (reasons []string) {
r := compareDiff(diffSlices(
a.DataPoints,
b.DataPoints,
func(a, b metricdata.SummaryDataPoint) bool {
r := equalSummaryDataPoint(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, fmt.Sprintf("Summary DataPoints not equal:\n%s", r))
}
return reasons
}
func equalSummaryDataPoint(a, b metricdata.SummaryDataPoint, cfg config) (reasons []string) {
if !a.Attributes.Equals(&b.Attributes) {
reasons = append(reasons, notEqualStr(
"Attributes",
a.Attributes.Encoded(attribute.DefaultEncoder()),
b.Attributes.Encoded(attribute.DefaultEncoder()),
))
}
if !cfg.ignoreTimestamp {
if !a.StartTime.Equal(b.StartTime) {
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano()))
}
if !a.Time.Equal(b.Time) {
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano()))
}
}
if !cfg.ignoreValue {
if a.Count != b.Count {
reasons = append(reasons, notEqualStr("Count", a.Count, b.Count))
}
if a.Sum != b.Sum {
reasons = append(reasons, notEqualStr("Sum", a.Sum, b.Sum))
}
r := compareDiff(diffSlices(
a.QuantileValues,
b.QuantileValues,
func(a, b metricdata.QuantileValue) bool {
r := equalQuantileValue(a, b, cfg)
return len(r) == 0
},
))
if r != "" {
reasons = append(reasons, r)
}
}
return reasons
}
func equalQuantileValue(a, b metricdata.QuantileValue, _ config) (reasons []string) {
if a.Quantile != b.Quantile {
reasons = append(reasons, notEqualStr("Quantile", a.Quantile, b.Quantile))
}
if a.Value != b.Value {
reasons = append(reasons, notEqualStr("Value", a.Value, b.Value))
}
return reasons
}
func notEqualStr(prefix string, expected, actual interface{}) string {
return fmt.Sprintf("%s not equal:\nexpected: %v\nactual: %v", prefix, expected, actual)
}
func equalExtrema[N int64 | float64](a, b metricdata.Extrema[N], _ config) (reasons []string) {
if !eqExtrema(a, b) {
reasons = append(reasons, notEqualStr("Extrema", a, b))
}
return reasons
}
func eqExtrema[N int64 | float64](a, b metricdata.Extrema[N]) bool {
aV, aOk := a.Value()
bV, bOk := b.Value()
if !aOk || !bOk {
return aOk == bOk
}
return aV == bV
}
func equalKeyValue(a, b attribute.KeyValue) bool {
if a.Key != b.Key {
return false
}
if a.Value.Type() != b.Value.Type() {
return false
}
switch a.Value.Type() {
case attribute.BOOL:
if a.Value.AsBool() != b.Value.AsBool() {
return false
}
case attribute.INT64:
if a.Value.AsInt64() != b.Value.AsInt64() {
return false
}
case attribute.FLOAT64:
if a.Value.AsFloat64() != b.Value.AsFloat64() {
return false
}
case attribute.STRING:
if a.Value.AsString() != b.Value.AsString() {
return false
}
case attribute.BOOLSLICE:
if ok := slices.Equal(a.Value.AsBoolSlice(), b.Value.AsBoolSlice()); !ok {
return false
}
case attribute.INT64SLICE:
if ok := slices.Equal(a.Value.AsInt64Slice(), b.Value.AsInt64Slice()); !ok {
return false
}
case attribute.FLOAT64SLICE:
if ok := slices.Equal(a.Value.AsFloat64Slice(), b.Value.AsFloat64Slice()); !ok {
return false
}
case attribute.STRINGSLICE:
if ok := slices.Equal(a.Value.AsStringSlice(), b.Value.AsStringSlice()); !ok {
return false
}
default:
// We control all types passed to this, panic to signal developers
// early they changed things in an incompatible way.
panic(fmt.Sprintf("unknown attribute value type: %s", a.Value.Type()))
}
return true
}
func equalExemplars[N int64 | float64](a, b metricdata.Exemplar[N], cfg config) (reasons []string) {
if !slices.EqualFunc(a.FilteredAttributes, b.FilteredAttributes, equalKeyValue) {
reasons = append(reasons, notEqualStr("FilteredAttributes", a.FilteredAttributes, b.FilteredAttributes))
}
if !cfg.ignoreTimestamp {
if !a.Time.Equal(b.Time) {
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano()))
}
}
if !cfg.ignoreValue {
if a.Value != b.Value {
reasons = append(reasons, notEqualStr("Value", a.Value, b.Value))
}
}
if !slices.Equal(a.SpanID, b.SpanID) {
reasons = append(reasons, notEqualStr("SpanID", a.SpanID, b.SpanID))
}
if !slices.Equal(a.TraceID, b.TraceID) {
reasons = append(reasons, notEqualStr("TraceID", a.TraceID, b.TraceID))
}
return reasons
}
func diffSlices[T any](a, b []T, equal func(T, T) bool) (extraA, extraB []T) {
visited := make([]bool, len(b))
for i := 0; i < len(a); i++ {
found := false
for j := 0; j < len(b); j++ {
if visited[j] {
continue
}
if equal(a[i], b[j]) {
visited[j] = true
found = true
break
}
}
if !found {
extraA = append(extraA, a[i])
}
}
for j := 0; j < len(b); j++ {
if visited[j] {
continue
}
extraB = append(extraB, b[j])
}
return extraA, extraB
}
func compareDiff[T any](extraExpected, extraActual []T) string {
if len(extraExpected) == 0 && len(extraActual) == 0 {
return ""
}
formatter := func(v T) string {
return fmt.Sprintf("%#v", v)
}
var msg bytes.Buffer
if len(extraExpected) > 0 {
_, _ = msg.WriteString("missing expected values:\n")
for _, v := range extraExpected {
_, _ = msg.WriteString(formatter(v) + "\n")
}
}
if len(extraActual) > 0 {
_, _ = msg.WriteString("unexpected additional values:\n")
for _, v := range extraActual {
_, _ = msg.WriteString(formatter(v) + "\n")
}
}
return msg.String()
}
func missingAttrStr(name string) string {
return fmt.Sprintf("missing attribute %s", name)
}
func hasAttributesExemplars[T int64 | float64](exemplar metricdata.Exemplar[T], attrs ...attribute.KeyValue) (reasons []string) {
s := attribute.NewSet(exemplar.FilteredAttributes...)
for _, attr := range attrs {
val, ok := s.Value(attr.Key)
if !ok {
reasons = append(reasons, missingAttrStr(string(attr.Key)))
continue
}
if val != attr.Value {
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit()))
}
}
return reasons
}
func hasAttributesDataPoints[T int64 | float64](dp metricdata.DataPoint[T], attrs ...attribute.KeyValue) (reasons []string) {
for _, attr := range attrs {
val, ok := dp.Attributes.Value(attr.Key)
if !ok {
reasons = append(reasons, missingAttrStr(string(attr.Key)))
continue
}
if val != attr.Value {
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit()))
}
}
return reasons
}
func hasAttributesGauge[T int64 | float64](gauge metricdata.Gauge[T], attrs ...attribute.KeyValue) (reasons []string) {
for n, dp := range gauge.DataPoints {
reas := hasAttributesDataPoints(dp, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("gauge datapoint %d attributes:\n", n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesSum[T int64 | float64](sum metricdata.Sum[T], attrs ...attribute.KeyValue) (reasons []string) {
for n, dp := range sum.DataPoints {
reas := hasAttributesDataPoints(dp, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("sum datapoint %d attributes:\n", n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesHistogramDataPoints[T int64 | float64](dp metricdata.HistogramDataPoint[T], attrs ...attribute.KeyValue) (reasons []string) {
for _, attr := range attrs {
val, ok := dp.Attributes.Value(attr.Key)
if !ok {
reasons = append(reasons, missingAttrStr(string(attr.Key)))
continue
}
if val != attr.Value {
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit()))
}
}
return reasons
}
func hasAttributesHistogram[T int64 | float64](histogram metricdata.Histogram[T], attrs ...attribute.KeyValue) (reasons []string) {
for n, dp := range histogram.DataPoints {
reas := hasAttributesHistogramDataPoints(dp, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("histogram datapoint %d attributes:\n", n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesExponentialHistogramDataPoints[T int64 | float64](dp metricdata.ExponentialHistogramDataPoint[T], attrs ...attribute.KeyValue) (reasons []string) {
for _, attr := range attrs {
val, ok := dp.Attributes.Value(attr.Key)
if !ok {
reasons = append(reasons, missingAttrStr(string(attr.Key)))
continue
}
if val != attr.Value {
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit()))
}
}
return reasons
}
func hasAttributesExponentialHistogram[T int64 | float64](histogram metricdata.ExponentialHistogram[T], attrs ...attribute.KeyValue) (reasons []string) {
for n, dp := range histogram.DataPoints {
reas := hasAttributesExponentialHistogramDataPoints(dp, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("histogram datapoint %d attributes:\n", n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesAggregation(agg metricdata.Aggregation, attrs ...attribute.KeyValue) (reasons []string) {
switch agg := agg.(type) {
case metricdata.Gauge[int64]:
reasons = hasAttributesGauge(agg, attrs...)
case metricdata.Gauge[float64]:
reasons = hasAttributesGauge(agg, attrs...)
case metricdata.Sum[int64]:
reasons = hasAttributesSum(agg, attrs...)
case metricdata.Sum[float64]:
reasons = hasAttributesSum(agg, attrs...)
case metricdata.Histogram[int64]:
reasons = hasAttributesHistogram(agg, attrs...)
case metricdata.Histogram[float64]:
reasons = hasAttributesHistogram(agg, attrs...)
case metricdata.ExponentialHistogram[int64]:
reasons = hasAttributesExponentialHistogram(agg, attrs...)
case metricdata.ExponentialHistogram[float64]:
reasons = hasAttributesExponentialHistogram(agg, attrs...)
case metricdata.Summary:
reasons = hasAttributesSummary(agg, attrs...)
default:
reasons = []string{fmt.Sprintf("unknown aggregation %T", agg)}
}
return reasons
}
func hasAttributesMetrics(metrics metricdata.Metrics, attrs ...attribute.KeyValue) (reasons []string) {
reas := hasAttributesAggregation(metrics.Data, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("Metric %s:\n", metrics.Name))
reasons = append(reasons, reas...)
}
return reasons
}
func hasAttributesScopeMetrics(sm metricdata.ScopeMetrics, attrs ...attribute.KeyValue) (reasons []string) {
for n, metrics := range sm.Metrics {
reas := hasAttributesMetrics(metrics, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("ScopeMetrics %s Metrics %d:\n", sm.Scope.Name, n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesResourceMetrics(rm metricdata.ResourceMetrics, attrs ...attribute.KeyValue) (reasons []string) {
for n, sm := range rm.ScopeMetrics {
reas := hasAttributesScopeMetrics(sm, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("ResourceMetrics ScopeMetrics %d:\n", n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesSummary(summary metricdata.Summary, attrs ...attribute.KeyValue) (reasons []string) {
for n, dp := range summary.DataPoints {
reas := hasAttributesSummaryDataPoint(dp, attrs...)
if len(reas) > 0 {
reasons = append(reasons, fmt.Sprintf("summary datapoint %d attributes:\n", n))
reasons = append(reasons, reas...)
}
}
return reasons
}
func hasAttributesSummaryDataPoint(dp metricdata.SummaryDataPoint, attrs ...attribute.KeyValue) (reasons []string) {
for _, attr := range attrs {
val, ok := dp.Attributes.Value(attr.Key)
if !ok {
reasons = append(reasons, missingAttrStr(string(attr.Key)))
continue
}
if val != attr.Value {
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit()))
}
}
return reasons
}
|