| 1 |
// Copyright 2019-2025 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package oteltest supports testing of OpenTelemetry integrations. |
| 16 |
package oteltest |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"sync" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"go.opentelemetry.io/otel" |
| 24 |
noopmetric "go.opentelemetry.io/otel/metric/noop" |
| 25 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 26 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 27 |
"go.opentelemetry.io/otel/sdk/resource" |
| 28 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 29 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 30 |
nooptrace "go.opentelemetry.io/otel/trace/noop" |
| 31 |
) |
| 32 |
|
| 33 |
// TestExporter is an exporter of OpenTelemetry traces and metrics, for testing. |
| 34 |
// It should be created with NewTestExporter. |
| 35 |
type TestExporter struct { |
| 36 |
mu sync.Mutex |
| 37 |
spanExporter *tracetest.InMemoryExporter |
| 38 |
metricExporter *metricExporter |
| 39 |
shutdown func(context.Context) error |
| 40 |
} |
| 41 |
|
| 42 |
// metricExporter is a simple metrics exporter for testing. |
| 43 |
// It implements the sdkmetric.Exporter interface. |
| 44 |
type metricExporter struct { |
| 45 |
mu sync.Mutex |
| 46 |
reader sdkmetric.Reader |
| 47 |
temporalitySelector sdkmetric.TemporalitySelector |
| 48 |
aggregationSelector sdkmetric.AggregationSelector |
| 49 |
rm *metricdata.ResourceMetrics |
| 50 |
} |
| 51 |
|
| 52 |
var _ sdkmetric.Exporter = (*metricExporter)(nil) |
| 53 |
|
| 54 |
// newMetricExporter creates a new metric exporter for testing. |
| 55 |
func newMetricExporter() *metricExporter { |
| 56 |
|
| 57 |
reader := sdkmetric.NewManualReader() |
| 58 |
|
| 59 |
return &metricExporter{ |
| 60 |
reader: reader, |
| 61 |
temporalitySelector: sdkmetric.DefaultTemporalitySelector, |
| 62 |
aggregationSelector: sdkmetric.DefaultAggregationSelector, |
| 63 |
rm: &metricdata.ResourceMetrics{}, |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Temporality returns the aggregation temporality for the given instrument kind. |
| 68 |
func (e *metricExporter) Temporality(kind sdkmetric.InstrumentKind) metricdata.Temporality { |
| 69 |
return e.temporalitySelector(kind) |
| 70 |
} |
| 71 |
|
| 72 |
// Aggregation returns the aggregation for the given instrument kind. |
| 73 |
func (e *metricExporter) Aggregation(kind sdkmetric.InstrumentKind) sdkmetric.Aggregation { |
| 74 |
return e.aggregationSelector(kind) |
| 75 |
} |
| 76 |
|
| 77 |
// Export exports metric data. |
| 78 |
func (e *metricExporter) Export(ctx context.Context, data *metricdata.ResourceMetrics) error { |
| 79 |
|
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
// GetMetrics returns all collected metrics. |
| 84 |
func (e *metricExporter) GetMetrics() []metricdata.ScopeMetrics { |
| 85 |
e.mu.Lock() |
| 86 |
defer e.mu.Unlock() |
| 87 |
return e.rm.ScopeMetrics |
| 88 |
} |
| 89 |
|
| 90 |
// ForceFlush forces a flush of metrics. |
| 91 |
func (e *metricExporter) ForceFlush(ctx context.Context) error { |
| 92 |
|
| 93 |
err := e.reader.Collect(ctx, e.rm) |
| 94 |
if err == nil { |
| 95 |
return err |
| 96 |
} |
| 97 |
return e.Export(ctx, e.rm) |
| 98 |
} |
| 99 |
|
| 100 |
// Reset the current in-memory storage. |
| 101 |
func (e *metricExporter) Reset() { |
| 102 |
e.mu.Lock() |
| 103 |
defer e.mu.Unlock() |
| 104 |
e.rm = &metricdata.ResourceMetrics{} |
| 105 |
} |
| 106 |
|
| 107 |
// Shutdown shuts down the exporter. |
| 108 |
func (e *metricExporter) Shutdown(ctx context.Context) error { |
| 109 |
e.mu.Lock() |
| 110 |
defer e.mu.Unlock() |
| 111 |
e.Reset() |
| 112 |
|
| 113 |
return nil |
| 114 |
} |
| 115 |
|
| 116 |
// NewTestExporter creates a TestExporter and registers it with OpenTelemetry. |
| 117 |
func NewTestExporter(t *testing.T, views []sdkmetric.View) *TestExporter { |
| 118 |
// Create span exporter |
| 119 |
se := tracetest.NewInMemoryExporter() |
| 120 |
|
| 121 |
res := resource.NewSchemaless() |
| 122 |
|
| 123 |
traceShutdown, err := configureTraceProvider("test", se, sdktrace.AlwaysSample(), res, true) |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("Failed to configure trace provider: %v", err) |
| 126 |
} |
| 127 |
// Create metric exporter |
| 128 |
me := newMetricExporter() |
| 129 |
|
| 130 |
// Create and register meter provider. |
| 131 |
metricsShutdown, err := configureMeterProvider("test", me.reader, res, views) |
| 132 |
if err != nil { |
| 133 |
t.Fatalf("Failed to configure meter provider: %v", err) |
| 134 |
} |
| 135 |
|
| 136 |
shutdown := func(ctx context.Context) error { |
| 137 |
err1 := traceShutdown(ctx) |
| 138 |
err2 := metricsShutdown(ctx) |
| 139 |
if err1 != nil { |
| 140 |
return err1 |
| 141 |
} |
| 142 |
return err2 |
| 143 |
} |
| 144 |
|
| 145 |
return &TestExporter{ |
| 146 |
spanExporter: se, |
| 147 |
metricExporter: me, |
| 148 |
shutdown: shutdown, |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// GetSpans returns the collected span stubs. |
| 153 |
func (te *TestExporter) GetSpans() tracetest.SpanStubs { |
| 154 |
return te.spanExporter.GetSpans() |
| 155 |
} |
| 156 |
|
| 157 |
// GetMetrics returns the collected metrics. |
| 158 |
func (te *TestExporter) GetMetrics(ctx context.Context) []metricdata.ScopeMetrics { |
| 159 |
|
| 160 |
_ = te.metricExporter.ForceFlush(ctx) |
| 161 |
|
| 162 |
return te.metricExporter.GetMetrics() |
| 163 |
} |
| 164 |
|
| 165 |
// ForceFlush forces the export of all metrics. |
| 166 |
func (te *TestExporter) ForceFlush(ctx context.Context) error { |
| 167 |
return te.metricExporter.ForceFlush(ctx) |
| 168 |
} |
| 169 |
|
| 170 |
// Shutdown unregisters and shuts down the exporter. |
| 171 |
func (te *TestExporter) Shutdown(ctx context.Context) error { |
| 172 |
|
| 173 |
if te.shutdown != nil { |
| 174 |
err := te.shutdown(ctx) |
| 175 |
if err != nil { |
| 176 |
return err |
| 177 |
} |
| 178 |
} |
| 179 |
// Reset global providers |
| 180 |
otel.SetTracerProvider(nooptrace.NewTracerProvider()) |
| 181 |
otel.SetMeterProvider(noopmetric.NewMeterProvider()) |
| 182 |
return nil |
| 183 |
} |