| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metricdatatest_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/metric" |
| 13 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 14 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 16 |
) |
| 17 |
|
| 18 |
func ExampleAssertEqual() { |
| 19 |
ctx := context.Background() |
| 20 |
|
| 21 |
// Create a meterprovider with a reader |
| 22 |
reader := sdkmetric.NewManualReader() |
| 23 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 24 |
defer func() { |
| 25 |
_ = mp.Shutdown(ctx) |
| 26 |
}() |
| 27 |
|
| 28 |
// Create an instrument(eg: counter/histogram/gauge) and simulate an operation |
| 29 |
meter := mp.Meter("payment-service") |
| 30 |
counter, _ := meter.Int64Counter("payment.requests") |
| 31 |
counter.Add(ctx, 5) |
| 32 |
|
| 33 |
// Collect the metrics |
| 34 |
rm := &metricdata.ResourceMetrics{} |
| 35 |
_ = reader.Collect(ctx, rm) |
| 36 |
got, _ := getMetrics("payment.requests", rm) |
| 37 |
|
| 38 |
want := metricdata.Metrics{ |
| 39 |
Name: "payment.requests", |
| 40 |
Data: metricdata.Sum[int64]{ |
| 41 |
DataPoints: []metricdata.DataPoint[int64]{{Value: 5}}, |
| 42 |
Temporality: metricdata.CumulativeTemporality, |
| 43 |
IsMonotonic: true, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
// Compare expected metrics with the received one |
| 48 |
t := &mockTestingT{} |
| 49 |
assertEqual := metricdatatest.AssertEqual( |
| 50 |
t, |
| 51 |
want, |
| 52 |
got, |
| 53 |
metricdatatest.IgnoreTimestamp(), // ignoring timestamps |
| 54 |
) |
| 55 |
fmt.Printf("Metrics are equal: %t\n", assertEqual) |
| 56 |
|
| 57 |
// Output: |
| 58 |
// Metrics are equal: true |
| 59 |
} |
| 60 |
|
| 61 |
func ExampleAssertAggregationsEqual() { |
| 62 |
ctx := context.Background() |
| 63 |
|
| 64 |
// Create a meterprovider with a reader |
| 65 |
reader := sdkmetric.NewManualReader() |
| 66 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 67 |
defer func() { |
| 68 |
_ = mp.Shutdown(ctx) |
| 69 |
}() |
| 70 |
|
| 71 |
// Create an instrument(eg: counter/histogram/gauge) and simulate an operation |
| 72 |
meter := mp.Meter("payment-service") |
| 73 |
counter, _ := meter.Int64Counter("payment.count") |
| 74 |
counter.Add(ctx, 5) |
| 75 |
|
| 76 |
// Collect the metrics |
| 77 |
rm := &metricdata.ResourceMetrics{} |
| 78 |
_ = reader.Collect(ctx, rm) |
| 79 |
got, _ := getMetrics("payment.count", rm) |
| 80 |
|
| 81 |
want := metricdata.Metrics{ |
| 82 |
Data: metricdata.Sum[int64]{ |
| 83 |
DataPoints: []metricdata.DataPoint[int64]{{Value: 5}}, |
| 84 |
Temporality: metricdata.CumulativeTemporality, |
| 85 |
IsMonotonic: true, |
| 86 |
}, |
| 87 |
} |
| 88 |
|
| 89 |
// Verify the expected data with the received one. |
| 90 |
t := &mockTestingT{} |
| 91 |
|
| 92 |
// Compare Aggregations |
| 93 |
hasEqualAggregations := metricdatatest.AssertAggregationsEqual( |
| 94 |
t, |
| 95 |
want.Data, |
| 96 |
got.Data, |
| 97 |
metricdatatest.IgnoreTimestamp(), |
| 98 |
) |
| 99 |
fmt.Printf("Aggregations are equal: %t\n", hasEqualAggregations) |
| 100 |
|
| 101 |
// Output: |
| 102 |
// Aggregations are equal: true |
| 103 |
} |
| 104 |
|
| 105 |
func ExampleAssertHasAttributes() { |
| 106 |
ctx := context.Background() |
| 107 |
|
| 108 |
// Create a meterprovider with a reader |
| 109 |
reader := sdkmetric.NewManualReader() |
| 110 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 111 |
defer func() { |
| 112 |
_ = mp.Shutdown(ctx) |
| 113 |
}() |
| 114 |
|
| 115 |
// Simulate an operation using an instrument(eg: counter) |
| 116 |
meter := mp.Meter("payment-service") |
| 117 |
counter, _ := meter.Int64Counter("payment.requests") |
| 118 |
|
| 119 |
// Add attribute to the measurement |
| 120 |
attributes := attribute.NewSet(attribute.String("payment.method", "credit_card")) |
| 121 |
counter.Add(ctx, 5, metric.WithAttributeSet(attributes)) |
| 122 |
|
| 123 |
// Collect the metrics |
| 124 |
rm := &metricdata.ResourceMetrics{} |
| 125 |
_ = reader.Collect(ctx, rm) |
| 126 |
metrics, _ := getMetrics("payment.requests", rm) |
| 127 |
|
| 128 |
// Verify the attributes in the received metrics |
| 129 |
t := &mockTestingT{} |
| 130 |
hasAttributes := metricdatatest.AssertHasAttributes(t, metrics, attributes.ToSlice()...) |
| 131 |
fmt.Printf("Metrics contains attributes: %t\n", hasAttributes) |
| 132 |
|
| 133 |
// Output: |
| 134 |
// Metrics contains attributes: true |
| 135 |
} |
| 136 |
|
| 137 |
func ExampleIgnoreValue() { |
| 138 |
want := metricdata.Metrics{ |
| 139 |
Name: "payment.duration", |
| 140 |
Data: metricdata.Histogram[float64]{ |
| 141 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 142 |
{ |
| 143 |
Count: 2, |
| 144 |
Sum: 224.0, |
| 145 |
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000}, |
| 146 |
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, |
| 147 |
}, |
| 148 |
}, |
| 149 |
Temporality: metricdata.CumulativeTemporality, |
| 150 |
}, |
| 151 |
} |
| 152 |
|
| 153 |
got := metricdata.Metrics{ |
| 154 |
Name: "payment.duration", |
| 155 |
Data: metricdata.Histogram[float64]{ |
| 156 |
// Aggregate measurements are different in received metrics |
| 157 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 158 |
{ |
| 159 |
Count: 10, |
| 160 |
Sum: 0, |
| 161 |
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000}, |
| 162 |
BucketCounts: []uint64{1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, |
| 163 |
}, |
| 164 |
}, |
| 165 |
Temporality: metricdata.CumulativeTemporality, |
| 166 |
}, |
| 167 |
} |
| 168 |
|
| 169 |
t := &mockTestingT{} |
| 170 |
|
| 171 |
// Compare metrics without values |
| 172 |
ignoreValue := metricdatatest.AssertEqual( |
| 173 |
t, |
| 174 |
want, |
| 175 |
got, |
| 176 |
metricdatatest.IgnoreValue(), |
| 177 |
) |
| 178 |
fmt.Printf("Metrics are equal(ignoring values): %t\n", ignoreValue) |
| 179 |
|
| 180 |
// Output: |
| 181 |
// Metrics are equal(ignoring values): true |
| 182 |
} |
| 183 |
|
| 184 |
func ExampleIgnoreExemplars() { |
| 185 |
// Histogram data with Exemplars |
| 186 |
want := metricdata.Metrics{ |
| 187 |
Name: "payment.duration", |
| 188 |
Data: metricdata.Histogram[float64]{ |
| 189 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 190 |
{ |
| 191 |
Count: 2, |
| 192 |
Sum: 224.0, |
| 193 |
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000}, |
| 194 |
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, |
| 195 |
Exemplars: []metricdata.Exemplar[float64]{ |
| 196 |
{ |
| 197 |
FilteredAttributes: []attribute.KeyValue{ |
| 198 |
attribute.String("payment.type", "recurring"), |
| 199 |
}, |
| 200 |
Time: time.Now(), |
| 201 |
Value: 15.0, |
| 202 |
SpanID: []byte{1, 2, 3, 4, 5, 6, 7, 8}, |
| 203 |
TraceID: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, |
| 204 |
}, |
| 205 |
}, |
| 206 |
}, |
| 207 |
}, |
| 208 |
Temporality: metricdata.CumulativeTemporality, |
| 209 |
}, |
| 210 |
} |
| 211 |
|
| 212 |
// Histogram data without Exemplars |
| 213 |
got := metricdata.Metrics{ |
| 214 |
Name: "payment.duration", |
| 215 |
Data: metricdata.Histogram[float64]{ |
| 216 |
// Aggregate measurements are different in received metrics(exemplars) |
| 217 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 218 |
{ |
| 219 |
Count: 2, |
| 220 |
Sum: 224.0, |
| 221 |
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000}, |
| 222 |
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, |
| 223 |
}, |
| 224 |
}, |
| 225 |
Temporality: metricdata.CumulativeTemporality, |
| 226 |
}, |
| 227 |
} |
| 228 |
|
| 229 |
t := &mockTestingT{} |
| 230 |
|
| 231 |
// Compare metrics |
| 232 |
ignoreExemplars := metricdatatest.AssertEqual( |
| 233 |
t, |
| 234 |
want, |
| 235 |
got, |
| 236 |
metricdatatest.IgnoreExemplars(), |
| 237 |
) |
| 238 |
fmt.Printf("Metrics are equal(ignoring exemplars): %t\n", ignoreExemplars) |
| 239 |
|
| 240 |
// Output: |
| 241 |
// Metrics are equal(ignoring exemplars): true |
| 242 |
} |
| 243 |
|
| 244 |
// Helper function to retrieve the metrics. |
| 245 |
// nolint:unparam // 'bool' return value currently unused, but retained for completeness |
| 246 |
func getMetrics(name string, rm *metricdata.ResourceMetrics) (metricdata.Metrics, bool) { |
| 247 |
for _, scopeMetrics := range rm.ScopeMetrics { |
| 248 |
for _, m := range scopeMetrics.Metrics { |
| 249 |
if m.Name == name { |
| 250 |
return m, true |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
return metricdata.Metrics{}, false |
| 255 |
} |
| 256 |
|
| 257 |
// mockTestingT implements the [metricdatatest.TestingT] interface for examples. |
| 258 |
// Usually, we use [*testing.T] as a substitute. |
| 259 |
type mockTestingT struct{} |
| 260 |
|
| 261 |
func (*mockTestingT) Helper() {} |
| 262 |
|
| 263 |
func (*mockTestingT) Error(...any) {} |