1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-11-30 08:46:54 +02:00

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 <pellared@hotmail.com>

* 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 <pellared@hotmail.com>

* change formatting for last testing.TB too

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
This commit is contained in:
Damien Mathieu 2023-09-11 16:26:21 +02:00 committed by GitHub
parent 77d6237575
commit ac4fca2260
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -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).

View File

@ -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

View File

@ -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))