Skip to content

Commit ac4fca2

Browse files
dmathieupellared
andauthored
Use a TB interface in metricdatatest (#4483)
* use testing.TB interface in metricdatatest * add changelog entry * use our own TB interface * Update CHANGELOG.md Co-authored-by: Robert Pająk <[email protected]> * 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 <[email protected]> * change formatting for last testing.TB too --------- Co-authored-by: Robert Pająk <[email protected]>
1 parent 77d6237 commit ac4fca2

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1313
- 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)
1414
- Add `IgnoreValue` option in `go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest` to allow ignoring values when comparing metrics. (#4447)
1515

16+
### Changed
17+
18+
- Use a `TestingT` interface instead of `*testing.T` struct in `go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest`. (#4483)
19+
1620
### Deprecated
1721

1822
- The `NewMetricExporter` in `go.opentelemetry.io/otel/bridge/opencensus` was deprecated in `v0.35.0` (#3541).

sdk/metric/metricdata/metricdatatest/assertion.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata
1818

1919
import (
2020
"fmt"
21-
"testing"
2221

2322
"go.opentelemetry.io/otel/attribute"
2423
"go.opentelemetry.io/otel/sdk/metric/metricdata"
@@ -53,6 +52,20 @@ type Datatypes interface {
5352
// Aggregation and Value type from metricdata are not included here.
5453
}
5554

55+
// TestingT is an interface that implements [testing.T], but without the
56+
// private method of [testing.TB], so other testing packages can rely on it as
57+
// well.
58+
// The methods in this interface must match the [testing.TB] interface.
59+
type TestingT interface {
60+
Helper()
61+
// DO NOT CHANGE: any modification will not be backwards compatible and
62+
// must never be done outside of a new major release.
63+
64+
Error(...any)
65+
// DO NOT CHANGE: any modification will not be backwards compatible and
66+
// must never be done outside of a new major release.
67+
}
68+
5669
type config struct {
5770
ignoreTimestamp bool
5871
ignoreExemplars bool
@@ -110,7 +123,7 @@ func IgnoreValue() Option {
110123

111124
// AssertEqual asserts that the two concrete data-types from the metricdata
112125
// package are equal.
113-
func AssertEqual[T Datatypes](t *testing.T, expected, actual T, opts ...Option) bool {
126+
func AssertEqual[T Datatypes](t TestingT, expected, actual T, opts ...Option) bool {
114127
t.Helper()
115128

116129
cfg := newConfig(opts)
@@ -178,7 +191,7 @@ func AssertEqual[T Datatypes](t *testing.T, expected, actual T, opts ...Option)
178191
}
179192

180193
// AssertAggregationsEqual asserts that two Aggregations are equal.
181-
func AssertAggregationsEqual(t *testing.T, expected, actual metricdata.Aggregation, opts ...Option) bool {
194+
func AssertAggregationsEqual(t TestingT, expected, actual metricdata.Aggregation, opts ...Option) bool {
182195
t.Helper()
183196

184197
cfg := newConfig(opts)
@@ -190,7 +203,7 @@ func AssertAggregationsEqual(t *testing.T, expected, actual metricdata.Aggregati
190203
}
191204

192205
// AssertHasAttributes asserts that all Datapoints or HistogramDataPoints have all passed attrs.
193-
func AssertHasAttributes[T Datatypes](t *testing.T, actual T, attrs ...attribute.KeyValue) bool {
206+
func AssertHasAttributes[T Datatypes](t TestingT, actual T, attrs ...attribute.KeyValue) bool {
194207
t.Helper()
195208

196209
var reasons []string

sdk/metric/metricdata/metricdatatest/assertion_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ func testDatatypeIgnoreValue[T Datatypes](a, b T, f equalFunc[T]) func(*testing.
619619
}
620620
}
621621

622+
func TestTestingTImplementation(t *testing.T) {
623+
assert.Implements(t, (*TestingT)(nil), t)
624+
}
625+
622626
func TestAssertEqual(t *testing.T) {
623627
t.Run("ResourceMetrics", testDatatype(resourceMetricsA, resourceMetricsB, equalResourceMetrics))
624628
t.Run("ScopeMetrics", testDatatype(scopeMetricsA, scopeMetricsB, equalScopeMetrics))

0 commit comments

Comments
 (0)