From ac4fca2260ae51e7318a2680e8e57771340e958a Mon Sep 17 00:00:00 2001 From: Damien Mathieu Date: Mon, 11 Sep 2023 16:26:21 +0200 Subject: [PATCH] Use a TB interface in metricdatatest (#4483) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * use testing.TB interface in metricdatatest * add changelog entry * use our own TB interface * Update CHANGELOG.md Co-authored-by: Robert Pająk * rename TB to TestingT * SIG meeting feedback * ensure *testing.T implements TestingT * Update sdk/metric/metricdata/metricdatatest/assertion.go Co-authored-by: Robert Pająk * change formatting for last testing.TB too --------- Co-authored-by: Robert Pająk --- CHANGELOG.md | 4 ++++ .../metricdata/metricdatatest/assertion.go | 21 +++++++++++++++---- .../metricdatatest/assertion_test.go | 4 ++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e43e3d62..b560cff94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add `WithProducer` option in `go.opentelemetry.op/otel/exporters/prometheus` to restore the ability to register producers on the prometheus exporter's manual reader. (#4473) - Add `IgnoreValue` option in `go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest` to allow ignoring values when comparing metrics. (#4447) +### Changed + +- Use a `TestingT` interface instead of `*testing.T` struct in `go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest`. (#4483) + ### Deprecated - The `NewMetricExporter` in `go.opentelemetry.io/otel/bridge/opencensus` was deprecated in `v0.35.0` (#3541). diff --git a/sdk/metric/metricdata/metricdatatest/assertion.go b/sdk/metric/metricdata/metricdatatest/assertion.go index d19696f58..dc8e0d1ef 100644 --- a/sdk/metric/metricdata/metricdatatest/assertion.go +++ b/sdk/metric/metricdata/metricdatatest/assertion.go @@ -18,7 +18,6 @@ package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata import ( "fmt" - "testing" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/metric/metricdata" @@ -53,6 +52,20 @@ type Datatypes interface { // Aggregation and Value type from metricdata are not included here. } +// TestingT is an interface that implements [testing.T], but without the +// private method of [testing.TB], so other testing packages can rely on it as +// well. +// The methods in this interface must match the [testing.TB] interface. +type TestingT interface { + Helper() + // DO NOT CHANGE: any modification will not be backwards compatible and + // must never be done outside of a new major release. + + Error(...any) + // DO NOT CHANGE: any modification will not be backwards compatible and + // must never be done outside of a new major release. +} + type config struct { ignoreTimestamp bool ignoreExemplars bool @@ -110,7 +123,7 @@ func IgnoreValue() Option { // AssertEqual asserts that the two concrete data-types from the metricdata // package are equal. -func AssertEqual[T Datatypes](t *testing.T, expected, actual T, opts ...Option) bool { +func AssertEqual[T Datatypes](t TestingT, expected, actual T, opts ...Option) bool { t.Helper() cfg := newConfig(opts) @@ -178,7 +191,7 @@ 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, opts ...Option) bool { +func AssertAggregationsEqual(t TestingT, expected, actual metricdata.Aggregation, opts ...Option) bool { t.Helper() cfg := newConfig(opts) @@ -190,7 +203,7 @@ func AssertAggregationsEqual(t *testing.T, expected, actual metricdata.Aggregati } // AssertHasAttributes asserts that all Datapoints or HistogramDataPoints have all passed attrs. -func AssertHasAttributes[T Datatypes](t *testing.T, actual T, attrs ...attribute.KeyValue) bool { +func AssertHasAttributes[T Datatypes](t TestingT, actual T, attrs ...attribute.KeyValue) bool { t.Helper() var reasons []string diff --git a/sdk/metric/metricdata/metricdatatest/assertion_test.go b/sdk/metric/metricdata/metricdatatest/assertion_test.go index 11ba70ed6..514f5c032 100644 --- a/sdk/metric/metricdata/metricdatatest/assertion_test.go +++ b/sdk/metric/metricdata/metricdatatest/assertion_test.go @@ -619,6 +619,10 @@ func testDatatypeIgnoreValue[T Datatypes](a, b T, f equalFunc[T]) func(*testing. } } +func TestTestingTImplementation(t *testing.T) { + assert.Implements(t, (*TestingT)(nil), t) +} + func TestAssertEqual(t *testing.T) { t.Run("ResourceMetrics", testDatatype(resourceMetricsA, resourceMetricsB, equalResourceMetrics)) t.Run("ScopeMetrics", testDatatype(scopeMetricsA, scopeMetricsB, equalScopeMetrics))