Debian Codegraph @

sdk/metric/periodic_reader_test.go

raw
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 const testDur = time.Second * 2
29
30 func TestWithTimeout(t *testing.T) {
31 test := func(d time.Duration) time.Duration {
32 opts := []PeriodicReaderOption{WithTimeout(d)}
33 return newPeriodicReaderConfig(opts).timeout
34 }
35
36 assert.Equal(t, testDur, test(testDur))
37 assert.Equal(t, defaultTimeout, newPeriodicReaderConfig(nil).timeout)
38 assert.Equal(t, defaultTimeout, test(time.Duration(0)), "invalid timeout should use default")
39 assert.Equal(t, defaultTimeout, test(time.Duration(-1)), "invalid timeout should use default")
40 }
41
42 func TestTimeoutEnvVar(t *testing.T) {
43 testCases := []struct {
44 v string
45 want time.Duration
46 }{
47 {
48 // empty value
49 "",
50 defaultTimeout,
51 },
52 {
53 // positive value
54 "1",
55 time.Millisecond,
56 },
57 {
58 // non-positive value
59 "0",
60 defaultTimeout,
61 },
62 {
63 // value with unit (not supported)
64 "1ms",
65 defaultTimeout,
66 },
67 {
68 // NaN
69 "abc",
70 defaultTimeout,
71 },
72 }
73 for _, tc := range testCases {
74 t.Run(tc.v, func(t *testing.T) {
75 t.Setenv(envTimeout, tc.v)
76 got := newPeriodicReaderConfig(nil).timeout
77 assert.Equal(t, tc.want, got)
78 })
79 }
80 }
81
82 func TestTimeoutEnvAndOption(t *testing.T) {
83 want := 5 * time.Millisecond
84 t.Setenv(envTimeout, "999")
85 opts := []PeriodicReaderOption{WithTimeout(want)}
86 got := newPeriodicReaderConfig(opts).timeout
87 assert.Equal(t, want, got, "option should have precedence over env var")
88 }
89
90 func TestWithInterval(t *testing.T) {
91 test := func(d time.Duration) time.Duration {
92 opts := []PeriodicReaderOption{WithInterval(d)}
93 return newPeriodicReaderConfig(opts).interval
94 }
95
96 assert.Equal(t, testDur, test(testDur))
97 assert.Equal(t, defaultInterval, newPeriodicReaderConfig(nil).interval)
98 assert.Equal(t, defaultInterval, test(time.Duration(0)), "invalid interval should use default")
99 assert.Equal(t, defaultInterval, test(time.Duration(-1)), "invalid interval should use default")
100 }
101
102 func TestIntervalEnvVar(t *testing.T) {
103 testCases := []struct {
104 v string
105 want time.Duration
106 }{
107 {
108 // empty value
109 "",
110 defaultInterval,
111 },
112 {
113 // positive value
114 "1",
115 time.Millisecond,
116 },
117 {
118 // non-positive value
119 "0",
120 defaultInterval,
121 },
122 {
123 // value with unit (not supported)
124 "1ms",
125 defaultInterval,
126 },
127 {
128 // NaN
129 "abc",
130 defaultInterval,
131 },
132 }
133 for _, tc := range testCases {
134 t.Run(tc.v, func(t *testing.T) {
135 t.Setenv(envInterval, tc.v)
136 got := newPeriodicReaderConfig(nil).interval
137 assert.Equal(t, tc.want, got)
138 })
139 }
140 }
141
142 func TestIntervalEnvAndOption(t *testing.T) {
143 want := 5 * time.Millisecond
144 t.Setenv(envInterval, "999")
145 opts := []PeriodicReaderOption{WithInterval(want)}
146 got := newPeriodicReaderConfig(opts).interval
147 assert.Equal(t, want, got, "option should have precedence over env var")
148 }
149
150 type fnExporter struct {
151 temporalityFunc TemporalitySelector
152 aggregationFunc AggregationSelector
153 exportFunc func(context.Context, *metricdata.ResourceMetrics) error
154 flushFunc func(context.Context) error
155 shutdownFunc func(context.Context) error
156 }
157
158 var _ Exporter = (*fnExporter)(nil)
159
160 func (e *fnExporter) Temporality(k InstrumentKind) metricdata.Temporality {
161 if e.temporalityFunc != nil {
162 return e.temporalityFunc(k)
163 }
164 return DefaultTemporalitySelector(k)
165 }
166
167 func (e *fnExporter) Aggregation(k InstrumentKind) Aggregation {
168 if e.aggregationFunc != nil {
169 return e.aggregationFunc(k)
170 }
171 return DefaultAggregationSelector(k)
172 }
173
174 func (e *fnExporter) Export(ctx context.Context, m *metricdata.ResourceMetrics) error {
175 if e.exportFunc != nil {
176 return e.exportFunc(ctx, m)
177 }
178 return nil
179 }
180
181 func (e *fnExporter) ForceFlush(ctx context.Context) error {
182 if e.flushFunc != nil {
183 return e.flushFunc(ctx)
184 }
185 return nil
186 }
187
188 func (e *fnExporter) Shutdown(ctx context.Context) error {
189 if e.shutdownFunc != nil {
190 return e.shutdownFunc(ctx)
191 }
192 return nil
193 }
194
195 type periodicReaderTestSuite struct {
196 *readerTestSuite
197
198 ErrReader *PeriodicReader
199 }
200
201 func (ts *periodicReaderTestSuite) SetupTest() {
202 e := &fnExporter{
203 exportFunc: func(context.Context, *metricdata.ResourceMetrics) error { return assert.AnError },
204 flushFunc: func(context.Context) error { return assert.AnError },
205 shutdownFunc: func(context.Context) error { return assert.AnError },
206 }
207
208 ts.ErrReader = NewPeriodicReader(e, WithProducer(testExternalProducer{}))
209 ts.ErrReader.register(testSDKProducer{})
210 }
211
212 func (ts *periodicReaderTestSuite) TearDownTest() {
213 ts.readerTestSuite.TearDownTest()
214
215 _ = ts.ErrReader.Shutdown(context.Background())
216 }
217
218 func (ts *periodicReaderTestSuite) TestForceFlushPropagated() {
219 ts.Equal(assert.AnError, ts.ErrReader.ForceFlush(context.Background()))
220 }
221
222 func (ts *periodicReaderTestSuite) TestShutdownPropagated() {
223 ts.Equal(assert.AnError, ts.ErrReader.Shutdown(context.Background()))
224 }
225
226 func TestPeriodicReader(t *testing.T) {
227 suite.Run(t, &periodicReaderTestSuite{
228 readerTestSuite: &readerTestSuite{
229 Factory: func(opts ...ReaderOption) Reader {
230 var popts []PeriodicReaderOption
231 for _, o := range opts {
232 popts = append(popts, o)
233 }
234 return NewPeriodicReader(new(fnExporter), popts...)
235 },
236 },
237 })
238 }
239
240 type chErrorHandler struct {
241 Err chan error
242 }
243
244 func newChErrorHandler() *chErrorHandler {
245 return &chErrorHandler{
246 Err: make(chan error, 1),
247 }
248 }
249
250 func (eh chErrorHandler) Handle(err error) {
251 eh.Err <- err
252 }
253
254 func triggerTicker(t *testing.T) chan time.Time {
255 t.Helper()
256
257 // Override the ticker C chan so tests are not flaky and rely on timing.
258 orig := newTicker
259 t.Cleanup(func() { newTicker = orig })
260
261 // Keep this at size zero so when triggered with a send it will hang until
262 // the select case is selected and the collection loop is started.
263 trigger := make(chan time.Time)
264 newTicker = func(d time.Duration) *time.Ticker {
265 ticker := time.NewTicker(d)
266 ticker.C = trigger
267 return ticker
268 }
269 return trigger
270 }
271
272 func TestPeriodicReaderRun(t *testing.T) {
273 trigger := triggerTicker(t)
274
275 // Register an error handler to validate export errors are passed to
276 // otel.Handle.
277 defer func(orig otel.ErrorHandler) {
278 otel.SetErrorHandler(orig)
279 }(otel.GetErrorHandler())
280 eh := newChErrorHandler()
281 otel.SetErrorHandler(eh)
282
283 exp := &fnExporter{
284 exportFunc: func(_ context.Context, m *metricdata.ResourceMetrics) error {
285 // The testSDKProducer produces testResourceMetricsAB.
286 assert.Equal(t, testResourceMetricsAB, *m)
287 return assert.AnError
288 },
289 }
290
291 r := NewPeriodicReader(exp, WithProducer(testExternalProducer{}))
292 r.register(testSDKProducer{})
293 trigger <- time.Now()
294 assert.Equal(t, assert.AnError, <-eh.Err)
295
296 // Ensure Reader is allowed clean up attempt.
297 _ = r.Shutdown(t.Context())
298 }
299
300 func TestPeriodicReaderFlushesPending(t *testing.T) {
301 // Override the ticker so tests are not flaky and rely on timing.
302 trigger := triggerTicker(t)
303 t.Cleanup(func() { close(trigger) })
304
305 expFunc := func(t *testing.T) (exp Exporter, called *bool) {
306 called = new(bool)
307 return &fnExporter{
308 exportFunc: func(_ context.Context, m *metricdata.ResourceMetrics) error {
309 // The testSDKProducer produces testResourceMetricsA.
310 assert.Equal(t, testResourceMetricsAB, *m)
311 *called = true
312 return assert.AnError
313 },
314 }, called
315 }
316
317 t.Run("ForceFlush", func(t *testing.T) {
318 exp, called := expFunc(t)
319 r := NewPeriodicReader(exp, WithProducer(testExternalProducer{}))
320 r.register(testSDKProducer{})
321 assert.Equal(t, assert.AnError, r.ForceFlush(t.Context()), "export error not returned")
322 assert.True(t, *called, "exporter Export method not called, pending telemetry not flushed")
323
324 // Ensure Reader is allowed clean up attempt.
325 _ = r.Shutdown(t.Context())
326 })
327
328 t.Run("ForceFlush timeout on producer", func(t *testing.T) {
329 exp, called := expFunc(t)
330 timeout := time.Millisecond
331 r := NewPeriodicReader(exp, WithTimeout(timeout), WithProducer(testExternalProducer{}))
332 r.register(testSDKProducer{
333 produceFunc: func(ctx context.Context, rm *metricdata.ResourceMetrics) error {
334 select {
335 case <-time.After(timeout + time.Second):
336 *rm = testResourceMetricsA
337 case <-ctx.Done():
338 // we timed out before we could collect metrics
339 return ctx.Err()
340 }
341 return nil
342 },
343 })
344 assert.ErrorIs(t, r.ForceFlush(t.Context()), context.DeadlineExceeded)
345 assert.False(t, *called, "exporter Export method called when it should have failed before export")
346
347 // Ensure Reader is allowed clean up attempt.
348 _ = r.Shutdown(t.Context())
349 })
350
351 t.Run("ForceFlush timeout on external producer", func(t *testing.T) {
352 exp, called := expFunc(t)
353 timeout := time.Millisecond
354 r := NewPeriodicReader(exp, WithTimeout(timeout), WithProducer(testExternalProducer{
355 produceFunc: func(ctx context.Context) ([]metricdata.ScopeMetrics, error) {
356 select {
357 case <-time.After(timeout + time.Second):
358 case <-ctx.Done():
359 // we timed out before we could collect metrics
360 return nil, ctx.Err()
361 }
362 return []metricdata.ScopeMetrics{testScopeMetricsA}, nil
363 },
364 }))
365 r.register(testSDKProducer{})
366 assert.ErrorIs(t, r.ForceFlush(t.Context()), context.DeadlineExceeded)
367 assert.False(t, *called, "exporter Export method called when it should have failed before export")
368
369 // Ensure Reader is allowed clean up attempt.
370 _ = r.Shutdown(t.Context())
371 })
372
373 t.Run("Shutdown", func(t *testing.T) {
374 exp, called := expFunc(t)
375 r := NewPeriodicReader(exp, WithProducer(testExternalProducer{}))
376 r.register(testSDKProducer{})
377 assert.Equal(t, assert.AnError, r.Shutdown(t.Context()), "export error not returned")
378 assert.True(t, *called, "exporter Export method not called, pending telemetry not flushed")
379 })
380
381 t.Run("Shutdown timeout on producer", func(t *testing.T) {
382 exp, called := expFunc(t)
383 timeout := time.Millisecond
384 r := NewPeriodicReader(exp, WithTimeout(timeout), WithProducer(testExternalProducer{}))
385 r.register(testSDKProducer{
386 produceFunc: func(ctx context.Context, rm *metricdata.ResourceMetrics) error {
387 select {
388 case <-time.After(timeout + time.Second):
389 *rm = testResourceMetricsA
390 case <-ctx.Done():
391 // we timed out before we could collect metrics
392 return ctx.Err()
393 }
394 return nil
395 },
396 })
397 assert.ErrorIs(t, r.Shutdown(t.Context()), context.DeadlineExceeded)
398 assert.False(t, *called, "exporter Export method called when it should have failed before export")
399 })
400
401 t.Run("Shutdown timeout on external producer", func(t *testing.T) {
402 exp, called := expFunc(t)
403 timeout := time.Millisecond
404 r := NewPeriodicReader(exp, WithTimeout(timeout), WithProducer(testExternalProducer{
405 produceFunc: func(ctx context.Context) ([]metricdata.ScopeMetrics, error) {
406 select {
407 case <-time.After(timeout + time.Second):
408 case <-ctx.Done():
409 // we timed out before we could collect metrics
410 return nil, ctx.Err()
411 }
412 return []metricdata.ScopeMetrics{testScopeMetricsA}, nil
413 },
414 }))
415 r.register(testSDKProducer{})
416 assert.ErrorIs(t, r.Shutdown(t.Context()), context.DeadlineExceeded)
417 assert.False(t, *called, "exporter Export method called when it should have failed before export")
418 })
419 }
420
421 func TestPeriodicReaderMultipleForceFlush(t *testing.T) {
422 ctx := t.Context()
423 r := NewPeriodicReader(new(fnExporter), WithProducer(testExternalProducer{}))
424 r.register(testSDKProducer{})
425 require.NoError(t, r.ForceFlush(ctx))
426 require.NoError(t, r.ForceFlush(ctx))
427 require.NoError(t, r.Shutdown(ctx))
428 }
429
430 func BenchmarkPeriodicReader(b *testing.B) {
431 r := NewPeriodicReader(new(fnExporter))
432 b.Run("Collect", benchReaderCollectFunc(r))
433 require.NoError(b, r.Shutdown(b.Context()))
434 }
435
436 func TestPeriodicReaderTemporality(t *testing.T) {
437 tests := []struct {
438 name string
439 exporter *fnExporter
440 // Currently only testing constant temporality. This should be expanded
441 // if we put more advanced selection in the SDK
442 wantTemporality metricdata.Temporality
443 }{
444 {
445 name: "default",
446 exporter: new(fnExporter),
447 wantTemporality: metricdata.CumulativeTemporality,
448 },
449 {
450 name: "delta",
451 exporter: &fnExporter{temporalityFunc: deltaTemporalitySelector},
452 wantTemporality: metricdata.DeltaTemporality,
453 },
454 {
455 name: "cumulative",
456 exporter: &fnExporter{temporalityFunc: cumulativeTemporalitySelector},
457 wantTemporality: metricdata.CumulativeTemporality,
458 },
459 }
460
461 for _, tt := range tests {
462 t.Run(tt.name, func(t *testing.T) {
463 var undefinedInstrument InstrumentKind
464 rdr := NewPeriodicReader(tt.exporter)
465 assert.Equal(t, tt.wantTemporality.String(), rdr.temporality(undefinedInstrument).String())
466 })
467 }
468 }
469
470 func TestPeriodicReaderCollect(t *testing.T) {
471 expiredCtx, cancel := context.WithDeadline(t.Context(), time.Now().Add(-1))
472 defer cancel()
473
474 tests := []struct {
475 name string
476 ctx context.Context
477 expectedErr error
478 }{
479 {
480 name: "with a valid context",
481 ctx: t.Context(),
482 expectedErr: nil,
483 },
484 {
485 name: "with an expired context",
486 ctx: expiredCtx,
487 expectedErr: context.DeadlineExceeded,
488 },
489 }
490
491 for _, tt := range tests {
492 t.Run(tt.name, func(t *testing.T) {
493 rdr := NewPeriodicReader(new(fnExporter))
494 mp := NewMeterProvider(WithReader(rdr))
495 meter := mp.Meter("test")
496
497 // Ensure the pipeline has a callback setup
498 testM, err := meter.Int64ObservableCounter("test")
499 assert.NoError(t, err)
500 _, err = meter.RegisterCallback(func(context.Context, metric.Observer) error {
501 return nil
502 }, testM)
503 assert.NoError(t, err)
504
505 rm := &metricdata.ResourceMetrics{}
506 assert.Equal(t, tt.expectedErr, rdr.Collect(tt.ctx, rm))
507 })
508 }
509 }
510
511 func TestPeriodicReaderInstrumentation(t *testing.T) {
512 // Enable SDK observability.
513 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
514
515 // Set up a global MeterProvider to collect the instrumentation metrics.
516 // The PeriodicReader's instrumentation emits metrics to the global MeterProvider.
517 orig := otel.GetMeterProvider()
518 t.Cleanup(func() { otel.SetMeterProvider(orig) })
519
520 instrumentationReader := NewManualReader()
521 instrumentationMP := NewMeterProvider(WithReader(instrumentationReader))
522 otel.SetMeterProvider(instrumentationMP)
523 t.Cleanup(func() { _ = instrumentationMP.Shutdown(t.Context()) })
524
525 // Create a periodic reader with an exporter that returns an error on export but not shutdown
526 exp := &fnExporter{
527 exportFunc: func(context.Context, *metricdata.ResourceMetrics) error {
528 return assert.AnError
529 },
530 shutdownFunc: func(context.Context) error {
531 return nil // No error on shutdown
532 },
533 }
534
535 periodicReader := NewPeriodicReader(exp)
536 t.Cleanup(func() { _ = periodicReader.Shutdown(t.Context()) })
537 periodicReader.register(testSDKProducer{})
538
539 // Exercise a collect (producer data).
540 var got metricdata.ResourceMetrics
541 require.NoError(t, periodicReader.Collect(t.Context(), &got))
542
543 // Trigger a collection and export (which exercises the instrumentation)
544 err := periodicReader.ForceFlush(t.Context())
545 assert.Error(t, err, "expected error from exporter")
546
547 // Collect the instrumentation metrics from the global MeterProvider
548 var instrumentationMetrics metricdata.ResourceMetrics
549 require.NoError(t, instrumentationReader.Collect(t.Context(), &instrumentationMetrics))
550
551 targetName := otelconv.SDKMetricReaderCollectionDuration{}.Name()
552 targetDesc := otelconv.SDKMetricReaderCollectionDuration{}.Description()
553 targetUnit := otelconv.SDKMetricReaderCollectionDuration{}.Unit()
554
555 // Find the SDK reader self-metric in the instrumentation metrics.
556 foundMetric := findMetricByName(&instrumentationMetrics, targetName)
557 require.NotNil(t, foundMetric, "SDK reader self-metric %q should be found in instrumentation metrics", targetName)
558
559 // Basic identity checks (don't assert scope name/version; that can vary).
560 assert.Equal(t, targetName, foundMetric.Name)
561 assert.Equal(t, targetDesc, foundMetric.Description)
562 assert.Equal(t, targetUnit, foundMetric.Unit)
563
564 // Must be a histogram with cumulative temporality.
565 hist, ok := foundMetric.Data.(metricdata.Histogram[float64])
566 require.True(t, ok, "expected histogram data")
567 assert.Equal(t, metricdata.CumulativeTemporality, hist.Temporality)
568 require.NotEmpty(t, hist.DataPoints)
569
570 // Check base attributes on one datapoint (flexibly).
571 dp := hist.DataPoints[0]
572 attrs := dp.Attributes.ToSlice()
573 t.Logf("observability attrs: %v", attrs)
574
575 const expectedComponentType = "periodic_metric_reader"
576
577 var hasName, hasType bool
578 for _, a := range attrs {
579 switch a.Key {
580 case "otel.component.name":
581 if s := a.Value.AsString(); s != "" && strings.Contains(s, "metric_reader") {
582 hasName = true
583 }
584 case "otel.component.type":
585 if a.Value.AsString() == expectedComponentType {
586 hasType = true
587 }
588 }
589 }
590 assert.True(t, hasName, "expected non-empty otel.component.name containing 'metric_reader'")
591 assert.True(t, hasType, "expected otel.component.type == %q", expectedComponentType)
592 }
593
594 func TestPeriodicReaderInstrumentationError(t *testing.T) {
595 // Enable SDK observability.
596 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
597
598 // Set up a MeterProvider that returns errors when creating instruments.
599 // This simulates the error path in NewPeriodicReader where observ.NewInstrumentation fails.
600 orig := otel.GetMeterProvider()
601 t.Cleanup(func() { otel.SetErrorHandler(otel.GetErrorHandler()) })
602 t.Cleanup(func() { otel.SetMeterProvider(orig) })
603
604 // Create an error handler to capture the error from otel.Handle()
605 eh := newChErrorHandler()
606 otel.SetErrorHandler(eh)
607
608 // Set up a MeterProvider that returns errors
609 mp := &errMeterProvider{err: assert.AnError}
610 otel.SetMeterProvider(mp)
611
612 // Create a periodic reader - this should trigger the error path
613 exp := &fnExporter{}
614 periodicReader := NewPeriodicReader(exp)
615 t.Cleanup(func() { _ = periodicReader.Shutdown(t.Context()) })
616
617 // Verify that the error was handled via otel.Handle()
618 select {
619 case err := <-eh.Err:
620 assert.Error(t, err, "expected error to be handled")
621 assert.ErrorIs(t, err, assert.AnError, "expected the error from NewInstrumentation")
622 case <-time.After(time.Second):
623 t.Fatal("timeout waiting for error to be handled")
624 }
625
626 // Verify the reader is still functional despite the instrumentation error
627 periodicReader.register(testSDKProducer{})
628 var rm metricdata.ResourceMetrics
629 assert.NoError(t, periodicReader.Collect(t.Context(), &rm), "reader should still work without instrumentation")
630 }
631
632 // errMeterProvider is a test helper that returns errors when creating instruments.
633 type errMeterProvider struct {
634 metric.MeterProvider
635 err error
636 }
637
638 func (m *errMeterProvider) Meter(string, ...metric.MeterOption) metric.Meter {
639 return &errMeter{err: m.err}
640 }
641
642 type errMeter struct {
643 metric.Meter
644 err error
645 }
646
647 func (m *errMeter) Float64Histogram(string, ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
648 return nil, m.err
649 }
650
651 // createMetricDataTestProducer creates a producer using patterns from metricdatatest.
652 func createMetricDataTestProducer() testSDKProducer {
653 return testSDKProducer{
654 produceFunc: func(_ context.Context, rm *metricdata.ResourceMetrics) error {
655 start := time.Now().Add(-time.Minute)
656 end := time.Now()
657
658 // Create attribute sets using common patterns
659 aliceAttrs := attribute.NewSet(attribute.String("user", "alice"), attribute.String("env", "prod"))
660 bobAttrs := attribute.NewSet(attribute.String("user", "bob"), attribute.String("env", "staging"))
661 charlieAttrs := attribute.NewSet(attribute.String("user", "charlie"), attribute.String("env", "dev"))
662
663 // Create exemplars for histogram metrics
664 exemplars := []metricdata.Exemplar[float64]{
665 {
666 FilteredAttributes: []attribute.KeyValue{attribute.String("trace", "span-1")},
667 Time: end,
668 Value: 15.5,
669 SpanID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
670 TraceID: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
671 },
672 }
673
674 // Define different metric types using metricdatatest patterns
675 createScopeMetrics := func(scopeIdx int) metricdata.ScopeMetrics {
676 metrics := []metricdata.Metrics{
677 // Counter metrics
678 {
679 Name: fmt.Sprintf("requests_total_%d", scopeIdx),
680 Description: "Total number of requests",
681 Unit: "1",
682 Data: metricdata.Sum[int64]{
683 Temporality: metricdata.CumulativeTemporality,
684 IsMonotonic: true,
685 DataPoints: []metricdata.DataPoint[int64]{
686 {Attributes: aliceAttrs, StartTime: start, Time: end, Value: 100 + int64(scopeIdx*10)},
687 {Attributes: bobAttrs, StartTime: start, Time: end, Value: 150 + int64(scopeIdx*15)},
688 {Attributes: charlieAttrs, StartTime: start, Time: end, Value: 75 + int64(scopeIdx*5)},
689 },
690 },
691 },
692 // Gauge metrics
693 {
694 Name: fmt.Sprintf("memory_usage_%d", scopeIdx),
695 Description: "Memory usage in MB",
696 Unit: "MB",
697 Data: metricdata.Gauge[float64]{
698 DataPoints: []metricdata.DataPoint[float64]{
699 {Attributes: aliceAttrs, Time: end, Value: 512.5 + float64(scopeIdx*10)},
700 {Attributes: bobAttrs, Time: end, Value: 768.2 + float64(scopeIdx*20)},
701 {Attributes: charlieAttrs, Time: end, Value: 256.8 + float64(scopeIdx*5)},
702 },
703 },
704 },
705 // Histogram metrics
706 {
707 Name: fmt.Sprintf("request_duration_%d", scopeIdx),
708 Description: "Request duration histogram",
709 Unit: "ms",
710 Data: metricdata.Histogram[float64]{
711 Temporality: metricdata.CumulativeTemporality,
712 DataPoints: []metricdata.HistogramDataPoint[float64]{
713 {
714 Attributes: aliceAttrs,
715 StartTime: start,
716 Time: end,
717 Count: 100,
718 Sum: 1500.5,
719 Min: metricdata.NewExtrema(1.0),
720 Max: metricdata.NewExtrema(50.0),
721 Bounds: []float64{1, 5, 10, 25, 50, 100, 250, 500},
722 BucketCounts: []uint64{10, 20, 30, 25, 10, 4, 1, 0, 0},
723 Exemplars: exemplars,
724 },
725 {
726 Attributes: bobAttrs,
727 StartTime: start,
728 Time: end,
729 Count: 80,
730 Sum: 1200.3,
731 Min: metricdata.NewExtrema(2.0),
732 Max: metricdata.NewExtrema(45.0),
733 Bounds: []float64{1, 5, 10, 25, 50, 100, 250, 500},
734 BucketCounts: []uint64{5, 15, 25, 20, 10, 4, 1, 0, 0},
735 Exemplars: exemplars,
736 },
737 },
738 },
739 },
740 // Exponential Histogram
741 {
742 Name: fmt.Sprintf("response_size_%d", scopeIdx),
743 Description: "Response size exponential histogram",
744 Unit: "bytes",
745 Data: metricdata.ExponentialHistogram[float64]{
746 Temporality: metricdata.CumulativeTemporality,
747 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{
748 {
749 Attributes: aliceAttrs,
750 StartTime: start,
751 Time: end,
752 Count: 50,
753 Sum: 25000.0,
754 Min: metricdata.NewExtrema(100.0),
755 Max: metricdata.NewExtrema(2000.0),
756 Scale: 2,
757 ZeroCount: 2,
758 Exemplars: exemplars,
759 },
760 },
761 },
762 },
763 }
764
765 return metricdata.ScopeMetrics{
766 Scope: instrumentation.Scope{
767 Name: fmt.Sprintf("benchmark/scope/%d", scopeIdx),
768 Version: "1.0.0",
769 },
770 Metrics: metrics,
771 }
772 }
773
774 // Create multiple scopes for comprehensive test data
775 var scopeMetrics []metricdata.ScopeMetrics
776 for i := range 20 { // 20 scopes with 4 metrics each = 80 total metrics
777 scopeMetrics = append(scopeMetrics, createScopeMetrics(i))
778 }
779
780 *rm = metricdata.ResourceMetrics{
781 Resource: resource.NewSchemaless(attribute.String("service.name", "benchmark-test")),
782 ScopeMetrics: scopeMetrics,
783 }
784 return nil
785 },
786 }
787 }
788
789 func BenchmarkPeriodicReaderInstrumentation(b *testing.B) {
790 run := func(b *testing.B, withInstrumentationMP bool) {
791 // Save and restore the original global meter provider
792 orig := otel.GetMeterProvider()
793 defer otel.SetMeterProvider(orig)
794
795 // Suppress internal logging messages for cleaner benchmark output
796 global.SetLogger(logr.Discard())
797 b.Cleanup(func() {
798 // Logger will be reset by test cleanup naturally
799 })
800
801 // Suppress error handler messages for cleaner benchmark output
802 origErrorHandler := otel.GetErrorHandler()
803 otel.SetErrorHandler(otel.ErrorHandlerFunc(func(error) {}))
804 b.Cleanup(func() {
805 otel.SetErrorHandler(origErrorHandler)
806 })
807
808 if withInstrumentationMP {
809 // Set up a meter provider for instrumentation to use
810 instrumentationReader := NewManualReader()
811 instrumentationMP := NewMeterProvider(WithReader(instrumentationReader))
812 otel.SetMeterProvider(instrumentationMP)
813
814 // Clean up the instrumentation meter provider
815 b.Cleanup(func() {
816 _ = instrumentationMP.Shutdown(b.Context())
817 })
818 }
819
820 var exportCallCount int64
821 exp := &fnExporter{
822 exportFunc: func(_ context.Context, _ *metricdata.ResourceMetrics) error {
823 // Count exports to ensure they're happening
824 exportCallCount++
825 return nil
826 },
827 shutdownFunc: func(context.Context) error {
828 return nil
829 },
830 }
831
832 r := NewPeriodicReader(exp)
833 // Register with producer using metricdatatest patterns for realistic benchmark data
834 r.register(createMetricDataTestProducer())
835 b.Cleanup(func() {
836 _ = r.Shutdown(b.Context()) // Ignore error in cleanup
837 })
838
839 rm := &metricdata.ResourceMetrics{}
840
841 b.ReportAllocs()
842 b.ResetTimer()
843
844 for b.Loop() {
845 // Test the full collect and export operation (simulating what collectAndExport does)
846 err := r.Collect(b.Context(), rm)
847 if err == nil {
848 err = exp.Export(b.Context(), rm)
849 }
850 _ = err // Ignore error for benchmark
851 }
852
853 b.StopTimer()
854 if exportCallCount == 0 {
855 b.Fatalf("Expected exports to be called, but got 0")
856 }
857 }
858
859 b.Run("NoObservability", func(b *testing.B) {
860 b.Setenv("OTEL_GO_X_OBSERVABILITY", "false")
861 run(b, false)
862 })
863
864 b.Run("Observability", func(b *testing.B) {
865 b.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
866 run(b, true)
867 })
868 }