| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"slices" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 15 |
) |
| 16 |
|
| 17 |
// equalResourceMetrics returns reasons ResourceMetrics are not equal. If they |
| 18 |
// are equal, the returned reasons will be empty. |
| 19 |
// |
| 20 |
// The ScopeMetrics each ResourceMetrics contains are compared based on |
| 21 |
// containing the same ScopeMetrics, not the order they are stored in. |
| 22 |
func equalResourceMetrics(a, b metricdata.ResourceMetrics, cfg config) (reasons []string) { |
| 23 |
if !a.Resource.Equal(b.Resource) { |
| 24 |
reasons = append(reasons, notEqualStr("Resources", a.Resource, b.Resource)) |
| 25 |
} |
| 26 |
|
| 27 |
r := diffSlices( |
| 28 |
a.ScopeMetrics, |
| 29 |
b.ScopeMetrics, |
| 30 |
func(sm metricdata.ScopeMetrics) string { |
| 31 |
return fmt.Sprintf("Scope %q", sm.Scope.Name) |
| 32 |
}, |
| 33 |
func(a, b metricdata.ScopeMetrics) []string { |
| 34 |
return equalScopeMetrics(a, b, cfg) |
| 35 |
}, |
| 36 |
) |
| 37 |
if r != "" { |
| 38 |
reasons = append(reasons, "ResourceMetrics ScopeMetrics not equal:\n"+r) |
| 39 |
} |
| 40 |
return reasons |
| 41 |
} |
| 42 |
|
| 43 |
// equalScopeMetrics returns reasons ScopeMetrics are not equal. If they are |
| 44 |
// equal, the returned reasons will be empty. |
| 45 |
// |
| 46 |
// The Metrics each ScopeMetrics contains are compared based on containing the |
| 47 |
// same Metrics, not the order they are stored in. |
| 48 |
func equalScopeMetrics(a, b metricdata.ScopeMetrics, cfg config) (reasons []string) { |
| 49 |
if a.Scope != b.Scope { |
| 50 |
reasons = append(reasons, notEqualStr("Scope", a.Scope, b.Scope)) |
| 51 |
} |
| 52 |
|
| 53 |
r := diffSlices( |
| 54 |
a.Metrics, |
| 55 |
b.Metrics, |
| 56 |
func(m metricdata.Metrics) string { |
| 57 |
return fmt.Sprintf("Metric %q", m.Name) |
| 58 |
}, |
| 59 |
func(a, b metricdata.Metrics) []string { |
| 60 |
return equalMetrics(a, b, cfg) |
| 61 |
}, |
| 62 |
) |
| 63 |
if r != "" { |
| 64 |
reasons = append(reasons, "ScopeMetrics Metrics not equal:\n"+r) |
| 65 |
} |
| 66 |
return reasons |
| 67 |
} |
| 68 |
|
| 69 |
// equalMetrics returns reasons Metrics are not equal. If they are equal, the |
| 70 |
// returned reasons will be empty. |
| 71 |
func equalMetrics(a, b metricdata.Metrics, cfg config) (reasons []string) { |
| 72 |
if a.Name != b.Name { |
| 73 |
reasons = append(reasons, notEqualStr("Name", a.Name, b.Name)) |
| 74 |
} |
| 75 |
if a.Description != b.Description { |
| 76 |
reasons = append(reasons, notEqualStr("Description", a.Description, b.Description)) |
| 77 |
} |
| 78 |
if a.Unit != b.Unit { |
| 79 |
reasons = append(reasons, notEqualStr("Unit", a.Unit, b.Unit)) |
| 80 |
} |
| 81 |
|
| 82 |
r := equalAggregations(a.Data, b.Data, cfg) |
| 83 |
if len(r) > 0 { |
| 84 |
reasons = append(reasons, "Metrics Data not equal:") |
| 85 |
reasons = append(reasons, r...) |
| 86 |
} |
| 87 |
return reasons |
| 88 |
} |
| 89 |
|
| 90 |
// equalAggregations returns reasons a and b are not equal. If they are equal, |
| 91 |
// the returned reasons will be empty. |
| 92 |
func equalAggregations(a, b metricdata.Aggregation, cfg config) (reasons []string) { |
| 93 |
if a == nil || b == nil { |
| 94 |
if a != b { |
| 95 |
return []string{notEqualStr("Aggregation", a, b)} |
| 96 |
} |
| 97 |
return reasons |
| 98 |
} |
| 99 |
|
| 100 |
if reflect.TypeOf(a) != reflect.TypeOf(b) { |
| 101 |
return []string{fmt.Sprintf("Aggregation types not equal:\nexpected: %T\nactual: %T", a, b)} |
| 102 |
} |
| 103 |
|
| 104 |
switch v := a.(type) { |
| 105 |
case metricdata.Gauge[int64]: |
| 106 |
r := equalGauges(v, b.(metricdata.Gauge[int64]), cfg) |
| 107 |
if len(r) > 0 { |
| 108 |
reasons = append(reasons, "Gauge[int64] not equal:") |
| 109 |
reasons = append(reasons, r...) |
| 110 |
} |
| 111 |
case metricdata.Gauge[float64]: |
| 112 |
r := equalGauges(v, b.(metricdata.Gauge[float64]), cfg) |
| 113 |
if len(r) > 0 { |
| 114 |
reasons = append(reasons, "Gauge[float64] not equal:") |
| 115 |
reasons = append(reasons, r...) |
| 116 |
} |
| 117 |
case metricdata.Sum[int64]: |
| 118 |
r := equalSums(v, b.(metricdata.Sum[int64]), cfg) |
| 119 |
if len(r) > 0 { |
| 120 |
reasons = append(reasons, "Sum[int64] not equal:") |
| 121 |
reasons = append(reasons, r...) |
| 122 |
} |
| 123 |
case metricdata.Sum[float64]: |
| 124 |
r := equalSums(v, b.(metricdata.Sum[float64]), cfg) |
| 125 |
if len(r) > 0 { |
| 126 |
reasons = append(reasons, "Sum[float64] not equal:") |
| 127 |
reasons = append(reasons, r...) |
| 128 |
} |
| 129 |
case metricdata.Histogram[int64]: |
| 130 |
r := equalHistograms(v, b.(metricdata.Histogram[int64]), cfg) |
| 131 |
if len(r) > 0 { |
| 132 |
reasons = append(reasons, "Histogram not equal:") |
| 133 |
reasons = append(reasons, r...) |
| 134 |
} |
| 135 |
case metricdata.Histogram[float64]: |
| 136 |
r := equalHistograms(v, b.(metricdata.Histogram[float64]), cfg) |
| 137 |
if len(r) > 0 { |
| 138 |
reasons = append(reasons, "Histogram not equal:") |
| 139 |
reasons = append(reasons, r...) |
| 140 |
} |
| 141 |
case metricdata.ExponentialHistogram[int64]: |
| 142 |
r := equalExponentialHistograms(v, b.(metricdata.ExponentialHistogram[int64]), cfg) |
| 143 |
if len(r) > 0 { |
| 144 |
reasons = append(reasons, "ExponentialHistogram not equal:") |
| 145 |
reasons = append(reasons, r...) |
| 146 |
} |
| 147 |
case metricdata.ExponentialHistogram[float64]: |
| 148 |
r := equalExponentialHistograms(v, b.(metricdata.ExponentialHistogram[float64]), cfg) |
| 149 |
if len(r) > 0 { |
| 150 |
reasons = append(reasons, "ExponentialHistogram not equal:") |
| 151 |
reasons = append(reasons, r...) |
| 152 |
} |
| 153 |
case metricdata.Summary: |
| 154 |
r := equalSummary(v, b.(metricdata.Summary), cfg) |
| 155 |
if len(r) > 0 { |
| 156 |
reasons = append(reasons, "Summary not equal:") |
| 157 |
reasons = append(reasons, r...) |
| 158 |
} |
| 159 |
default: |
| 160 |
reasons = append(reasons, fmt.Sprintf("Aggregation of unknown types %T", a)) |
| 161 |
} |
| 162 |
return reasons |
| 163 |
} |
| 164 |
|
| 165 |
// equalGauges returns reasons Gauges are not equal. If they are equal, the |
| 166 |
// returned reasons will be empty. |
| 167 |
// |
| 168 |
// The DataPoints each Gauge contains are compared based on containing the |
| 169 |
// same DataPoints, not the order they are stored in. |
| 170 |
func equalGauges[N int64 | float64](a, b metricdata.Gauge[N], cfg config) (reasons []string) { |
| 171 |
r := diffSlices( |
| 172 |
a.DataPoints, |
| 173 |
b.DataPoints, |
| 174 |
func(dp metricdata.DataPoint[N]) string { |
| 175 |
return fmt.Sprintf("DataPoint [%v]", dp.Attributes.Encoded(attribute.DefaultEncoder())) |
| 176 |
}, |
| 177 |
func(a, b metricdata.DataPoint[N]) []string { |
| 178 |
return equalDataPoints(a, b, cfg) |
| 179 |
}, |
| 180 |
) |
| 181 |
if r != "" { |
| 182 |
reasons = append(reasons, "Gauge DataPoints not equal:\n"+r) |
| 183 |
} |
| 184 |
return reasons |
| 185 |
} |
| 186 |
|
| 187 |
// equalSums returns reasons Sums are not equal. If they are equal, the |
| 188 |
// returned reasons will be empty. |
| 189 |
// |
| 190 |
// The DataPoints each Sum contains are compared based on containing the same |
| 191 |
// DataPoints, not the order they are stored in. |
| 192 |
func equalSums[N int64 | float64](a, b metricdata.Sum[N], cfg config) (reasons []string) { |
| 193 |
if a.Temporality != b.Temporality { |
| 194 |
reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality)) |
| 195 |
} |
| 196 |
if a.IsMonotonic != b.IsMonotonic { |
| 197 |
reasons = append(reasons, notEqualStr("IsMonotonic", a.IsMonotonic, b.IsMonotonic)) |
| 198 |
} |
| 199 |
|
| 200 |
r := diffSlices( |
| 201 |
a.DataPoints, |
| 202 |
b.DataPoints, |
| 203 |
func(dp metricdata.DataPoint[N]) string { |
| 204 |
return fmt.Sprintf("DataPoint [%v]", dp.Attributes.Encoded(attribute.DefaultEncoder())) |
| 205 |
}, |
| 206 |
func(a, b metricdata.DataPoint[N]) []string { |
| 207 |
return equalDataPoints(a, b, cfg) |
| 208 |
}, |
| 209 |
) |
| 210 |
if r != "" { |
| 211 |
reasons = append(reasons, "Sum DataPoints not equal:\n"+r) |
| 212 |
} |
| 213 |
return reasons |
| 214 |
} |
| 215 |
|
| 216 |
// equalHistograms returns reasons Histograms are not equal. If they are |
| 217 |
// equal, the returned reasons will be empty. |
| 218 |
// |
| 219 |
// The DataPoints each Histogram contains are compared based on containing the |
| 220 |
// same HistogramDataPoint, not the order they are stored in. |
| 221 |
func equalHistograms[N int64 | float64](a, b metricdata.Histogram[N], cfg config) (reasons []string) { |
| 222 |
if a.Temporality != b.Temporality { |
| 223 |
reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality)) |
| 224 |
} |
| 225 |
|
| 226 |
r := diffSlices( |
| 227 |
a.DataPoints, |
| 228 |
b.DataPoints, |
| 229 |
func(dp metricdata.HistogramDataPoint[N]) string { |
| 230 |
return fmt.Sprintf("HistogramDataPoint [%v]", dp.Attributes.Encoded(attribute.DefaultEncoder())) |
| 231 |
}, |
| 232 |
func(a, b metricdata.HistogramDataPoint[N]) []string { |
| 233 |
return equalHistogramDataPoints(a, b, cfg) |
| 234 |
}, |
| 235 |
) |
| 236 |
if r != "" { |
| 237 |
reasons = append(reasons, "Histogram DataPoints not equal:\n"+r) |
| 238 |
} |
| 239 |
return reasons |
| 240 |
} |
| 241 |
|
| 242 |
// equalDataPoints returns reasons DataPoints are not equal. If they are |
| 243 |
// equal, the returned reasons will be empty. |
| 244 |
func equalDataPoints[N int64 | float64]( |
| 245 |
a, b metricdata.DataPoint[N], |
| 246 |
cfg config, |
| 247 |
) (reasons []string) { // nolint: revive // Intentional internal control flag |
| 248 |
if !a.Attributes.Equals(&b.Attributes) { |
| 249 |
reasons = append(reasons, notEqualStr( |
| 250 |
"Attributes", |
| 251 |
a.Attributes.Encoded(attribute.DefaultEncoder()), |
| 252 |
b.Attributes.Encoded(attribute.DefaultEncoder()), |
| 253 |
)) |
| 254 |
} |
| 255 |
|
| 256 |
if !cfg.ignoreTimestamp { |
| 257 |
if !a.StartTime.Equal(b.StartTime) { |
| 258 |
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano())) |
| 259 |
} |
| 260 |
if !a.Time.Equal(b.Time) { |
| 261 |
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano())) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if !cfg.ignoreValue { |
| 266 |
if a.Value != b.Value { |
| 267 |
reasons = append(reasons, notEqualStr("Value", a.Value, b.Value)) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if !cfg.ignoreExemplars { |
| 272 |
r := diffSlices( |
| 273 |
a.Exemplars, |
| 274 |
b.Exemplars, |
| 275 |
func(_ metricdata.Exemplar[N]) string { |
| 276 |
return "Exemplar" |
| 277 |
}, |
| 278 |
func(a, b metricdata.Exemplar[N]) []string { |
| 279 |
return equalExemplars(a, b, cfg) |
| 280 |
}, |
| 281 |
) |
| 282 |
if r != "" { |
| 283 |
reasons = append(reasons, "Exemplars not equal:\n"+r) |
| 284 |
} |
| 285 |
} |
| 286 |
return reasons |
| 287 |
} |
| 288 |
|
| 289 |
// equalHistogramDataPoints returns reasons HistogramDataPoints are not equal. |
| 290 |
// If they are equal, the returned reasons will be empty. |
| 291 |
func equalHistogramDataPoints[N int64 | float64]( |
| 292 |
a, b metricdata.HistogramDataPoint[N], |
| 293 |
cfg config, |
| 294 |
) (reasons []string) { // nolint: revive // Intentional internal control flag |
| 295 |
if !a.Attributes.Equals(&b.Attributes) { |
| 296 |
reasons = append(reasons, notEqualStr( |
| 297 |
"Attributes", |
| 298 |
a.Attributes.Encoded(attribute.DefaultEncoder()), |
| 299 |
b.Attributes.Encoded(attribute.DefaultEncoder()), |
| 300 |
)) |
| 301 |
} |
| 302 |
if !cfg.ignoreTimestamp { |
| 303 |
if !a.StartTime.Equal(b.StartTime) { |
| 304 |
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano())) |
| 305 |
} |
| 306 |
if !a.Time.Equal(b.Time) { |
| 307 |
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano())) |
| 308 |
} |
| 309 |
} |
| 310 |
if !cfg.ignoreValue { |
| 311 |
if a.Count != b.Count { |
| 312 |
reasons = append(reasons, notEqualStr("Count", a.Count, b.Count)) |
| 313 |
} |
| 314 |
if !slices.Equal(a.Bounds, b.Bounds) { |
| 315 |
reasons = append(reasons, notEqualStr("Bounds", a.Bounds, b.Bounds)) |
| 316 |
} |
| 317 |
if !slices.Equal(a.BucketCounts, b.BucketCounts) { |
| 318 |
reasons = append(reasons, notEqualStr("BucketCounts", a.BucketCounts, b.BucketCounts)) |
| 319 |
} |
| 320 |
if !eqExtrema(a.Min, b.Min) { |
| 321 |
reasons = append(reasons, notEqualStr("Min", a.Min, b.Min)) |
| 322 |
} |
| 323 |
if !eqExtrema(a.Max, b.Max) { |
| 324 |
reasons = append(reasons, notEqualStr("Max", a.Max, b.Max)) |
| 325 |
} |
| 326 |
if a.Sum != b.Sum { |
| 327 |
reasons = append(reasons, notEqualStr("Sum", a.Sum, b.Sum)) |
| 328 |
} |
| 329 |
} |
| 330 |
if !cfg.ignoreExemplars { |
| 331 |
r := diffSlices( |
| 332 |
a.Exemplars, |
| 333 |
b.Exemplars, |
| 334 |
func(_ metricdata.Exemplar[N]) string { |
| 335 |
return "Exemplar" |
| 336 |
}, |
| 337 |
func(a, b metricdata.Exemplar[N]) []string { |
| 338 |
return equalExemplars(a, b, cfg) |
| 339 |
}, |
| 340 |
) |
| 341 |
if r != "" { |
| 342 |
reasons = append(reasons, "Exemplars not equal:\n"+r) |
| 343 |
} |
| 344 |
} |
| 345 |
return reasons |
| 346 |
} |
| 347 |
|
| 348 |
// equalExponentialHistograms returns reasons exponential Histograms are not equal. If they are |
| 349 |
// equal, the returned reasons will be empty. |
| 350 |
// |
| 351 |
// The DataPoints each Histogram contains are compared based on containing the |
| 352 |
// same HistogramDataPoint, not the order they are stored in. |
| 353 |
func equalExponentialHistograms[N int64 | float64]( |
| 354 |
a, b metricdata.ExponentialHistogram[N], |
| 355 |
cfg config, |
| 356 |
) (reasons []string) { |
| 357 |
if a.Temporality != b.Temporality { |
| 358 |
reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality)) |
| 359 |
} |
| 360 |
|
| 361 |
r := diffSlices( |
| 362 |
a.DataPoints, |
| 363 |
b.DataPoints, |
| 364 |
func(dp metricdata.ExponentialHistogramDataPoint[N]) string { |
| 365 |
return fmt.Sprintf("ExponentialHistogramDataPoint [%v]", dp.Attributes.Encoded(attribute.DefaultEncoder())) |
| 366 |
}, |
| 367 |
func(a, b metricdata.ExponentialHistogramDataPoint[N]) []string { |
| 368 |
return equalExponentialHistogramDataPoints(a, b, cfg) |
| 369 |
}, |
| 370 |
) |
| 371 |
if r != "" { |
| 372 |
reasons = append(reasons, "Histogram DataPoints not equal:\n"+r) |
| 373 |
} |
| 374 |
return reasons |
| 375 |
} |
| 376 |
|
| 377 |
// equalExponentialHistogramDataPoints returns reasons HistogramDataPoints are not equal. |
| 378 |
// If they are equal, the returned reasons will be empty. |
| 379 |
func equalExponentialHistogramDataPoints[N int64 | float64]( |
| 380 |
a, b metricdata.ExponentialHistogramDataPoint[N], |
| 381 |
cfg config, |
| 382 |
) (reasons []string) { // nolint: revive // Intentional internal control flag |
| 383 |
if !a.Attributes.Equals(&b.Attributes) { |
| 384 |
reasons = append(reasons, notEqualStr( |
| 385 |
"Attributes", |
| 386 |
a.Attributes.Encoded(attribute.DefaultEncoder()), |
| 387 |
b.Attributes.Encoded(attribute.DefaultEncoder()), |
| 388 |
)) |
| 389 |
} |
| 390 |
if !cfg.ignoreTimestamp { |
| 391 |
if !a.StartTime.Equal(b.StartTime) { |
| 392 |
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano())) |
| 393 |
} |
| 394 |
if !a.Time.Equal(b.Time) { |
| 395 |
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano())) |
| 396 |
} |
| 397 |
} |
| 398 |
if !cfg.ignoreValue { |
| 399 |
if a.Count != b.Count { |
| 400 |
reasons = append(reasons, notEqualStr("Count", a.Count, b.Count)) |
| 401 |
} |
| 402 |
if !eqExtrema(a.Min, b.Min) { |
| 403 |
reasons = append(reasons, notEqualStr("Min", a.Min, b.Min)) |
| 404 |
} |
| 405 |
if !eqExtrema(a.Max, b.Max) { |
| 406 |
reasons = append(reasons, notEqualStr("Max", a.Max, b.Max)) |
| 407 |
} |
| 408 |
if a.Sum != b.Sum { |
| 409 |
reasons = append(reasons, notEqualStr("Sum", a.Sum, b.Sum)) |
| 410 |
} |
| 411 |
|
| 412 |
if a.Scale != b.Scale { |
| 413 |
reasons = append(reasons, notEqualStr("Scale", a.Scale, b.Scale)) |
| 414 |
} |
| 415 |
if a.ZeroCount != b.ZeroCount { |
| 416 |
reasons = append(reasons, notEqualStr("ZeroCount", a.ZeroCount, b.ZeroCount)) |
| 417 |
} |
| 418 |
|
| 419 |
r := equalExponentialBuckets(a.PositiveBucket, b.PositiveBucket, cfg) |
| 420 |
if len(r) > 0 { |
| 421 |
reasons = append(reasons, r...) |
| 422 |
} |
| 423 |
r = equalExponentialBuckets(a.NegativeBucket, b.NegativeBucket, cfg) |
| 424 |
if len(r) > 0 { |
| 425 |
reasons = append(reasons, r...) |
| 426 |
} |
| 427 |
} |
| 428 |
if !cfg.ignoreExemplars { |
| 429 |
r := diffSlices( |
| 430 |
a.Exemplars, |
| 431 |
b.Exemplars, |
| 432 |
func(_ metricdata.Exemplar[N]) string { |
| 433 |
return "Exemplar" |
| 434 |
}, |
| 435 |
func(a, b metricdata.Exemplar[N]) []string { |
| 436 |
return equalExemplars(a, b, cfg) |
| 437 |
}, |
| 438 |
) |
| 439 |
if r != "" { |
| 440 |
reasons = append(reasons, "Exemplars not equal:\n"+r) |
| 441 |
} |
| 442 |
} |
| 443 |
return reasons |
| 444 |
} |
| 445 |
|
| 446 |
func equalExponentialBuckets(a, b metricdata.ExponentialBucket, _ config) (reasons []string) { |
| 447 |
if a.Offset != b.Offset { |
| 448 |
reasons = append(reasons, notEqualStr("Offset", a.Offset, b.Offset)) |
| 449 |
} |
| 450 |
if !slices.Equal(a.Counts, b.Counts) { |
| 451 |
reasons = append(reasons, notEqualStr("Counts", a.Counts, b.Counts)) |
| 452 |
} |
| 453 |
return reasons |
| 454 |
} |
| 455 |
|
| 456 |
func equalSummary(a, b metricdata.Summary, cfg config) (reasons []string) { |
| 457 |
r := diffSlices( |
| 458 |
a.DataPoints, |
| 459 |
b.DataPoints, |
| 460 |
func(dp metricdata.SummaryDataPoint) string { |
| 461 |
return fmt.Sprintf("SummaryDataPoint [%v]", dp.Attributes.Encoded(attribute.DefaultEncoder())) |
| 462 |
}, |
| 463 |
func(a, b metricdata.SummaryDataPoint) []string { |
| 464 |
return equalSummaryDataPoint(a, b, cfg) |
| 465 |
}, |
| 466 |
) |
| 467 |
if r != "" { |
| 468 |
reasons = append(reasons, "Summary DataPoints not equal:\n"+r) |
| 469 |
} |
| 470 |
return reasons |
| 471 |
} |
| 472 |
|
| 473 |
func equalSummaryDataPoint(a, b metricdata.SummaryDataPoint, cfg config) (reasons []string) { |
| 474 |
if !a.Attributes.Equals(&b.Attributes) { |
| 475 |
reasons = append(reasons, notEqualStr( |
| 476 |
"Attributes", |
| 477 |
a.Attributes.Encoded(attribute.DefaultEncoder()), |
| 478 |
b.Attributes.Encoded(attribute.DefaultEncoder()), |
| 479 |
)) |
| 480 |
} |
| 481 |
if !cfg.ignoreTimestamp { |
| 482 |
if !a.StartTime.Equal(b.StartTime) { |
| 483 |
reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano())) |
| 484 |
} |
| 485 |
if !a.Time.Equal(b.Time) { |
| 486 |
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano())) |
| 487 |
} |
| 488 |
} |
| 489 |
if !cfg.ignoreValue { |
| 490 |
if a.Count != b.Count { |
| 491 |
reasons = append(reasons, notEqualStr("Count", a.Count, b.Count)) |
| 492 |
} |
| 493 |
if a.Sum != b.Sum { |
| 494 |
reasons = append(reasons, notEqualStr("Sum", a.Sum, b.Sum)) |
| 495 |
} |
| 496 |
r := diffSlices( |
| 497 |
a.QuantileValues, |
| 498 |
b.QuantileValues, |
| 499 |
func(qv metricdata.QuantileValue) string { |
| 500 |
return fmt.Sprintf("QuantileValue %v", qv.Quantile) |
| 501 |
}, |
| 502 |
func(a, b metricdata.QuantileValue) []string { |
| 503 |
return equalQuantileValue(a, b, cfg) |
| 504 |
}, |
| 505 |
) |
| 506 |
if r != "" { |
| 507 |
reasons = append(reasons, r) |
| 508 |
} |
| 509 |
} |
| 510 |
return reasons |
| 511 |
} |
| 512 |
|
| 513 |
func equalQuantileValue(a, b metricdata.QuantileValue, _ config) (reasons []string) { |
| 514 |
if a.Quantile != b.Quantile { |
| 515 |
reasons = append(reasons, notEqualStr("Quantile", a.Quantile, b.Quantile)) |
| 516 |
} |
| 517 |
if a.Value != b.Value { |
| 518 |
reasons = append(reasons, notEqualStr("Value", a.Value, b.Value)) |
| 519 |
} |
| 520 |
return reasons |
| 521 |
} |
| 522 |
|
| 523 |
func notEqualStr(prefix string, expected, actual any) string { |
| 524 |
return fmt.Sprintf("%s not equal:\nexpected: %v\nactual: %v", prefix, expected, actual) |
| 525 |
} |
| 526 |
|
| 527 |
func equalExtrema[N int64 | float64](a, b metricdata.Extrema[N], _ config) (reasons []string) { |
| 528 |
if !eqExtrema(a, b) { |
| 529 |
reasons = append(reasons, notEqualStr("Extrema", a, b)) |
| 530 |
} |
| 531 |
return reasons |
| 532 |
} |
| 533 |
|
| 534 |
func eqExtrema[N int64 | float64](a, b metricdata.Extrema[N]) bool { |
| 535 |
aV, aOk := a.Value() |
| 536 |
bV, bOk := b.Value() |
| 537 |
|
| 538 |
if !aOk || !bOk { |
| 539 |
return aOk == bOk |
| 540 |
} |
| 541 |
return aV == bV |
| 542 |
} |
| 543 |
|
| 544 |
func equalKeyValue(a, b attribute.KeyValue) bool { |
| 545 |
if a.Key != b.Key { |
| 546 |
return false |
| 547 |
} |
| 548 |
if a.Value.Type() != b.Value.Type() { |
| 549 |
return false |
| 550 |
} |
| 551 |
switch a.Value.Type() { |
| 552 |
case attribute.BOOL: |
| 553 |
if a.Value.AsBool() != b.Value.AsBool() { |
| 554 |
return false |
| 555 |
} |
| 556 |
case attribute.INT64: |
| 557 |
if a.Value.AsInt64() != b.Value.AsInt64() { |
| 558 |
return false |
| 559 |
} |
| 560 |
case attribute.FLOAT64: |
| 561 |
if a.Value.AsFloat64() != b.Value.AsFloat64() { |
| 562 |
return false |
| 563 |
} |
| 564 |
case attribute.STRING: |
| 565 |
if a.Value.AsString() != b.Value.AsString() { |
| 566 |
return false |
| 567 |
} |
| 568 |
case attribute.BOOLSLICE: |
| 569 |
if ok := slices.Equal(a.Value.AsBoolSlice(), b.Value.AsBoolSlice()); !ok { |
| 570 |
return false |
| 571 |
} |
| 572 |
case attribute.INT64SLICE: |
| 573 |
if ok := slices.Equal(a.Value.AsInt64Slice(), b.Value.AsInt64Slice()); !ok { |
| 574 |
return false |
| 575 |
} |
| 576 |
case attribute.FLOAT64SLICE: |
| 577 |
if ok := slices.Equal(a.Value.AsFloat64Slice(), b.Value.AsFloat64Slice()); !ok { |
| 578 |
return false |
| 579 |
} |
| 580 |
case attribute.STRINGSLICE: |
| 581 |
if ok := slices.Equal(a.Value.AsStringSlice(), b.Value.AsStringSlice()); !ok { |
| 582 |
return false |
| 583 |
} |
| 584 |
case attribute.EMPTY: |
| 585 |
default: |
| 586 |
// We control all types passed to this, panic to signal developers |
| 587 |
// early they changed things in an incompatible way. |
| 588 |
panic(fmt.Sprintf("unknown attribute value type: %s", a.Value.Type())) |
| 589 |
} |
| 590 |
return true |
| 591 |
} |
| 592 |
|
| 593 |
func equalExemplars[N int64 | float64](a, b metricdata.Exemplar[N], cfg config) (reasons []string) { |
| 594 |
if !slices.EqualFunc(a.FilteredAttributes, b.FilteredAttributes, equalKeyValue) { |
| 595 |
reasons = append(reasons, notEqualStr("FilteredAttributes", a.FilteredAttributes, b.FilteredAttributes)) |
| 596 |
} |
| 597 |
if !cfg.ignoreTimestamp { |
| 598 |
if !a.Time.Equal(b.Time) { |
| 599 |
reasons = append(reasons, notEqualStr("Time", a.Time.UnixNano(), b.Time.UnixNano())) |
| 600 |
} |
| 601 |
} |
| 602 |
if !cfg.ignoreValue { |
| 603 |
if a.Value != b.Value { |
| 604 |
reasons = append(reasons, notEqualStr("Value", a.Value, b.Value)) |
| 605 |
} |
| 606 |
} |
| 607 |
if !slices.Equal(a.SpanID, b.SpanID) { |
| 608 |
reasons = append(reasons, notEqualStr("SpanID", a.SpanID, b.SpanID)) |
| 609 |
} |
| 610 |
if !slices.Equal(a.TraceID, b.TraceID) { |
| 611 |
reasons = append(reasons, notEqualStr("TraceID", a.TraceID, b.TraceID)) |
| 612 |
} |
| 613 |
return reasons |
| 614 |
} |
| 615 |
|
| 616 |
func diffSlices[T any](a, b []T, formatContext func(T) string, compare func(T, T) []string) string { |
| 617 |
visited := make([]bool, len(b)) |
| 618 |
var extraA []T |
| 619 |
var extraB []T |
| 620 |
|
| 621 |
for i := range a { |
| 622 |
found := false |
| 623 |
for j := range b { |
| 624 |
if visited[j] { |
| 625 |
continue |
| 626 |
} |
| 627 |
if len(compare(a[i], b[j])) == 0 { |
| 628 |
visited[j] = true |
| 629 |
found = true |
| 630 |
break |
| 631 |
} |
| 632 |
} |
| 633 |
if !found { |
| 634 |
extraA = append(extraA, a[i]) |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
for j := range b { |
| 639 |
if visited[j] { |
| 640 |
continue |
| 641 |
} |
| 642 |
extraB = append(extraB, b[j]) |
| 643 |
} |
| 644 |
|
| 645 |
if len(extraA) == 0 && len(extraB) == 0 { |
| 646 |
return "" |
| 647 |
} |
| 648 |
|
| 649 |
var msg bytes.Buffer |
| 650 |
minLen := min(len(extraB), len(extraA)) |
| 651 |
|
| 652 |
for i := range minLen { |
| 653 |
reasons := compare(extraA[i], extraB[i]) |
| 654 |
_, _ = msg.WriteString(formatContext(extraA[i]) + ":\n") |
| 655 |
for _, reason := range reasons { |
| 656 |
// Indent reasons |
| 657 |
lines := strings.SplitSeq(reason, "\n") |
| 658 |
for line := range lines { |
| 659 |
if line != "" { |
| 660 |
_, _ = msg.WriteString("\t" + line + "\n") |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
formatter := func(v T) string { |
| 667 |
return fmt.Sprintf("%#v", v) |
| 668 |
} |
| 669 |
if len(extraA) > minLen { |
| 670 |
_, _ = msg.WriteString("missing expected values:\n") |
| 671 |
for i := minLen; i < len(extraA); i++ { |
| 672 |
_, _ = msg.WriteString(formatter(extraA[i]) + "\n") |
| 673 |
} |
| 674 |
} |
| 675 |
if len(extraB) > minLen { |
| 676 |
_, _ = msg.WriteString("unexpected additional values:\n") |
| 677 |
for i := minLen; i < len(extraB); i++ { |
| 678 |
_, _ = msg.WriteString(formatter(extraB[i]) + "\n") |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
return msg.String() |
| 683 |
} |
| 684 |
|
| 685 |
func missingAttrStr(name string) string { |
| 686 |
return "missing attribute " + name |
| 687 |
} |
| 688 |
|
| 689 |
func hasAttributesExemplars[T int64 | float64]( |
| 690 |
exemplar metricdata.Exemplar[T], |
| 691 |
attrs ...attribute.KeyValue, |
| 692 |
) (reasons []string) { |
| 693 |
s := attribute.NewSet(exemplar.FilteredAttributes...) |
| 694 |
for _, attr := range attrs { |
| 695 |
val, ok := s.Value(attr.Key) |
| 696 |
if !ok { |
| 697 |
reasons = append(reasons, missingAttrStr(string(attr.Key))) |
| 698 |
continue |
| 699 |
} |
| 700 |
if val != attr.Value { |
| 701 |
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit())) |
| 702 |
} |
| 703 |
} |
| 704 |
return reasons |
| 705 |
} |
| 706 |
|
| 707 |
func hasAttributesDataPoints[T int64 | float64]( |
| 708 |
dp metricdata.DataPoint[T], |
| 709 |
attrs ...attribute.KeyValue, |
| 710 |
) (reasons []string) { |
| 711 |
for _, attr := range attrs { |
| 712 |
val, ok := dp.Attributes.Value(attr.Key) |
| 713 |
if !ok { |
| 714 |
reasons = append(reasons, missingAttrStr(string(attr.Key))) |
| 715 |
continue |
| 716 |
} |
| 717 |
if val != attr.Value { |
| 718 |
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit())) |
| 719 |
} |
| 720 |
} |
| 721 |
return reasons |
| 722 |
} |
| 723 |
|
| 724 |
func hasAttributesGauge[T int64 | float64](gauge metricdata.Gauge[T], attrs ...attribute.KeyValue) (reasons []string) { |
| 725 |
for n, dp := range gauge.DataPoints { |
| 726 |
reas := hasAttributesDataPoints(dp, attrs...) |
| 727 |
if len(reas) > 0 { |
| 728 |
reasons = append(reasons, fmt.Sprintf("gauge datapoint %d attributes:\n", n)) |
| 729 |
reasons = append(reasons, reas...) |
| 730 |
} |
| 731 |
} |
| 732 |
return reasons |
| 733 |
} |
| 734 |
|
| 735 |
func hasAttributesSum[T int64 | float64](sum metricdata.Sum[T], attrs ...attribute.KeyValue) (reasons []string) { |
| 736 |
for n, dp := range sum.DataPoints { |
| 737 |
reas := hasAttributesDataPoints(dp, attrs...) |
| 738 |
if len(reas) > 0 { |
| 739 |
reasons = append(reasons, fmt.Sprintf("sum datapoint %d attributes:\n", n)) |
| 740 |
reasons = append(reasons, reas...) |
| 741 |
} |
| 742 |
} |
| 743 |
return reasons |
| 744 |
} |
| 745 |
|
| 746 |
func hasAttributesHistogramDataPoints[T int64 | float64]( |
| 747 |
dp metricdata.HistogramDataPoint[T], |
| 748 |
attrs ...attribute.KeyValue, |
| 749 |
) (reasons []string) { |
| 750 |
for _, attr := range attrs { |
| 751 |
val, ok := dp.Attributes.Value(attr.Key) |
| 752 |
if !ok { |
| 753 |
reasons = append(reasons, missingAttrStr(string(attr.Key))) |
| 754 |
continue |
| 755 |
} |
| 756 |
if val != attr.Value { |
| 757 |
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit())) |
| 758 |
} |
| 759 |
} |
| 760 |
return reasons |
| 761 |
} |
| 762 |
|
| 763 |
func hasAttributesHistogram[T int64 | float64]( |
| 764 |
histogram metricdata.Histogram[T], |
| 765 |
attrs ...attribute.KeyValue, |
| 766 |
) (reasons []string) { |
| 767 |
for n, dp := range histogram.DataPoints { |
| 768 |
reas := hasAttributesHistogramDataPoints(dp, attrs...) |
| 769 |
if len(reas) > 0 { |
| 770 |
reasons = append(reasons, fmt.Sprintf("histogram datapoint %d attributes:\n", n)) |
| 771 |
reasons = append(reasons, reas...) |
| 772 |
} |
| 773 |
} |
| 774 |
return reasons |
| 775 |
} |
| 776 |
|
| 777 |
func hasAttributesExponentialHistogramDataPoints[T int64 | float64]( |
| 778 |
dp metricdata.ExponentialHistogramDataPoint[T], |
| 779 |
attrs ...attribute.KeyValue, |
| 780 |
) (reasons []string) { |
| 781 |
for _, attr := range attrs { |
| 782 |
val, ok := dp.Attributes.Value(attr.Key) |
| 783 |
if !ok { |
| 784 |
reasons = append(reasons, missingAttrStr(string(attr.Key))) |
| 785 |
continue |
| 786 |
} |
| 787 |
if val != attr.Value { |
| 788 |
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit())) |
| 789 |
} |
| 790 |
} |
| 791 |
return reasons |
| 792 |
} |
| 793 |
|
| 794 |
func hasAttributesExponentialHistogram[T int64 | float64]( |
| 795 |
histogram metricdata.ExponentialHistogram[T], |
| 796 |
attrs ...attribute.KeyValue, |
| 797 |
) (reasons []string) { |
| 798 |
for n, dp := range histogram.DataPoints { |
| 799 |
reas := hasAttributesExponentialHistogramDataPoints(dp, attrs...) |
| 800 |
if len(reas) > 0 { |
| 801 |
reasons = append(reasons, fmt.Sprintf("histogram datapoint %d attributes:\n", n)) |
| 802 |
reasons = append(reasons, reas...) |
| 803 |
} |
| 804 |
} |
| 805 |
return reasons |
| 806 |
} |
| 807 |
|
| 808 |
func hasAttributesAggregation(agg metricdata.Aggregation, attrs ...attribute.KeyValue) (reasons []string) { |
| 809 |
switch agg := agg.(type) { |
| 810 |
case metricdata.Gauge[int64]: |
| 811 |
reasons = hasAttributesGauge(agg, attrs...) |
| 812 |
case metricdata.Gauge[float64]: |
| 813 |
reasons = hasAttributesGauge(agg, attrs...) |
| 814 |
case metricdata.Sum[int64]: |
| 815 |
reasons = hasAttributesSum(agg, attrs...) |
| 816 |
case metricdata.Sum[float64]: |
| 817 |
reasons = hasAttributesSum(agg, attrs...) |
| 818 |
case metricdata.Histogram[int64]: |
| 819 |
reasons = hasAttributesHistogram(agg, attrs...) |
| 820 |
case metricdata.Histogram[float64]: |
| 821 |
reasons = hasAttributesHistogram(agg, attrs...) |
| 822 |
case metricdata.ExponentialHistogram[int64]: |
| 823 |
reasons = hasAttributesExponentialHistogram(agg, attrs...) |
| 824 |
case metricdata.ExponentialHistogram[float64]: |
| 825 |
reasons = hasAttributesExponentialHistogram(agg, attrs...) |
| 826 |
case metricdata.Summary: |
| 827 |
reasons = hasAttributesSummary(agg, attrs...) |
| 828 |
default: |
| 829 |
reasons = []string{fmt.Sprintf("unknown aggregation %T", agg)} |
| 830 |
} |
| 831 |
return reasons |
| 832 |
} |
| 833 |
|
| 834 |
func hasAttributesMetrics(metrics metricdata.Metrics, attrs ...attribute.KeyValue) (reasons []string) { |
| 835 |
reas := hasAttributesAggregation(metrics.Data, attrs...) |
| 836 |
if len(reas) > 0 { |
| 837 |
reasons = append(reasons, fmt.Sprintf("Metric %s:\n", metrics.Name)) |
| 838 |
reasons = append(reasons, reas...) |
| 839 |
} |
| 840 |
return reasons |
| 841 |
} |
| 842 |
|
| 843 |
func hasAttributesScopeMetrics(sm metricdata.ScopeMetrics, attrs ...attribute.KeyValue) (reasons []string) { |
| 844 |
for n, metrics := range sm.Metrics { |
| 845 |
reas := hasAttributesMetrics(metrics, attrs...) |
| 846 |
if len(reas) > 0 { |
| 847 |
reasons = append(reasons, fmt.Sprintf("ScopeMetrics %s Metrics %d:\n", sm.Scope.Name, n)) |
| 848 |
reasons = append(reasons, reas...) |
| 849 |
} |
| 850 |
} |
| 851 |
return reasons |
| 852 |
} |
| 853 |
|
| 854 |
func hasAttributesResourceMetrics(rm metricdata.ResourceMetrics, attrs ...attribute.KeyValue) (reasons []string) { |
| 855 |
for n, sm := range rm.ScopeMetrics { |
| 856 |
reas := hasAttributesScopeMetrics(sm, attrs...) |
| 857 |
if len(reas) > 0 { |
| 858 |
reasons = append(reasons, fmt.Sprintf("ResourceMetrics ScopeMetrics %d:\n", n)) |
| 859 |
reasons = append(reasons, reas...) |
| 860 |
} |
| 861 |
} |
| 862 |
return reasons |
| 863 |
} |
| 864 |
|
| 865 |
func hasAttributesSummary(summary metricdata.Summary, attrs ...attribute.KeyValue) (reasons []string) { |
| 866 |
for n, dp := range summary.DataPoints { |
| 867 |
reas := hasAttributesSummaryDataPoint(dp, attrs...) |
| 868 |
if len(reas) > 0 { |
| 869 |
reasons = append(reasons, fmt.Sprintf("summary datapoint %d attributes:\n", n)) |
| 870 |
reasons = append(reasons, reas...) |
| 871 |
} |
| 872 |
} |
| 873 |
return reasons |
| 874 |
} |
| 875 |
|
| 876 |
func hasAttributesSummaryDataPoint(dp metricdata.SummaryDataPoint, attrs ...attribute.KeyValue) (reasons []string) { |
| 877 |
for _, attr := range attrs { |
| 878 |
val, ok := dp.Attributes.Value(attr.Key) |
| 879 |
if !ok { |
| 880 |
reasons = append(reasons, missingAttrStr(string(attr.Key))) |
| 881 |
continue |
| 882 |
} |
| 883 |
if val != attr.Value { |
| 884 |
reasons = append(reasons, notEqualStr(string(attr.Key), attr.Value.Emit(), val.Emit())) |
| 885 |
} |
| 886 |
} |
| 887 |
return reasons |
| 888 |
} |