Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a TB interface in metricdatatest #4483

Merged
merged 9 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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).
Expand Down
21 changes: 17 additions & 4 deletions sdk/metric/metricdata/metricdatatest/assertion.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions sdk/metric/metricdata/metricdatatest/assertion_test.go
Expand Up @@ -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))
Expand Down