| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/go-logr/logr" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
"github.com/stretchr/testify/suite" |
| 17 |
|
| 18 |
"go.opentelemetry.io/otel" |
| 19 |
"go.opentelemetry.io/otel/attribute" |
| 20 |
"go.opentelemetry.io/otel/internal/global" |
| 21 |
"go.opentelemetry.io/otel/metric" |
| 22 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 23 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 24 |
"go.opentelemetry.io/otel/sdk/resource" |
| 25 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 26 |
) |
| 27 |
|
| 28 |
func TestManualReader(t *testing.T) { |
| 29 |
suite.Run(t, &readerTestSuite{Factory: func(opts ...ReaderOption) Reader { |
| 30 |
var mopts []ManualReaderOption |
| 31 |
for _, o := range opts { |
| 32 |
mopts = append(mopts, o) |
| 33 |
} |
| 34 |
return NewManualReader(mopts...) |
| 35 |
}}) |
| 36 |
} |
| 37 |
|
| 38 |
func BenchmarkManualReader(b *testing.B) { |
| 39 |
b.Run("Collect", benchReaderCollectFunc(NewManualReader())) |
| 40 |
} |
| 41 |
|
| 42 |
var ( |
| 43 |
deltaTemporalitySelector = func(InstrumentKind) metricdata.Temporality { return metricdata.DeltaTemporality } |
| 44 |
cumulativeTemporalitySelector = func(InstrumentKind) metricdata.Temporality { return metricdata.CumulativeTemporality } |
| 45 |
) |
| 46 |
|
| 47 |
func TestManualReaderTemporality(t *testing.T) { |
| 48 |
tests := []struct { |
| 49 |
name string |
| 50 |
options []ManualReaderOption |
| 51 |
// Currently only testing constant temporality. This should be expanded |
| 52 |
// if we put more advanced selection in the SDK |
| 53 |
wantTemporality metricdata.Temporality |
| 54 |
}{ |
| 55 |
{ |
| 56 |
name: "default", |
| 57 |
wantTemporality: metricdata.CumulativeTemporality, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "delta", |
| 61 |
options: []ManualReaderOption{ |
| 62 |
WithTemporalitySelector(deltaTemporalitySelector), |
| 63 |
}, |
| 64 |
wantTemporality: metricdata.DeltaTemporality, |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "repeats overwrite", |
| 68 |
options: []ManualReaderOption{ |
| 69 |
WithTemporalitySelector(deltaTemporalitySelector), |
| 70 |
WithTemporalitySelector(cumulativeTemporalitySelector), |
| 71 |
}, |
| 72 |
wantTemporality: metricdata.CumulativeTemporality, |
| 73 |
}, |
| 74 |
} |
| 75 |
|
| 76 |
for _, tt := range tests { |
| 77 |
t.Run(tt.name, func(t *testing.T) { |
| 78 |
var undefinedInstrument InstrumentKind |
| 79 |
rdr := NewManualReader(tt.options...) |
| 80 |
assert.Equal(t, tt.wantTemporality, rdr.temporality(undefinedInstrument)) |
| 81 |
}) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestManualReaderCollect(t *testing.T) { |
| 86 |
expiredCtx, cancel := context.WithDeadline(t.Context(), time.Now().Add(-1)) |
| 87 |
defer cancel() |
| 88 |
|
| 89 |
tests := []struct { |
| 90 |
name string |
| 91 |
ctx context.Context |
| 92 |
expectedErr error |
| 93 |
}{ |
| 94 |
{ |
| 95 |
name: "with a valid context", |
| 96 |
ctx: t.Context(), |
| 97 |
expectedErr: nil, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "with an expired context", |
| 101 |
ctx: expiredCtx, |
| 102 |
expectedErr: context.DeadlineExceeded, |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
for _, tt := range tests { |
| 107 |
t.Run(tt.name, func(t *testing.T) { |
| 108 |
rdr := NewManualReader() |
| 109 |
mp := NewMeterProvider(WithReader(rdr)) |
| 110 |
meter := mp.Meter("test") |
| 111 |
|
| 112 |
// Ensure the pipeline has a callback setup |
| 113 |
testM, err := meter.Int64ObservableCounter("test") |
| 114 |
assert.NoError(t, err) |
| 115 |
_, err = meter.RegisterCallback(func(context.Context, metric.Observer) error { |
| 116 |
return nil |
| 117 |
}, testM) |
| 118 |
assert.NoError(t, err) |
| 119 |
|
| 120 |
rm := &metricdata.ResourceMetrics{} |
| 121 |
assert.Equal(t, tt.expectedErr, rdr.Collect(tt.ctx, rm)) |
| 122 |
}) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestManualReaderInstrumentation(t *testing.T) { |
| 127 |
// Enable SDK observability. |
| 128 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 129 |
|
| 130 |
// ManualReader under test with a fake producer. |
| 131 |
manualReader := NewManualReader() |
| 132 |
t.Cleanup(func() { _ = manualReader.Shutdown(t.Context()) }) |
| 133 |
manualReader.register(testSDKProducer{}) |
| 134 |
|
| 135 |
// Exercise a collect (producer data). |
| 136 |
var got metricdata.ResourceMetrics |
| 137 |
require.NoError(t, manualReader.Collect(t.Context(), &got)) |
| 138 |
|
| 139 |
// Collect again so we have something to scan through. |
| 140 |
var rm metricdata.ResourceMetrics |
| 141 |
require.NoError(t, manualReader.Collect(t.Context(), &rm)) |
| 142 |
require.NotEmpty(t, rm.ScopeMetrics) |
| 143 |
|
| 144 |
targetName := otelconv.SDKMetricReaderCollectionDuration{}.Name() |
| 145 |
targetDesc := otelconv.SDKMetricReaderCollectionDuration{}.Description() |
| 146 |
targetUnit := otelconv.SDKMetricReaderCollectionDuration{}.Unit() |
| 147 |
|
| 148 |
// Find the SDK reader self-metric anywhere in the collected data. |
| 149 |
foundMetric := findMetricByName(&rm, targetName) |
| 150 |
|
| 151 |
// If not found, explain and skip (this metric is emitted via the *global* MP). |
| 152 |
if foundMetric == nil { |
| 153 |
t.Skipf("SDK reader self-metric %q not found. It is emitted via the global MeterProvider; "+ |
| 154 |
"this test does not install a global MP.", targetName) |
| 155 |
return |
| 156 |
} |
| 157 |
|
| 158 |
// Basic identity checks. |
| 159 |
assert.Equal(t, targetName, foundMetric.Name) |
| 160 |
assert.Equal(t, targetDesc, foundMetric.Description) |
| 161 |
assert.Equal(t, targetUnit, foundMetric.Unit) |
| 162 |
|
| 163 |
// Must be a histogram with cumulative temporality. |
| 164 |
hist, ok := foundMetric.Data.(metricdata.Histogram[float64]) |
| 165 |
require.True(t, ok, "expected histogram data") |
| 166 |
assert.Equal(t, metricdata.CumulativeTemporality, hist.Temporality) |
| 167 |
require.NotEmpty(t, hist.DataPoints) |
| 168 |
|
| 169 |
// Check base attributes on one datapoint (flexibly). |
| 170 |
dp := hist.DataPoints[0] |
| 171 |
attrs := dp.Attributes.ToSlice() |
| 172 |
t.Logf("observability attrs: %v", attrs) |
| 173 |
|
| 174 |
const expectedComponentType = "go.opentelemetry.io/otel/sdk/metric/metric.ManualReader" |
| 175 |
|
| 176 |
var hasName, hasType bool |
| 177 |
for _, a := range attrs { |
| 178 |
switch a.Key { |
| 179 |
case "otel.component.name": |
| 180 |
if s := a.Value.AsString(); s != "" && strings.Contains(s, "metric_reader") { |
| 181 |
hasName = true |
| 182 |
} |
| 183 |
case "otel.component.type": |
| 184 |
if a.Value.AsString() == expectedComponentType { |
| 185 |
hasType = true |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
assert.True(t, hasName, "expected non-empty otel.component.name containing 'metric_reader'") |
| 190 |
assert.True(t, hasType, "expected otel.component.type == %q", expectedComponentType) |
| 191 |
} |
| 192 |
|
| 193 |
// findMetricByName searches all scopes/metrics for the given metric name. |
| 194 |
func findMetricByName(rm *metricdata.ResourceMetrics, name string) *metricdata.Metrics { |
| 195 |
for si := range rm.ScopeMetrics { |
| 196 |
sm := &rm.ScopeMetrics[si] |
| 197 |
for mi := range sm.Metrics { |
| 198 |
if sm.Metrics[mi].Name == name { |
| 199 |
return &sm.Metrics[mi] |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
return nil |
| 204 |
} |
| 205 |
|
| 206 |
// createMetricDataTestProducerForManual creates a producer using patterns from metricdatatest for manual reader benchmarks. |
| 207 |
func createMetricDataTestProducerForManual() testSDKProducer { |
| 208 |
return testSDKProducer{ |
| 209 |
produceFunc: func(_ context.Context, rm *metricdata.ResourceMetrics) error { |
| 210 |
start := time.Now().Add(-time.Minute) |
| 211 |
end := time.Now() |
| 212 |
|
| 213 |
// Create attribute sets using common patterns |
| 214 |
aliceAttrs := attribute.NewSet(attribute.String("user", "alice"), attribute.String("env", "prod")) |
| 215 |
bobAttrs := attribute.NewSet(attribute.String("user", "bob"), attribute.String("env", "staging")) |
| 216 |
charlieAttrs := attribute.NewSet(attribute.String("user", "charlie"), attribute.String("env", "dev")) |
| 217 |
|
| 218 |
// Create exemplars for histogram metrics |
| 219 |
exemplars := []metricdata.Exemplar[float64]{ |
| 220 |
{ |
| 221 |
FilteredAttributes: []attribute.KeyValue{attribute.String("trace", "span-1")}, |
| 222 |
Time: end, |
| 223 |
Value: 15.5, |
| 224 |
SpanID: []byte{1, 2, 3, 4, 5, 6, 7, 8}, |
| 225 |
TraceID: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, |
| 226 |
}, |
| 227 |
} |
| 228 |
|
| 229 |
// Define different metric types using metricdatatest patterns |
| 230 |
createScopeMetrics := func(scopeIdx int) metricdata.ScopeMetrics { |
| 231 |
metrics := []metricdata.Metrics{ |
| 232 |
// Counter metrics |
| 233 |
{ |
| 234 |
Name: fmt.Sprintf("requests_total_%d", scopeIdx), |
| 235 |
Description: "Total number of requests", |
| 236 |
Unit: "1", |
| 237 |
Data: metricdata.Sum[int64]{ |
| 238 |
Temporality: metricdata.CumulativeTemporality, |
| 239 |
IsMonotonic: true, |
| 240 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 241 |
{Attributes: aliceAttrs, StartTime: start, Time: end, Value: 100 + int64(scopeIdx*10)}, |
| 242 |
{Attributes: bobAttrs, StartTime: start, Time: end, Value: 150 + int64(scopeIdx*15)}, |
| 243 |
{Attributes: charlieAttrs, StartTime: start, Time: end, Value: 75 + int64(scopeIdx*5)}, |
| 244 |
}, |
| 245 |
}, |
| 246 |
}, |
| 247 |
// Gauge metrics |
| 248 |
{ |
| 249 |
Name: fmt.Sprintf("memory_usage_%d", scopeIdx), |
| 250 |
Description: "Memory usage in MB", |
| 251 |
Unit: "MB", |
| 252 |
Data: metricdata.Gauge[float64]{ |
| 253 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 254 |
{Attributes: aliceAttrs, Time: end, Value: 512.5 + float64(scopeIdx*10)}, |
| 255 |
{Attributes: bobAttrs, Time: end, Value: 768.2 + float64(scopeIdx*20)}, |
| 256 |
{Attributes: charlieAttrs, Time: end, Value: 256.8 + float64(scopeIdx*5)}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
}, |
| 260 |
// Histogram metrics |
| 261 |
{ |
| 262 |
Name: fmt.Sprintf("request_duration_%d", scopeIdx), |
| 263 |
Description: "Request duration histogram", |
| 264 |
Unit: "ms", |
| 265 |
Data: metricdata.Histogram[float64]{ |
| 266 |
Temporality: metricdata.CumulativeTemporality, |
| 267 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 268 |
{ |
| 269 |
Attributes: aliceAttrs, |
| 270 |
StartTime: start, |
| 271 |
Time: end, |
| 272 |
Count: 100, |
| 273 |
Sum: 1500.5, |
| 274 |
Min: metricdata.NewExtrema(1.0), |
| 275 |
Max: metricdata.NewExtrema(50.0), |
| 276 |
Bounds: []float64{1, 5, 10, 25, 50, 100, 250, 500}, |
| 277 |
BucketCounts: []uint64{10, 20, 30, 25, 10, 4, 1, 0, 0}, |
| 278 |
Exemplars: exemplars, |
| 279 |
}, |
| 280 |
{ |
| 281 |
Attributes: bobAttrs, |
| 282 |
StartTime: start, |
| 283 |
Time: end, |
| 284 |
Count: 80, |
| 285 |
Sum: 1200.3, |
| 286 |
Min: metricdata.NewExtrema(2.0), |
| 287 |
Max: metricdata.NewExtrema(45.0), |
| 288 |
Bounds: []float64{1, 5, 10, 25, 50, 100, 250, 500}, |
| 289 |
BucketCounts: []uint64{5, 15, 25, 20, 10, 4, 1, 0, 0}, |
| 290 |
Exemplars: exemplars, |
| 291 |
}, |
| 292 |
}, |
| 293 |
}, |
| 294 |
}, |
| 295 |
// Exponential Histogram |
| 296 |
{ |
| 297 |
Name: fmt.Sprintf("response_size_%d", scopeIdx), |
| 298 |
Description: "Response size exponential histogram", |
| 299 |
Unit: "bytes", |
| 300 |
Data: metricdata.ExponentialHistogram[float64]{ |
| 301 |
Temporality: metricdata.CumulativeTemporality, |
| 302 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{ |
| 303 |
{ |
| 304 |
Attributes: aliceAttrs, |
| 305 |
StartTime: start, |
| 306 |
Time: end, |
| 307 |
Count: 50, |
| 308 |
Sum: 25000.0, |
| 309 |
Min: metricdata.NewExtrema(100.0), |
| 310 |
Max: metricdata.NewExtrema(2000.0), |
| 311 |
Scale: 2, |
| 312 |
ZeroCount: 2, |
| 313 |
Exemplars: exemplars, |
| 314 |
}, |
| 315 |
}, |
| 316 |
}, |
| 317 |
}, |
| 318 |
} |
| 319 |
|
| 320 |
return metricdata.ScopeMetrics{ |
| 321 |
Scope: instrumentation.Scope{ |
| 322 |
Name: fmt.Sprintf("benchmark/scope/%d", scopeIdx), |
| 323 |
Version: "1.0.0", |
| 324 |
}, |
| 325 |
Metrics: metrics, |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// Create multiple scopes for comprehensive test data |
| 330 |
var scopeMetrics []metricdata.ScopeMetrics |
| 331 |
for i := range 20 { // 20 scopes with 4 metrics each = 80 total metrics |
| 332 |
scopeMetrics = append(scopeMetrics, createScopeMetrics(i)) |
| 333 |
} |
| 334 |
|
| 335 |
*rm = metricdata.ResourceMetrics{ |
| 336 |
Resource: resource.NewSchemaless(attribute.String("service.name", "benchmark-test")), |
| 337 |
ScopeMetrics: scopeMetrics, |
| 338 |
} |
| 339 |
return nil |
| 340 |
}, |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
func BenchmarkManualReaderInstrumentation(b *testing.B) { |
| 345 |
run := func(b *testing.B, withInstrumentationMP bool) { |
| 346 |
// Save and restore the original global meter provider |
| 347 |
orig := otel.GetMeterProvider() |
| 348 |
defer otel.SetMeterProvider(orig) |
| 349 |
|
| 350 |
// Suppress internal logging messages for cleaner benchmark output |
| 351 |
global.SetLogger(logr.Discard()) |
| 352 |
b.Cleanup(func() { |
| 353 |
// Logger will be reset by test cleanup naturally |
| 354 |
}) |
| 355 |
|
| 356 |
// Suppress error handler messages for cleaner benchmark output |
| 357 |
origErrorHandler := otel.GetErrorHandler() |
| 358 |
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(error) {})) |
| 359 |
b.Cleanup(func() { |
| 360 |
otel.SetErrorHandler(origErrorHandler) |
| 361 |
}) |
| 362 |
|
| 363 |
if withInstrumentationMP { |
| 364 |
// Set up a meter provider for instrumentation to use |
| 365 |
instrumentationReader := NewManualReader() |
| 366 |
instrumentationMP := NewMeterProvider(WithReader(instrumentationReader)) |
| 367 |
otel.SetMeterProvider(instrumentationMP) |
| 368 |
|
| 369 |
// Clean up the instrumentation meter provider |
| 370 |
b.Cleanup(func() { |
| 371 |
_ = instrumentationMP.Shutdown(b.Context()) |
| 372 |
}) |
| 373 |
} |
| 374 |
|
| 375 |
r := NewManualReader() |
| 376 |
// Register with producer using metricdatatest patterns for realistic benchmark data |
| 377 |
r.register(createMetricDataTestProducerForManual()) |
| 378 |
b.Cleanup(func() { |
| 379 |
_ = r.Shutdown(b.Context()) // Ignore error in cleanup |
| 380 |
}) |
| 381 |
|
| 382 |
rm := &metricdata.ResourceMetrics{} |
| 383 |
|
| 384 |
b.ReportAllocs() |
| 385 |
b.ResetTimer() |
| 386 |
|
| 387 |
for b.Loop() { |
| 388 |
// Test the collect operation (simulating what manual readers do) |
| 389 |
err := r.Collect(b.Context(), rm) |
| 390 |
_ = err // Ignore error for benchmark |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
b.Run("NoObservability", func(b *testing.B) { |
| 395 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "false") |
| 396 |
run(b, false) |
| 397 |
}) |
| 398 |
|
| 399 |
b.Run("Observability", func(b *testing.B) { |
| 400 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 401 |
run(b, true) |
| 402 |
}) |
| 403 |
} |