oteltest

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package oteltest provides testing utilities for OpenTelemetry tracing. It includes an in-memory span exporter and assertion helpers for verifying span attributes, events, and structure in unit tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Key   string
	Value attr.Value
	// contains filtered or unexported fields
}

Attr is a wrapper around the OTel Attribute with some helpful testing functions.

func (Attr) AssertEquals

func (a Attr) AssertEquals(expected any)

AssertEquals asserts that the attribute is equal to the expected value.

func (Attr) String

func (a Attr) String() string

String returns the attribute as a string and fails if the attribute is not a string.

type Event

type Event struct {
	Name  string
	Attrs map[string]any
}

Event is a summary of an otel event.

type Exporter

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

Exporter is a wrapper around the OTel InMemoryExporter that provides some helper functions for testing.

func Setup

Setup sets up otel tracing for testing (no sampling, sync, stores spans in memory) and returns a TracerProvider and an Exporter that can be used to flush the spans. The TracerProvider can be passed to middleware to ensure spans are captured in tests. Tests can get a tracer by calling tp.Tracer(name).

func (*Exporter) Flush

func (e *Exporter) Flush() []Span

Flush returns the spans buffered in memory.

func (*Exporter) FlushOne

func (e *Exporter) FlushOne() Span

FlushOne returns the first span buffered in memory and fails if there is not exactly one span.

func (*Exporter) InMemoryExporter

func (e *Exporter) InMemoryExporter() *tracetest.InMemoryExporter

InMemoryExporter returns the underlying OTel InMemoryExporter.

type Span

type Span struct {
	Stub tracetest.SpanStub
	// contains filtered or unexported fields
}

Span is a wrapper around the OTel SpanStub with some helpful testing functions.

func NewSpan

func NewSpan(t *testing.T, name string, opts TestSpan) Span

NewSpan creates a test span from a TestSpan specification, setting defaults for missing fields.

func (*Span) AssertAttrEquals

func (s *Span) AssertAttrEquals(key string, expected any)

AssertAttrEquals asserts that the attribute is equal to the expected value.

func (*Span) AssertChildOf added in v0.3.1

func (s *Span) AssertChildOf(parent *Span)

AssertChildOf asserts that this span is a child of the given parent span.

func (*Span) AssertEqual

func (s *Span) AssertEqual(expected TestSpan)

AssertEqual compares this span with an expected TestSpan. This test ignores non-deterministic fields like timestamps and span IDs and only compares the meaningful fields like name, status, attributes, and events.

func (*Span) AssertInTimeRange

func (s *Span) AssertInTimeRange(tr TimeRange)

AssertInTimeRange asserts that the span's start and end times are within the given time range.

func (*Span) AssertJSONAttrEquals

func (s *Span) AssertJSONAttrEquals(key string, expected any)

AssertJSONAttrEquals asserts that a JSON-encoded attribute equals the expected value. The attribute value is expected to be a JSON string that will be unmarshaled and compared.

func (*Span) AssertMetadata

func (s *Span) AssertMetadata(expected map[string]any)

AssertMetadata asserts that the span has the expected metadata. Metadata is stored as a JSON object in the "braintrust.metadata" attribute.

func (*Span) AssertNameIs

func (s *Span) AssertNameIs(expected string)

AssertNameIs asserts that the span's name equals the expected name.

func (*Span) AssertTags

func (s *Span) AssertTags(expected []string)

AssertTags asserts that the span has the expected tags. Tags are stored as an OTel StringSlice attribute.

func (*Span) Attr

func (s *Span) Attr(key string) Attr

Attr return the attribute matching the key and fails if there isn't exactly one.

func (*Span) Attrs

func (s *Span) Attrs(key string) []Attr

Attrs returns all the span's attributes matching the key.

func (*Span) Events

func (s *Span) Events() []sdktrace.Event

Events returns the span's events.

func (*Span) HasAttr

func (s *Span) HasAttr(key string) bool

HasAttr returns true if the span has at least one attribute with the given key.

func (*Span) Input

func (s *Span) Input() any

Input returns the Braintrust input from the span attributes. Supports both "braintrust.input" (OpenAI tracing) and "braintrust.input_json" (eval tracing).

func (*Span) Metadata

func (s *Span) Metadata() map[string]any

Metadata returns the Braintrust metadata from the span attributes.

func (*Span) Metrics

func (s *Span) Metrics() map[string]float64

Metrics returns the Braintrust metrics from the span attributes.

func (*Span) Name

func (s *Span) Name() string

Name returns the span's name.

func (*Span) Output

func (s *Span) Output() any

Output returns the Braintrust output from the span attributes. Supports both "braintrust.output" (OpenAI tracing) and "braintrust.output_json" (eval tracing).

func (*Span) Status

func (s *Span) Status() sdktrace.Status

Status returns the span's status.

type TestSpan

type TestSpan struct {
	Name              string
	Attrs             map[string]any
	JSONAttrs         map[string]any // Values will be JSON-serialized and merged into Attrs
	StatusCode        codes.Code
	StatusDescription string
	Events            []Event
	SpanKind          oteltrace.SpanKind
	TimeRange         TimeRange // Optional: if non-zero, validates span timing is within this range
}

TestSpan is a span that is easier to write and compare to real otel spans in tests. Any missing attributes will be set to sane OTel defaults, like Unset status and empty events and attributes.

type TimeRange

type TimeRange struct {
	Start time.Time
	End   time.Time
}

TimeRange represents a time range with a start and end time.

func (TimeRange) IsZero

func (tr TimeRange) IsZero() bool

IsZero returns true if the TimeRange is the zero value

type Timer

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

Timer is a simple timer for creating time ranges in tests.

func NewTimer

func NewTimer() *Timer

NewTimer creates a new timer starting from the current time.

func (*Timer) Tick

func (t *Timer) Tick() TimeRange

Tick returns the time range since the timer was created to now.

Jump to

Keyboard shortcuts

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