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 ¶
- type Attr
- type Event
- type Exporter
- type Span
- func (s *Span) AssertAttrEquals(key string, expected any)
- func (s *Span) AssertChildOf(parent *Span)
- func (s *Span) AssertEqual(expected TestSpan)
- func (s *Span) AssertInTimeRange(tr TimeRange)
- func (s *Span) AssertJSONAttrEquals(key string, expected any)
- func (s *Span) AssertMetadata(expected map[string]any)
- func (s *Span) AssertNameIs(expected string)
- func (s *Span) AssertTags(expected []string)
- func (s *Span) Attr(key string) Attr
- func (s *Span) Attrs(key string) []Attr
- func (s *Span) Events() []sdktrace.Event
- func (s *Span) HasAttr(key string) bool
- func (s *Span) Input() any
- func (s *Span) Metadata() map[string]any
- func (s *Span) Metrics() map[string]float64
- func (s *Span) Name() string
- func (s *Span) Output() any
- func (s *Span) Status() sdktrace.Status
- type TestSpan
- type TimeRange
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attr ¶
Attr is a wrapper around the OTel Attribute with some helpful testing functions.
func (Attr) AssertEquals ¶
AssertEquals asserts that the attribute is equal to the expected value.
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 ¶
func Setup(t *testing.T) (oteltrace.TracerProvider, *Exporter)
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) FlushOne ¶
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 ¶
Span is a wrapper around the OTel SpanStub with some helpful testing functions.
func NewSpan ¶
NewSpan creates a test span from a TestSpan specification, setting defaults for missing fields.
func (*Span) AssertAttrEquals ¶
AssertAttrEquals asserts that the attribute is equal to the expected value.
func (*Span) AssertChildOf ¶ added in v0.3.1
AssertChildOf asserts that this span is a child of the given parent span.
func (*Span) AssertEqual ¶
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 ¶
AssertInTimeRange asserts that the span's start and end times are within the given time range.
func (*Span) AssertJSONAttrEquals ¶
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 ¶
AssertMetadata asserts that the span has the expected metadata. Metadata is stored as a JSON object in the "braintrust.metadata" attribute.
func (*Span) AssertNameIs ¶
AssertNameIs asserts that the span's name equals the expected name.
func (*Span) AssertTags ¶
AssertTags asserts that the span has the expected tags. Tags are stored as an OTel StringSlice attribute.
func (*Span) Attr ¶
Attr return the attribute matching the key and fails if there isn't exactly one.
func (*Span) HasAttr ¶
HasAttr returns true if the span has at least one attribute with the given key.
func (*Span) Input ¶
Input returns the Braintrust input from the span attributes. Supports both "braintrust.input" (OpenAI tracing) and "braintrust.input_json" (eval tracing).
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.