tracetest

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package tracetest provides test helpers for code that depends on the v2/trace package. Use NewRecorder to obtain a [Tracer] that captures completed spans in memory so you can assert on them in unit tests without a live OTLP collector.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RecordedEvent

type RecordedEvent struct {
	// Name is the event name (e.g. "exception" for errors).
	Name string
	// Attributes holds the event attributes, keyed by attribute name.
	Attributes map[string]any
}

RecordedEvent is a single event recorded on a span.

type RecordedSpan

type RecordedSpan struct {
	// Name is the operation name passed to Tracer.Start.
	Name string
	// Attributes holds all span attributes, keyed by attribute name.
	// Each value is the native Go type (bool, int64, float64, string, etc.).
	Attributes map[string]any
	// HasError is true when the span status is codes.Error.
	HasError bool
	// StatusCode is the OTEL status code set on the span.
	StatusCode codes.Code
	// StatusMsg is the status description, typically err.Error().
	StatusMsg string
	// Events holds the span events recorded via Span.RecordError or
	// the raw OTEL API.
	Events []RecordedEvent
}

RecordedSpan is a stable, assertable snapshot of a completed span. It is safe to use with assert.Equal and similar helpers.

type SpanRecorder

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

SpanRecorder captures completed spans for test assertions.

func NewRecorder

func NewRecorder() (*trace.Tracer, *SpanRecorder)

NewRecorder returns a trace.Tracer backed by an in-memory span recorder and the SpanRecorder used to inspect captured spans. The Tracer does NOT register itself as the global OTEL provider, ensuring test isolation.

tracer, rec := tracetest.NewRecorder()
ctx, span := tracer.Start(context.Background(), "my-op")
span.SetAttribute("user.id", 42)
span.End()

spans := rec.Ended()
assert.Equal(t, "my-op", spans[0].Name)
assert.Equal(t, int64(42), spans[0].Attributes["user.id"])

func (*SpanRecorder) Ended

func (r *SpanRecorder) Ended() []RecordedSpan

Ended returns a snapshot of all completed spans captured so far, projected into RecordedSpan values for easy assertion. The returned slice is a copy and is safe to hold across concurrent calls.

func (*SpanRecorder) Reset

func (r *SpanRecorder) Reset()

Reset clears all recorded spans. Useful when a single recorder is shared across multiple sub-tests.

Jump to

Keyboard shortcuts

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