testutil

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package testutil provides in-memory exporters and helpers for testing code that uses metry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupTestMetrics

func SetupTestMetrics(t *testing.T) (*sdkmetric.ManualReader, metric.Meter)

SetupTestMetrics configures the global meter provider with a ManualReader so tests can collect metrics synchronously via reader.Collect. Registers cleanup on t. Returns the reader (for Collect and assertions) and the meter (for creating instruments).

Types

type InMemoryMetricExporter

type InMemoryMetricExporter struct {
	// contains filtered or unexported fields
}

InMemoryMetricExporter stores the count of Export calls and the last ResourceMetrics for test assertions (e.g. lifecycle tests that need to assert on datapoints). Contract: unsupported aggregation types (e.g. Gauge) cause panic in Export (fail-fast by design).

func NewInMemoryMetricExporter

func NewInMemoryMetricExporter() *InMemoryMetricExporter

NewInMemoryMetricExporter returns a new in-memory metric exporter.

func (*InMemoryMetricExporter) Aggregation

Aggregation implements sdkmetric.Exporter.

func (*InMemoryMetricExporter) Export

Export implements sdkmetric.Exporter; it increments the export count and stores a deep copy of the payload. A snapshot is required because the SDK may reuse the same *ResourceMetrics buffer across export cycles. Unsupported aggregation types (e.g. Gauge) cause panic before copy (fail-fast contract).

func (*InMemoryMetricExporter) Exporter added in v0.2.0

Exporter returns the underlying sdkmetric.Exporter.

func (*InMemoryMetricExporter) ForceFlush

ForceFlush implements sdkmetric.Exporter.

func (*InMemoryMetricExporter) GetMetrics

func (e *InMemoryMetricExporter) GetMetrics() int

GetMetrics returns the number of Export calls received.

func (*InMemoryMetricExporter) LastResourceMetrics added in v0.1.2

func (e *InMemoryMetricExporter) LastResourceMetrics() *metricdata.ResourceMetrics

LastResourceMetrics returns a deep copy of the last ResourceMetrics passed to Export, or nil. Safe to call after shutdown; returned value is independent of SDK buffers.

func (*InMemoryMetricExporter) Len

func (e *InMemoryMetricExporter) Len() int

Len returns the number of Export calls received.

func (*InMemoryMetricExporter) Reset

func (e *InMemoryMetricExporter) Reset()

Reset clears the export count and the last snapshot so tests do not see stale data.

func (*InMemoryMetricExporter) Shutdown

Shutdown implements sdkmetric.Exporter.

func (*InMemoryMetricExporter) Temporality

Temporality implements sdkmetric.Exporter.

type InMemoryTraceExporter

type InMemoryTraceExporter struct {
	// contains filtered or unexported fields
}

InMemoryTraceExporter stores spans in memory for test assertions.

func NewInMemoryTraceExporter

func NewInMemoryTraceExporter() *InMemoryTraceExporter

NewInMemoryTraceExporter returns a new in-memory trace exporter.

func SetupTestTracing

func SetupTestTracing(t *testing.T) *InMemoryTraceExporter

SetupTestTracing configures the global tracer with an in-memory exporter using a synchronous SimpleSpanProcessor so spans are available immediately for assertions. Registers cleanup on t. Returns the InMemoryTraceExporter for assertions.

func (*InMemoryTraceExporter) GetSpans

GetSpans returns a copy of the stored span stubs.

func (*InMemoryTraceExporter) Len

func (e *InMemoryTraceExporter) Len() int

Len returns the number of stored spans.

func (*InMemoryTraceExporter) Reset

func (e *InMemoryTraceExporter) Reset()

Reset clears all stored spans.

func (*InMemoryTraceExporter) SpanExporter added in v0.2.0

func (e *InMemoryTraceExporter) SpanExporter() sdktrace.SpanExporter

SpanExporter returns the underlying sdktrace.SpanExporter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL