From c791db4239422eb0b69ac08da6b2f3a2e2b5823c Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Tue, 9 Aug 2022 21:10:43 +0000 Subject: [PATCH] use config over bool --- .../metricdata/metricdatatest/assertion.go | 32 ++++++++------ .../metricdatatest/assertion_test.go | 32 +++++++------- .../metricdata/metricdatatest/comparisons.go | 44 +++++++++---------- 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/sdk/metric/metricdata/metricdatatest/assertion.go b/sdk/metric/metricdata/metricdatatest/assertion.go index bde51ba13be..4c519d15d49 100644 --- a/sdk/metric/metricdata/metricdatatest/assertion.go +++ b/sdk/metric/metricdata/metricdatatest/assertion.go @@ -83,27 +83,27 @@ func AssertEqual[T Datatypes](t *testing.T, expected, actual T, opts ...Option) var r []string switch e := interface{}(expected).(type) { case metricdata.DataPoint[int64]: - r = equalDataPoints(e, aIface.(metricdata.DataPoint[int64]), cfg.ignoreTimestamp) + r = equalDataPoints(e, aIface.(metricdata.DataPoint[int64]), cfg) case metricdata.DataPoint[float64]: - r = equalDataPoints(e, aIface.(metricdata.DataPoint[float64]), cfg.ignoreTimestamp) + r = equalDataPoints(e, aIface.(metricdata.DataPoint[float64]), cfg) case metricdata.Gauge[int64]: - r = equalGauges(e, aIface.(metricdata.Gauge[int64]), cfg.ignoreTimestamp) + r = equalGauges(e, aIface.(metricdata.Gauge[int64]), cfg) case metricdata.Gauge[float64]: - r = equalGauges(e, aIface.(metricdata.Gauge[float64]), cfg.ignoreTimestamp) + r = equalGauges(e, aIface.(metricdata.Gauge[float64]), cfg) case metricdata.Histogram: - r = equalHistograms(e, aIface.(metricdata.Histogram), cfg.ignoreTimestamp) + r = equalHistograms(e, aIface.(metricdata.Histogram), cfg) case metricdata.HistogramDataPoint: - r = equalHistogramDataPoints(e, aIface.(metricdata.HistogramDataPoint), cfg.ignoreTimestamp) + r = equalHistogramDataPoints(e, aIface.(metricdata.HistogramDataPoint), cfg) case metricdata.Metrics: - r = equalMetrics(e, aIface.(metricdata.Metrics), cfg.ignoreTimestamp) + r = equalMetrics(e, aIface.(metricdata.Metrics), cfg) case metricdata.ResourceMetrics: - r = equalResourceMetrics(e, aIface.(metricdata.ResourceMetrics), cfg.ignoreTimestamp) + r = equalResourceMetrics(e, aIface.(metricdata.ResourceMetrics), cfg) case metricdata.ScopeMetrics: - r = equalScopeMetrics(e, aIface.(metricdata.ScopeMetrics), cfg.ignoreTimestamp) + r = equalScopeMetrics(e, aIface.(metricdata.ScopeMetrics), cfg) case metricdata.Sum[int64]: - r = equalSums(e, aIface.(metricdata.Sum[int64]), cfg.ignoreTimestamp) + r = equalSums(e, aIface.(metricdata.Sum[int64]), cfg) case metricdata.Sum[float64]: - r = equalSums(e, aIface.(metricdata.Sum[float64]), cfg.ignoreTimestamp) + r = equalSums(e, aIface.(metricdata.Sum[float64]), cfg) default: // We control all types passed to this, panic to signal developers // early they changed things in an incompatible way. @@ -118,9 +118,15 @@ func AssertEqual[T Datatypes](t *testing.T, expected, actual T, opts ...Option) } // AssertAggregationsEqual asserts that two Aggregations are equal. -func AssertAggregationsEqual(t *testing.T, expected, actual metricdata.Aggregation) bool { +func AssertAggregationsEqual(t *testing.T, expected, actual metricdata.Aggregation, opts ...Option) bool { t.Helper() - if r := equalAggregations(expected, actual, false); len(r) > 0 { + + cfg := config{} + for _, opt := range opts { + cfg = opt.apply(cfg) + } + + if r := equalAggregations(expected, actual, cfg); len(r) > 0 { t.Error(r) return false } diff --git a/sdk/metric/metricdata/metricdatatest/assertion_test.go b/sdk/metric/metricdata/metricdatatest/assertion_test.go index d0ec9c70251..71331f5a945 100644 --- a/sdk/metric/metricdata/metricdatatest/assertion_test.go +++ b/sdk/metric/metricdata/metricdatatest/assertion_test.go @@ -216,14 +216,14 @@ var ( } ) -type equalFunc[T Datatypes] func(T, T, bool) []string +type equalFunc[T Datatypes] func(T, T, config) []string func testDatatype[T Datatypes](a, b T, f equalFunc[T]) func(*testing.T) { return func(t *testing.T) { AssertEqual(t, a, a) AssertEqual(t, b, b) - r := f(a, b, false) + r := f(a, b, config{}) assert.Greaterf(t, len(r), 0, "%v == %v", a, b) } } @@ -233,7 +233,7 @@ func testDatatypeIgnoreTime[T Datatypes](a, b T, f equalFunc[T]) func(*testing.T AssertEqual(t, a, a) AssertEqual(t, b, b) - r := f(a, b, true) + r := f(a, b, config{ignoreTimestamp: true}) assert.Equalf(t, len(r), 0, "%v == %v", a, b) } } @@ -278,42 +278,42 @@ func TestAssertAggregationsEqual(t *testing.T) { AssertAggregationsEqual(t, gaugeFloat64A, gaugeFloat64A) AssertAggregationsEqual(t, histogramA, histogramA) - r := equalAggregations(sumInt64A, nil, false) + r := equalAggregations(sumInt64A, nil, config{}) assert.Len(t, r, 1, "should return nil comparison mismatch only") - r = equalAggregations(sumInt64A, gaugeInt64A, false) + r = equalAggregations(sumInt64A, gaugeInt64A, config{}) assert.Len(t, r, 1, "should return with type mismatch only") - r = equalAggregations(unknownAggregation{}, unknownAggregation{}, false) + r = equalAggregations(unknownAggregation{}, unknownAggregation{}, config{}) assert.Len(t, r, 1, "should return with unknown aggregation only") - r = equalAggregations(sumInt64A, sumInt64B, false) + r = equalAggregations(sumInt64A, sumInt64B, config{}) assert.Greaterf(t, len(r), 0, "%v == %v", sumInt64A, sumInt64B) - r = equalAggregations(sumInt64A, sumInt64C, true) + r = equalAggregations(sumInt64A, sumInt64C, config{ignoreTimestamp: true}) assert.Equalf(t, len(r), 0, "%v == %v", sumInt64A, sumInt64C) - r = equalAggregations(sumFloat64A, sumFloat64B, false) + r = equalAggregations(sumFloat64A, sumFloat64B, config{}) assert.Greaterf(t, len(r), 0, "%v == %v", sumFloat64A, sumFloat64B) - r = equalAggregations(sumFloat64A, sumFloat64C, true) + r = equalAggregations(sumFloat64A, sumFloat64C, config{ignoreTimestamp: true}) assert.Equalf(t, len(r), 0, "%v == %v", sumFloat64A, sumFloat64C) - r = equalAggregations(gaugeInt64A, gaugeInt64B, false) + r = equalAggregations(gaugeInt64A, gaugeInt64B, config{}) assert.Greaterf(t, len(r), 0, "%v == %v", gaugeInt64A, gaugeInt64B) - r = equalAggregations(gaugeInt64A, gaugeInt64C, true) + r = equalAggregations(gaugeInt64A, gaugeInt64C, config{ignoreTimestamp: true}) assert.Equalf(t, len(r), 0, "%v == %v", gaugeInt64A, gaugeInt64C) - r = equalAggregations(gaugeFloat64A, gaugeFloat64B, false) + r = equalAggregations(gaugeFloat64A, gaugeFloat64B, config{}) assert.Greaterf(t, len(r), 0, "%v == %v", gaugeFloat64A, gaugeFloat64B) - r = equalAggregations(gaugeFloat64A, gaugeFloat64C, true) + r = equalAggregations(gaugeFloat64A, gaugeFloat64C, config{ignoreTimestamp: true}) assert.Equalf(t, len(r), 0, "%v == %v", gaugeFloat64A, gaugeFloat64C) - r = equalAggregations(histogramA, histogramB, false) + r = equalAggregations(histogramA, histogramB, config{}) assert.Greaterf(t, len(r), 0, "%v == %v", histogramA, histogramB) - r = equalAggregations(histogramA, histogramC, true) + r = equalAggregations(histogramA, histogramC, config{ignoreTimestamp: true}) assert.Equalf(t, len(r), 0, "%v == %v", histogramA, histogramC) } diff --git a/sdk/metric/metricdata/metricdatatest/comparisons.go b/sdk/metric/metricdata/metricdatatest/comparisons.go index ed8f11f62cf..4c40f3bf67d 100644 --- a/sdk/metric/metricdata/metricdatatest/comparisons.go +++ b/sdk/metric/metricdata/metricdatatest/comparisons.go @@ -31,7 +31,7 @@ import ( // // 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, ignoreTimestamp bool) (reasons []string) { +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)) } @@ -40,7 +40,7 @@ func equalResourceMetrics(a, b metricdata.ResourceMetrics, ignoreTimestamp bool) a.ScopeMetrics, b.ScopeMetrics, func(a, b metricdata.ScopeMetrics) bool { - r := equalScopeMetrics(a, b, ignoreTimestamp) + r := equalScopeMetrics(a, b, cfg) return len(r) == 0 }, )) @@ -55,7 +55,7 @@ func equalResourceMetrics(a, b metricdata.ResourceMetrics, ignoreTimestamp bool) // // 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, ignoreTimestamp bool) (reasons []string) { +func equalScopeMetrics(a, b metricdata.ScopeMetrics, cfg config) (reasons []string) { if a.Scope != b.Scope { reasons = append(reasons, notEqualStr("Scope", a.Scope, b.Scope)) } @@ -64,7 +64,7 @@ func equalScopeMetrics(a, b metricdata.ScopeMetrics, ignoreTimestamp bool) (reas a.Metrics, b.Metrics, func(a, b metricdata.Metrics) bool { - r := equalMetrics(a, b, ignoreTimestamp) + r := equalMetrics(a, b, cfg) return len(r) == 0 }, )) @@ -76,7 +76,7 @@ func equalScopeMetrics(a, b metricdata.ScopeMetrics, ignoreTimestamp bool) (reas // equalMetrics returns reasons Metrics are not equal. If they are equal, the // returned reasons will be empty. -func equalMetrics(a, b metricdata.Metrics, ignoreTimestamp bool) (reasons []string) { +func equalMetrics(a, b metricdata.Metrics, cfg config) (reasons []string) { if a.Name != b.Name { reasons = append(reasons, notEqualStr("Name", a.Name, b.Name)) } @@ -87,7 +87,7 @@ func equalMetrics(a, b metricdata.Metrics, ignoreTimestamp bool) (reasons []stri reasons = append(reasons, notEqualStr("Unit", a.Unit, b.Unit)) } - r := equalAggregations(a.Data, b.Data, ignoreTimestamp) + r := equalAggregations(a.Data, b.Data, cfg) if len(r) > 0 { reasons = append(reasons, "Metrics Data not equal:") reasons = append(reasons, r...) @@ -97,7 +97,7 @@ func equalMetrics(a, b metricdata.Metrics, ignoreTimestamp bool) (reasons []stri // 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, ignoreTimestamp bool) (reasons []string) { +func equalAggregations(a, b metricdata.Aggregation, cfg config) (reasons []string) { if a == nil || b == nil { if a != b { return []string{notEqualStr("Aggregation", a, b)} @@ -111,31 +111,31 @@ func equalAggregations(a, b metricdata.Aggregation, ignoreTimestamp bool) (reaso switch v := a.(type) { case metricdata.Gauge[int64]: - r := equalGauges(v, b.(metricdata.Gauge[int64]), ignoreTimestamp) + 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]), ignoreTimestamp) + 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]), ignoreTimestamp) + 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]), ignoreTimestamp) + 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: - r := equalHistograms(v, b.(metricdata.Histogram), ignoreTimestamp) + r := equalHistograms(v, b.(metricdata.Histogram), cfg) if len(r) > 0 { reasons = append(reasons, "Histogram not equal:") reasons = append(reasons, r...) @@ -151,12 +151,12 @@ func equalAggregations(a, b metricdata.Aggregation, ignoreTimestamp bool) (reaso // // 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], ignoreTimestamp bool) (reasons []string) { +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, ignoreTimestamp) + r := equalDataPoints(a, b, cfg) return len(r) == 0 }, )) @@ -171,7 +171,7 @@ func equalGauges[N int64 | float64](a, b metricdata.Gauge[N], ignoreTimestamp bo // // 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], ignoreTimestamp bool) (reasons []string) { +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)) } @@ -183,7 +183,7 @@ func equalSums[N int64 | float64](a, b metricdata.Sum[N], ignoreTimestamp bool) a.DataPoints, b.DataPoints, func(a, b metricdata.DataPoint[N]) bool { - r := equalDataPoints(a, b, ignoreTimestamp) + r := equalDataPoints(a, b, cfg) return len(r) == 0 }, )) @@ -198,7 +198,7 @@ func equalSums[N int64 | float64](a, b metricdata.Sum[N], ignoreTimestamp bool) // // The DataPoints each Histogram contains are compared based on containing the // same HistogramDataPoint, not the order they are stored in. -func equalHistograms(a, b metricdata.Histogram, ignoreTimestamp bool) (reasons []string) { +func equalHistograms(a, b metricdata.Histogram, cfg config) (reasons []string) { if a.Temporality != b.Temporality { reasons = append(reasons, notEqualStr("Temporality", a.Temporality, b.Temporality)) } @@ -207,7 +207,7 @@ func equalHistograms(a, b metricdata.Histogram, ignoreTimestamp bool) (reasons [ a.DataPoints, b.DataPoints, func(a, b metricdata.HistogramDataPoint) bool { - r := equalHistogramDataPoints(a, b, ignoreTimestamp) + r := equalHistogramDataPoints(a, b, cfg) return len(r) == 0 }, )) @@ -219,7 +219,7 @@ func equalHistograms(a, b metricdata.Histogram, ignoreTimestamp bool) (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], ignoreTimestamp bool) (reasons []string) { // nolint: revive // Intentional internal control flag +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", @@ -228,7 +228,7 @@ func equalDataPoints[N int64 | float64](a, b metricdata.DataPoint[N], ignoreTime )) } - if !ignoreTimestamp { + if !cfg.ignoreTimestamp { if !a.StartTime.Equal(b.StartTime) { reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano())) } @@ -245,7 +245,7 @@ func equalDataPoints[N int64 | float64](a, b metricdata.DataPoint[N], ignoreTime // equalHistogramDataPoints returns reasons HistogramDataPoints are not equal. // If they are equal, the returned reasons will be empty. -func equalHistogramDataPoints(a, b metricdata.HistogramDataPoint, ignoreTimestamp bool) (reasons []string) { // nolint: revive // Intentional internal control flag +func equalHistogramDataPoints(a, b metricdata.HistogramDataPoint, cfg config) (reasons []string) { // nolint: revive // Intentional internal control flag if !a.Attributes.Equals(&b.Attributes) { reasons = append(reasons, notEqualStr( "Attributes", @@ -253,7 +253,7 @@ func equalHistogramDataPoints(a, b metricdata.HistogramDataPoint, ignoreTimestam b.Attributes.Encoded(attribute.DefaultEncoder()), )) } - if !ignoreTimestamp { + if !cfg.ignoreTimestamp { if !a.StartTime.Equal(b.StartTime) { reasons = append(reasons, notEqualStr("StartTime", a.StartTime.UnixNano(), b.StartTime.UnixNano())) }