| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package metricdatatest provides testing functionality for use with the |
| 5 |
// metricdata package. |
| 6 |
package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 13 |
) |
| 14 |
|
| 15 |
// Datatypes are the concrete data-types the metricdata package provides. |
| 16 |
type Datatypes interface { |
| 17 |
metricdata.DataPoint[float64] | |
| 18 |
metricdata.DataPoint[int64] | |
| 19 |
metricdata.Gauge[float64] | |
| 20 |
metricdata.Gauge[int64] | |
| 21 |
metricdata.Histogram[float64] | |
| 22 |
metricdata.Histogram[int64] | |
| 23 |
metricdata.HistogramDataPoint[float64] | |
| 24 |
metricdata.HistogramDataPoint[int64] | |
| 25 |
metricdata.Extrema[int64] | |
| 26 |
metricdata.Extrema[float64] | |
| 27 |
metricdata.Metrics | |
| 28 |
metricdata.ResourceMetrics | |
| 29 |
metricdata.ScopeMetrics | |
| 30 |
metricdata.Sum[float64] | |
| 31 |
metricdata.Sum[int64] | |
| 32 |
metricdata.Exemplar[float64] | |
| 33 |
metricdata.Exemplar[int64] | |
| 34 |
metricdata.ExponentialHistogram[float64] | |
| 35 |
metricdata.ExponentialHistogram[int64] | |
| 36 |
metricdata.ExponentialHistogramDataPoint[float64] | |
| 37 |
metricdata.ExponentialHistogramDataPoint[int64] | |
| 38 |
metricdata.ExponentialBucket | |
| 39 |
metricdata.Summary | |
| 40 |
metricdata.SummaryDataPoint | |
| 41 |
metricdata.QuantileValue |
| 42 |
|
| 43 |
// Interface types are not allowed in union types, therefore the |
| 44 |
// Aggregation and Value type from metricdata are not included here. |
| 45 |
} |
| 46 |
|
| 47 |
// TestingT is an interface that implements [testing.T], but without the |
| 48 |
// private method of [testing.TB], so other testing packages can rely on it as |
| 49 |
// well. |
| 50 |
// The methods in this interface must match the [testing.TB] interface. |
| 51 |
type TestingT interface { |
| 52 |
Helper() |
| 53 |
// DO NOT CHANGE: any modification will not be backwards compatible and |
| 54 |
// must never be done outside of a new major release. |
| 55 |
|
| 56 |
Error(...any) |
| 57 |
// DO NOT CHANGE: any modification will not be backwards compatible and |
| 58 |
// must never be done outside of a new major release. |
| 59 |
} |
| 60 |
|
| 61 |
type config struct { |
| 62 |
ignoreTimestamp bool |
| 63 |
ignoreExemplars bool |
| 64 |
ignoreValue bool |
| 65 |
} |
| 66 |
|
| 67 |
func newConfig(opts []Option) config { |
| 68 |
var cfg config |
| 69 |
for _, opt := range opts { |
| 70 |
cfg = opt.apply(cfg) |
| 71 |
} |
| 72 |
return cfg |
| 73 |
} |
| 74 |
|
| 75 |
// Option allows for fine grain control over how AssertEqual operates. |
| 76 |
type Option interface { |
| 77 |
apply(cfg config) config |
| 78 |
} |
| 79 |
|
| 80 |
type fnOption func(cfg config) config |
| 81 |
|
| 82 |
func (fn fnOption) apply(cfg config) config { |
| 83 |
return fn(cfg) |
| 84 |
} |
| 85 |
|
| 86 |
// IgnoreTimestamp disables checking if timestamps are different. |
| 87 |
func IgnoreTimestamp() Option { |
| 88 |
return fnOption(func(cfg config) config { |
| 89 |
cfg.ignoreTimestamp = true |
| 90 |
return cfg |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
// IgnoreExemplars disables checking if Exemplars are different. |
| 95 |
func IgnoreExemplars() Option { |
| 96 |
return fnOption(func(cfg config) config { |
| 97 |
cfg.ignoreExemplars = true |
| 98 |
return cfg |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
// IgnoreValue disables checking if values are different. This can be |
| 103 |
// useful for non-deterministic values, like measured durations. |
| 104 |
// |
| 105 |
// This will ignore the value and trace information for Exemplars; |
| 106 |
// the buckets, zero count, scale, sum, max, min, and counts of |
| 107 |
// ExponentialHistogramDataPoints; the buckets, sum, count, max, |
| 108 |
// and min of HistogramDataPoints; the value of DataPoints. |
| 109 |
func IgnoreValue() Option { |
| 110 |
return fnOption(func(cfg config) config { |
| 111 |
cfg.ignoreValue = true |
| 112 |
return cfg |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
// AssertEqual asserts that the two concrete data-types from the metricdata |
| 117 |
// package are equal. |
| 118 |
func AssertEqual[T Datatypes](t TestingT, expected, actual T, opts ...Option) bool { |
| 119 |
t.Helper() |
| 120 |
|
| 121 |
cfg := newConfig(opts) |
| 122 |
|
| 123 |
// Generic types cannot be type asserted. Use an interface instead. |
| 124 |
aIface := any(actual) |
| 125 |
|
| 126 |
var r []string |
| 127 |
switch e := any(expected).(type) { |
| 128 |
case metricdata.Exemplar[int64]: |
| 129 |
r = equalExemplars(e, aIface.(metricdata.Exemplar[int64]), cfg) |
| 130 |
case metricdata.Exemplar[float64]: |
| 131 |
r = equalExemplars(e, aIface.(metricdata.Exemplar[float64]), cfg) |
| 132 |
case metricdata.DataPoint[int64]: |
| 133 |
r = equalDataPoints(e, aIface.(metricdata.DataPoint[int64]), cfg) |
| 134 |
case metricdata.DataPoint[float64]: |
| 135 |
r = equalDataPoints(e, aIface.(metricdata.DataPoint[float64]), cfg) |
| 136 |
case metricdata.Gauge[int64]: |
| 137 |
r = equalGauges(e, aIface.(metricdata.Gauge[int64]), cfg) |
| 138 |
case metricdata.Gauge[float64]: |
| 139 |
r = equalGauges(e, aIface.(metricdata.Gauge[float64]), cfg) |
| 140 |
case metricdata.Histogram[float64]: |
| 141 |
r = equalHistograms(e, aIface.(metricdata.Histogram[float64]), cfg) |
| 142 |
case metricdata.Histogram[int64]: |
| 143 |
r = equalHistograms(e, aIface.(metricdata.Histogram[int64]), cfg) |
| 144 |
case metricdata.HistogramDataPoint[float64]: |
| 145 |
r = equalHistogramDataPoints(e, aIface.(metricdata.HistogramDataPoint[float64]), cfg) |
| 146 |
case metricdata.HistogramDataPoint[int64]: |
| 147 |
r = equalHistogramDataPoints(e, aIface.(metricdata.HistogramDataPoint[int64]), cfg) |
| 148 |
case metricdata.Extrema[int64]: |
| 149 |
r = equalExtrema(e, aIface.(metricdata.Extrema[int64]), cfg) |
| 150 |
case metricdata.Extrema[float64]: |
| 151 |
r = equalExtrema(e, aIface.(metricdata.Extrema[float64]), cfg) |
| 152 |
case metricdata.Metrics: |
| 153 |
r = equalMetrics(e, aIface.(metricdata.Metrics), cfg) |
| 154 |
case metricdata.ResourceMetrics: |
| 155 |
r = equalResourceMetrics(e, aIface.(metricdata.ResourceMetrics), cfg) |
| 156 |
case metricdata.ScopeMetrics: |
| 157 |
r = equalScopeMetrics(e, aIface.(metricdata.ScopeMetrics), cfg) |
| 158 |
case metricdata.Sum[int64]: |
| 159 |
r = equalSums(e, aIface.(metricdata.Sum[int64]), cfg) |
| 160 |
case metricdata.Sum[float64]: |
| 161 |
r = equalSums(e, aIface.(metricdata.Sum[float64]), cfg) |
| 162 |
case metricdata.ExponentialHistogram[float64]: |
| 163 |
r = equalExponentialHistograms(e, aIface.(metricdata.ExponentialHistogram[float64]), cfg) |
| 164 |
case metricdata.ExponentialHistogram[int64]: |
| 165 |
r = equalExponentialHistograms(e, aIface.(metricdata.ExponentialHistogram[int64]), cfg) |
| 166 |
case metricdata.ExponentialHistogramDataPoint[float64]: |
| 167 |
r = equalExponentialHistogramDataPoints(e, aIface.(metricdata.ExponentialHistogramDataPoint[float64]), cfg) |
| 168 |
case metricdata.ExponentialHistogramDataPoint[int64]: |
| 169 |
r = equalExponentialHistogramDataPoints(e, aIface.(metricdata.ExponentialHistogramDataPoint[int64]), cfg) |
| 170 |
case metricdata.ExponentialBucket: |
| 171 |
r = equalExponentialBuckets(e, aIface.(metricdata.ExponentialBucket), cfg) |
| 172 |
case metricdata.Summary: |
| 173 |
r = equalSummary(e, aIface.(metricdata.Summary), cfg) |
| 174 |
case metricdata.SummaryDataPoint: |
| 175 |
r = equalSummaryDataPoint(e, aIface.(metricdata.SummaryDataPoint), cfg) |
| 176 |
case metricdata.QuantileValue: |
| 177 |
r = equalQuantileValue(e, aIface.(metricdata.QuantileValue), cfg) |
| 178 |
default: |
| 179 |
// We control all types passed to this, panic to signal developers |
| 180 |
// early they changed things in an incompatible way. |
| 181 |
panic(fmt.Sprintf("unknown types: %T", expected)) |
| 182 |
} |
| 183 |
|
| 184 |
if len(r) > 0 { |
| 185 |
t.Error(r) |
| 186 |
return false |
| 187 |
} |
| 188 |
return true |
| 189 |
} |
| 190 |
|
| 191 |
// AssertAggregationsEqual asserts that two Aggregations are equal. |
| 192 |
func AssertAggregationsEqual(t TestingT, expected, actual metricdata.Aggregation, opts ...Option) bool { |
| 193 |
t.Helper() |
| 194 |
|
| 195 |
cfg := newConfig(opts) |
| 196 |
if r := equalAggregations(expected, actual, cfg); len(r) > 0 { |
| 197 |
t.Error(r) |
| 198 |
return false |
| 199 |
} |
| 200 |
return true |
| 201 |
} |
| 202 |
|
| 203 |
// AssertHasAttributes asserts that all Datapoints or HistogramDataPoints have all passed attrs. |
| 204 |
func AssertHasAttributes[T Datatypes](t TestingT, actual T, attrs ...attribute.KeyValue) bool { |
| 205 |
t.Helper() |
| 206 |
|
| 207 |
var reasons []string |
| 208 |
|
| 209 |
switch e := any(actual).(type) { |
| 210 |
case metricdata.Exemplar[int64]: |
| 211 |
reasons = hasAttributesExemplars(e, attrs...) |
| 212 |
case metricdata.Exemplar[float64]: |
| 213 |
reasons = hasAttributesExemplars(e, attrs...) |
| 214 |
case metricdata.DataPoint[int64]: |
| 215 |
reasons = hasAttributesDataPoints(e, attrs...) |
| 216 |
case metricdata.DataPoint[float64]: |
| 217 |
reasons = hasAttributesDataPoints(e, attrs...) |
| 218 |
case metricdata.Gauge[int64]: |
| 219 |
reasons = hasAttributesGauge(e, attrs...) |
| 220 |
case metricdata.Gauge[float64]: |
| 221 |
reasons = hasAttributesGauge(e, attrs...) |
| 222 |
case metricdata.Sum[int64]: |
| 223 |
reasons = hasAttributesSum(e, attrs...) |
| 224 |
case metricdata.Sum[float64]: |
| 225 |
reasons = hasAttributesSum(e, attrs...) |
| 226 |
case metricdata.HistogramDataPoint[int64]: |
| 227 |
reasons = hasAttributesHistogramDataPoints(e, attrs...) |
| 228 |
case metricdata.HistogramDataPoint[float64]: |
| 229 |
reasons = hasAttributesHistogramDataPoints(e, attrs...) |
| 230 |
case metricdata.Extrema[int64], metricdata.Extrema[float64]: |
| 231 |
// Nothing to check. |
| 232 |
case metricdata.Histogram[int64]: |
| 233 |
reasons = hasAttributesHistogram(e, attrs...) |
| 234 |
case metricdata.Histogram[float64]: |
| 235 |
reasons = hasAttributesHistogram(e, attrs...) |
| 236 |
case metricdata.Metrics: |
| 237 |
reasons = hasAttributesMetrics(e, attrs...) |
| 238 |
case metricdata.ScopeMetrics: |
| 239 |
reasons = hasAttributesScopeMetrics(e, attrs...) |
| 240 |
case metricdata.ResourceMetrics: |
| 241 |
reasons = hasAttributesResourceMetrics(e, attrs...) |
| 242 |
case metricdata.ExponentialHistogram[int64]: |
| 243 |
reasons = hasAttributesExponentialHistogram(e, attrs...) |
| 244 |
case metricdata.ExponentialHistogram[float64]: |
| 245 |
reasons = hasAttributesExponentialHistogram(e, attrs...) |
| 246 |
case metricdata.ExponentialHistogramDataPoint[int64]: |
| 247 |
reasons = hasAttributesExponentialHistogramDataPoints(e, attrs...) |
| 248 |
case metricdata.ExponentialHistogramDataPoint[float64]: |
| 249 |
reasons = hasAttributesExponentialHistogramDataPoints(e, attrs...) |
| 250 |
case metricdata.ExponentialBucket: |
| 251 |
// Nothing to check. |
| 252 |
case metricdata.Summary: |
| 253 |
reasons = hasAttributesSummary(e, attrs...) |
| 254 |
case metricdata.SummaryDataPoint: |
| 255 |
reasons = hasAttributesSummaryDataPoint(e, attrs...) |
| 256 |
case metricdata.QuantileValue: |
| 257 |
// Nothing to check. |
| 258 |
default: |
| 259 |
// We control all types passed to this, panic to signal developers |
| 260 |
// early they changed things in an incompatible way. |
| 261 |
panic(fmt.Sprintf("unknown types: %T", actual)) |
| 262 |
} |
| 263 |
|
| 264 |
if len(reasons) > 0 { |
| 265 |
t.Error(reasons) |
| 266 |
return false |
| 267 |
} |
| 268 |
|
| 269 |
return true |
| 270 |
} |