diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e43e3d620b..b560cff9454 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 d19696f583f..dc8e0d1efaf 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 11ba70ed679..514f5c03267 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))