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

Ignore value option for metricdatatest #4447

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

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

### Deprecated

Expand Down
15 changes: 15 additions & 0 deletions sdk/metric/metricdata/metricdatatest/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Datatypes interface {
type config struct {
ignoreTimestamp bool
ignoreExemplars bool
ignoreValue bool
}

func newConfig(opts []Option) config {
Expand Down Expand Up @@ -93,6 +94,20 @@ func IgnoreExemplars() Option {
})
}

// IgnoreValue disables checking if values are different. This can be
// useful for non-deterministic values, like measured durations.
//
// This will ignore the value and trace information for Exemplars;
// the buckets, zero count, scale, sum, max, min, and counts of
// ExponentialHistogramDataPoints; the buckets, sum, count, max,
// and min of HistogramDataPoints; the value of DataPoints.
func IgnoreValue() Option {
return fnOption(func(cfg config) config {
cfg.ignoreValue = true
return cfg
})
}

// 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 {
Expand Down
183 changes: 183 additions & 0 deletions sdk/metric/metricdata/metricdatatest/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ var (
SpanID: spanIDA,
TraceID: traceIDA,
}
exemplarInt64D = metricdata.Exemplar[int64]{
FilteredAttributes: fltrAttrA,
Time: endA,
Value: 12,
SpanID: spanIDA,
TraceID: traceIDA,
}
exemplarFloat64D = metricdata.Exemplar[float64]{
FilteredAttributes: fltrAttrA,
Time: endA,
Value: 12.0,
SpanID: spanIDA,
TraceID: traceIDA,
}

dataPointInt64A = metricdata.DataPoint[int64]{
Attributes: attrA,
Expand Down Expand Up @@ -128,6 +142,20 @@ var (
Value: -1.0,
Exemplars: []metricdata.Exemplar[float64]{exemplarFloat64C},
}
dataPointInt64D = metricdata.DataPoint[int64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Value: 2,
Exemplars: []metricdata.Exemplar[int64]{exemplarInt64A},
}
dataPointFloat64D = metricdata.DataPoint[float64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Value: 2.0,
Exemplars: []metricdata.Exemplar[float64]{exemplarFloat64A},
}

minFloat64A = metricdata.NewExtrema(-1.)
minInt64A = metricdata.NewExtrema[int64](-1)
Expand Down Expand Up @@ -204,6 +232,30 @@ var (
Sum: 2,
Exemplars: []metricdata.Exemplar[float64]{exemplarFloat64C},
}
histogramDataPointInt64D = metricdata.HistogramDataPoint[int64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Count: 3,
Bounds: []float64{0, 10, 100},
BucketCounts: []uint64{1, 1, 1},
Max: maxInt64B,
Min: minInt64B,
Sum: 3,
Exemplars: []metricdata.Exemplar[int64]{exemplarInt64A},
}
histogramDataPointFloat64D = metricdata.HistogramDataPoint[float64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Count: 3,
Bounds: []float64{0, 10, 100},
BucketCounts: []uint64{1, 1, 1},
Max: maxFloat64B,
Min: minFloat64B,
Sum: 3,
Exemplars: []metricdata.Exemplar[float64]{exemplarFloat64A},
}

exponentialBucket2 = metricdata.ExponentialBucket{
Offset: 2,
Expand Down Expand Up @@ -301,6 +353,34 @@ var (
NegativeBucket: exponentialBucket2,
Exemplars: []metricdata.Exemplar[float64]{exemplarFloat64C},
}
exponentialHistogramDataPointInt64D = metricdata.ExponentialHistogramDataPoint[int64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Count: 6,
Min: minInt64B,
Max: maxInt64B,
Sum: 3,
Scale: 2,
ZeroCount: 3,
PositiveBucket: exponentialBucket4,
NegativeBucket: exponentialBucket5,
Exemplars: []metricdata.Exemplar[int64]{exemplarInt64A},
}
exponentialHistogramDataPointFloat64D = metricdata.ExponentialHistogramDataPoint[float64]{
Attributes: attrA,
StartTime: startA,
Time: endA,
Count: 6,
Min: minFloat64B,
Max: maxFloat64B,
Sum: 3,
Scale: 2,
ZeroCount: 3,
PositiveBucket: exponentialBucket4,
NegativeBucket: exponentialBucket5,
Exemplars: []metricdata.Exemplar[float64]{exemplarFloat64A},
}

gaugeInt64A = metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64A},
Expand All @@ -320,6 +400,12 @@ var (
gaugeFloat64C = metricdata.Gauge[float64]{
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64C},
}
gaugeInt64D = metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64D},
}
gaugeFloat64D = metricdata.Gauge[float64]{
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64D},
}

sumInt64A = metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
Expand Down Expand Up @@ -351,6 +437,16 @@ var (
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64C},
}
sumInt64D = metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{dataPointInt64D},
}
sumFloat64D = metricdata.Sum[float64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[float64]{dataPointFloat64D},
}

histogramInt64A = metricdata.Histogram[int64]{
Temporality: metricdata.CumulativeTemporality,
Expand All @@ -376,6 +472,14 @@ var (
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint[float64]{histogramDataPointFloat64C},
}
histogramInt64D = metricdata.Histogram[int64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint[int64]{histogramDataPointInt64D},
}
histogramFloat64D = metricdata.Histogram[float64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint[float64]{histogramDataPointFloat64D},
}

exponentialHistogramInt64A = metricdata.ExponentialHistogram[int64]{
Temporality: metricdata.CumulativeTemporality,
Expand All @@ -401,6 +505,14 @@ var (
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{exponentialHistogramDataPointFloat64C},
}
exponentialHistogramInt64D = metricdata.ExponentialHistogram[int64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[int64]{exponentialHistogramDataPointInt64D},
}
exponentialHistogramFloat64D = metricdata.ExponentialHistogram[float64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{exponentialHistogramDataPointFloat64D},
}

metricsA = metricdata.Metrics{
Name: "A",
Expand All @@ -420,6 +532,12 @@ var (
Unit: "1",
Data: sumInt64C,
}
metricsD = metricdata.Metrics{
Name: "A",
Description: "A desc",
Unit: "1",
Data: sumInt64D,
}

scopeMetricsA = metricdata.ScopeMetrics{
Scope: instrumentation.Scope{Name: "A"},
Expand All @@ -433,6 +551,10 @@ var (
Scope: instrumentation.Scope{Name: "A"},
Metrics: []metricdata.Metrics{metricsC},
}
scopeMetricsD = metricdata.ScopeMetrics{
Scope: instrumentation.Scope{Name: "A"},
Metrics: []metricdata.Metrics{metricsD},
}

resourceMetricsA = metricdata.ResourceMetrics{
Resource: resource.NewSchemaless(attribute.String("resource", "A")),
Expand All @@ -446,6 +568,10 @@ var (
Resource: resource.NewSchemaless(attribute.String("resource", "A")),
ScopeMetrics: []metricdata.ScopeMetrics{scopeMetricsC},
}
resourceMetricsD = metricdata.ResourceMetrics{
Resource: resource.NewSchemaless(attribute.String("resource", "A")),
ScopeMetrics: []metricdata.ScopeMetrics{scopeMetricsD},
}
)

type equalFunc[T Datatypes] func(T, T, config) []string
Expand Down Expand Up @@ -482,6 +608,17 @@ func testDatatypeIgnoreExemplars[T Datatypes](a, b T, f equalFunc[T]) func(*test
}
}

func testDatatypeIgnoreValue[T Datatypes](a, b T, f equalFunc[T]) func(*testing.T) {
return func(t *testing.T) {
AssertEqual(t, a, a)
AssertEqual(t, b, b)

c := newConfig([]Option{IgnoreValue()})
r := f(a, b, c)
assert.Len(t, r, 0, "unexpected inequality")
}
}

func TestAssertEqual(t *testing.T) {
t.Run("ResourceMetrics", testDatatype(resourceMetricsA, resourceMetricsB, equalResourceMetrics))
t.Run("ScopeMetrics", testDatatype(scopeMetricsA, scopeMetricsB, equalScopeMetrics))
Expand Down Expand Up @@ -557,6 +694,28 @@ func TestAssertEqualIgnoreExemplars(t *testing.T) {
t.Run("ExponentialHistogramDataPointFloat64", testDatatypeIgnoreExemplars(exponentialHistogramDataPointFloat64A, ehdpFloat64, equalExponentialHistogramDataPoints[float64]))
}

func TestAssertEqualIgnoreValue(t *testing.T) {
t.Run("ResourceMetrics", testDatatypeIgnoreValue(resourceMetricsA, resourceMetricsD, equalResourceMetrics))
t.Run("ScopeMetrics", testDatatypeIgnoreValue(scopeMetricsA, scopeMetricsD, equalScopeMetrics))
t.Run("Metrics", testDatatypeIgnoreValue(metricsA, metricsD, equalMetrics))
t.Run("HistogramInt64", testDatatypeIgnoreValue(histogramInt64A, histogramInt64D, equalHistograms[int64]))
t.Run("HistogramFloat64", testDatatypeIgnoreValue(histogramFloat64A, histogramFloat64D, equalHistograms[float64]))
t.Run("SumInt64", testDatatypeIgnoreValue(sumInt64A, sumInt64D, equalSums[int64]))
t.Run("SumFloat64", testDatatypeIgnoreValue(sumFloat64A, sumFloat64D, equalSums[float64]))
t.Run("GaugeInt64", testDatatypeIgnoreValue(gaugeInt64A, gaugeInt64D, equalGauges[int64]))
t.Run("GaugeFloat64", testDatatypeIgnoreValue(gaugeFloat64A, gaugeFloat64D, equalGauges[float64]))
t.Run("HistogramDataPointInt64", testDatatypeIgnoreValue(histogramDataPointInt64A, histogramDataPointInt64D, equalHistogramDataPoints[int64]))
t.Run("HistogramDataPointFloat64", testDatatypeIgnoreValue(histogramDataPointFloat64A, histogramDataPointFloat64D, equalHistogramDataPoints[float64]))
t.Run("DataPointInt64", testDatatypeIgnoreValue(dataPointInt64A, dataPointInt64D, equalDataPoints[int64]))
t.Run("DataPointFloat64", testDatatypeIgnoreValue(dataPointFloat64A, dataPointFloat64D, equalDataPoints[float64]))
t.Run("ExemplarInt64", testDatatypeIgnoreValue(exemplarInt64A, exemplarInt64D, equalExemplars[int64]))
t.Run("ExemplarFloat64", testDatatypeIgnoreValue(exemplarFloat64A, exemplarFloat64D, equalExemplars[float64]))
t.Run("ExponentialHistogramInt64", testDatatypeIgnoreValue(exponentialHistogramInt64A, exponentialHistogramInt64D, equalExponentialHistograms[int64]))
t.Run("ExponentialHistogramFloat64", testDatatypeIgnoreValue(exponentialHistogramFloat64A, exponentialHistogramFloat64D, equalExponentialHistograms[float64]))
t.Run("ExponentialHistogramDataPointInt64", testDatatypeIgnoreValue(exponentialHistogramDataPointInt64A, exponentialHistogramDataPointInt64D, equalExponentialHistogramDataPoints[int64]))
t.Run("ExponentialHistogramDataPointFloat64", testDatatypeIgnoreValue(exponentialHistogramDataPointFloat64A, exponentialHistogramDataPointFloat64D, equalExponentialHistogramDataPoints[float64]))
}

type unknownAggregation struct {
metricdata.Aggregation
}
Expand Down Expand Up @@ -587,47 +746,71 @@ func TestAssertAggregationsEqual(t *testing.T) {
r = equalAggregations(sumInt64A, sumInt64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "sums should be equal: %v", r)

r = equalAggregations(sumInt64A, sumInt64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", sumInt64A, sumInt64D)

r = equalAggregations(sumFloat64A, sumFloat64B, config{})
assert.Greaterf(t, len(r), 0, "sums should not be equal: %v == %v", sumFloat64A, sumFloat64B)

r = equalAggregations(sumFloat64A, sumFloat64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "sums should be equal: %v", r)

r = equalAggregations(sumFloat64A, sumFloat64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", sumFloat64A, sumFloat64D)

r = equalAggregations(gaugeInt64A, gaugeInt64B, config{})
assert.Greaterf(t, len(r), 0, "gauges should not be equal: %v == %v", gaugeInt64A, gaugeInt64B)

r = equalAggregations(gaugeInt64A, gaugeInt64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "gauges should be equal: %v", r)

r = equalAggregations(gaugeInt64A, gaugeInt64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", gaugeInt64A, gaugeInt64D)

r = equalAggregations(gaugeFloat64A, gaugeFloat64B, config{})
assert.Greaterf(t, len(r), 0, "gauges should not be equal: %v == %v", gaugeFloat64A, gaugeFloat64B)

r = equalAggregations(gaugeFloat64A, gaugeFloat64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "gauges should be equal: %v", r)

r = equalAggregations(gaugeFloat64A, gaugeFloat64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", gaugeFloat64A, gaugeFloat64D)

r = equalAggregations(histogramInt64A, histogramInt64B, config{})
assert.Greaterf(t, len(r), 0, "histograms should not be equal: %v == %v", histogramInt64A, histogramInt64B)

r = equalAggregations(histogramInt64A, histogramInt64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "histograms should be equal: %v", r)

r = equalAggregations(histogramInt64A, histogramInt64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", histogramInt64A, histogramInt64D)

r = equalAggregations(histogramFloat64A, histogramFloat64B, config{})
assert.Greaterf(t, len(r), 0, "histograms should not be equal: %v == %v", histogramFloat64A, histogramFloat64B)

r = equalAggregations(histogramFloat64A, histogramFloat64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "histograms should be equal: %v", r)

r = equalAggregations(histogramFloat64A, histogramFloat64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", histogramFloat64A, histogramFloat64D)

r = equalAggregations(exponentialHistogramInt64A, exponentialHistogramInt64B, config{})
assert.Greaterf(t, len(r), 0, "exponential histograms should not be equal: %v == %v", exponentialHistogramInt64A, exponentialHistogramInt64B)

r = equalAggregations(exponentialHistogramInt64A, exponentialHistogramInt64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "exponential histograms should be equal: %v", r)

r = equalAggregations(exponentialHistogramInt64A, exponentialHistogramInt64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", exponentialHistogramInt64A, exponentialHistogramInt64D)

r = equalAggregations(exponentialHistogramFloat64A, exponentialHistogramFloat64B, config{})
assert.Greaterf(t, len(r), 0, "exponential histograms should not be equal: %v == %v", exponentialHistogramFloat64A, exponentialHistogramFloat64B)

r = equalAggregations(exponentialHistogramFloat64A, exponentialHistogramFloat64C, config{ignoreTimestamp: true})
assert.Len(t, r, 0, "exponential histograms should be equal: %v", r)

r = equalAggregations(exponentialHistogramFloat64A, exponentialHistogramFloat64D, config{ignoreValue: true})
assert.Len(t, r, 0, "value should be ignored: %v == %v", exponentialHistogramFloat64A, exponentialHistogramFloat64D)
}

func TestAssertAttributes(t *testing.T) {
Expand Down
Loading