oteltesting
import oteltesting "github.com/foomo/opentelemetry-go/testing"Test helpers for wiring OpenTelemetry providers with automatic cleanup. Part of the root module.
Functions
ReportTraces
func ReportTraces(tb testing.TB, exporter trace.SpanExporter) *trace.TracerProviderCreates a TracerProvider with a BatchSpanProcessor configured to never auto-flush (1-hour batch timeout). Registers a tb.Cleanup handler that calls tp.Shutdown, flushing all buffered spans through the exporter.
Parameters:
tb-- the test instance (*testing.T,*testing.B, etc.)exporter-- a span exporter (e.g., fromglossytrace.NewTest)
Returns: a configured *trace.TracerProvider ready to create tracers
func TestMyHandler(t *testing.T) {
exporter := glossytrace.NewTest(t, glossytrace.WithFlamegraph())
tp := oteltesting.ReportTraces(t, exporter)
tracer := tp.Tracer("test")
ctx, span := tracer.Start(context.Background(), "handler")
// ... test code ...
span.End()
}ReportMetrics
func ReportMetrics(tb testing.TB, exporter metric.Exporter) *metric.MeterProviderCreates a MeterProvider backed by a ManualReader. On tb.Cleanup, collects all metrics via reader.Collect, exports them through the exporter, and shuts down the provider.
Parameters:
tb-- the test instanceexporter-- a metric exporter (e.g., fromglossymetric.NewTest)
Returns: a configured *metric.MeterProvider ready to create meters
func TestMyMetrics(t *testing.T) {
exporter := glossymetric.NewTest(t)
mp := oteltesting.ReportMetrics(t, exporter)
meter := mp.Meter("test")
counter, _ := meter.Int64Counter("requests")
counter.Add(context.Background(), 1)
}TestMainReportMetrics
func TestMainReportMetrics(m *testing.M, exporter metric.Exporter) (*metric.MeterProvider, func())Sets up a MeterProvider with a ManualReader for use in TestMain. Returns the provider and a flush function that must be called manually after m.Run().
The flush function:
- Calls
reader.Collectto gather all recorded metrics - Exports them through the exporter
- Calls
mp.Shutdown
Parameters:
m-- the test main instanceexporter-- a metric exporter (e.g., fromglossymetric.NewTestMain)
Returns: (*metric.MeterProvider, func())
func TestMain(m *testing.M) {
exporter := glossymetric.NewTestMain(m)
mp, flush := oteltesting.TestMainReportMetrics(m, exporter)
// Use mp to create meters for your test suite
_ = mp
code := m.Run()
flush()
os.Exit(code)
}