import "go.opentelemetry.io/otel/oteltest"
Package oteltest provides testing utilities for the otel package.
This package is currently in a pre-GA phase. Backwards incompatible changes may be introduced in subsequent minor version releases as we work to track the evolving OpenTelemetry specification and user feedback.
The Harness can be used to validate an implementation of the OpenTelemetry API defined by the `otel` package.
func TestCustomSDKTracingImplementation(t *testing.T) { yourTraceProvider := NewTracerProvider() subjectFactory := func() otel.Tracer { return yourTraceProvider.Tracer("testing") } oteltest.NewHarness(t).TestTracer(subjectFactory) }
Currently the Harness only provides testing of the trace portion of the OpenTelemetry API.
To test tracing functionality a full testing implementation of the OpenTelemetry tracing API are provided. The provided TracerProvider, Tracer, and Span all implement their related interface and are designed to allow introspection of their state and history. Additionally, a SpanRecorder can be provided to the TracerProvider to record all Spans started and ended by the testing structures.
sr := new(oteltest.StandardSpanRecorder) tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
config.go doc.go event.go harness.go meter.go provider.go span.go text_map_propagator.go tracer.go
DefaulTracer returns a default tracer for testing purposes.
LabelsToMap converts label set to keyValue map, to be easily used in tests
ResolveNumberByKind takes defined metric descriptor creates a concrete typed metric number
type Async struct { Instrument // contains filtered or unexported fields }
type Batch struct { // Measurement needs to be aligned for 64-bit atomic operations. Measurements []Measurement Ctx context.Context Labels []label.KeyValue LibraryName string }
Event encapsulates the properties of calls to AddEvent.
type Harness struct {
// contains filtered or unexported fields
}
Harness is a testing harness used to test implementations of the OpenTelemetry API.
NewHarness returns an instantiated *Harness using t.
TestTracer runs validation tests for an implementation of the OpenTelemetry Tracer API.
type Instrument struct {
// contains filtered or unexported fields
}
func (i Instrument) Descriptor() metric.Descriptor
type Measured struct { Name string InstrumentationName string InstrumentationVersion string Labels map[label.Key]label.Value Number number.Number }
Measured is the helper struct which provides flat representation of recorded measurements to simplify testing
AsStructs converts recorded batches to array of flat, readable Measured helper structures
type Measurement struct { // Number needs to be aligned for 64-bit atomic operations. Number number.Number Instrument metric.InstrumentImpl }
MeterImpl is an OpenTelemetry Meter implementation used for testing.
func NewMeterProvider() (*MeterImpl, metric.MeterProvider)
func (m *MeterImpl) NewAsyncInstrument(descriptor metric.Descriptor, runner metric.AsyncRunner) (metric.AsyncImpl, error)
func (m *MeterImpl) RecordBatch(ctx context.Context, labels []label.KeyValue, measurements ...metric.Measurement)
type Option interface {
Apply(*config)
}
Option applies an option to a config.
WithSpanContextFunc sets the SpanContextFunc used to generate a new Spans context from a parent SpanContext.
func WithSpanRecorder(sr SpanRecorder) Option
WithSpanRecorder sets the SpanRecorder to use with the TracerProvider for testing.
type Span struct {
// contains filtered or unexported fields
}
Span is an OpenTelemetry Span used for testing.
func (s *Span) AddEvent(name string, o ...trace.EventOption)
AddEvent adds an event to s.
Attributes returns the attributes set on s, either at or after creation time. If the same attribute key was set multiple times, the last call will be used. Attributes cannot be changed after End has been called on s.
func (s *Span) End(opts ...trace.SpanOption)
End ends s. If the Tracer that created s was configured with a SpanRecorder, that recorder's OnEnd method is called as the final part of this method.
EndTime returns the time at which s was ended if at has been ended, or false otherwise. If the span has been ended, the returned time will be the wall-clock time unless a specific end time was provided.
Ended returns whether s has been ended, i.e. whether End has been called at least once on s.
Events returns the events set on s. Events cannot be changed after End has been called on s.
IsRecording returns the recording state of s.
Links returns the links set on s at creation time. If multiple links for the same SpanContext were set, the last link will be used.
Name returns the name most recently set on s, either at or after creation time. It cannot be change after End has been called on s.
ParentSpanID returns the SpanID of the parent Span. If s is a root Span, and therefore does not have a parent, the returned SpanID will be invalid (i.e., it will contain all zeroes).
func (s *Span) RecordError(err error, opts ...trace.EventOption)
RecordError records an error as a Span event.
SetAttributes sets attrs as attributes of s.
SetName sets the name of s.
SetStatus sets the status of s in the form of a code and a message.
func (s *Span) SpanContext() trace.SpanContext
SpanContext returns the SpanContext of s.
SpanKind returns the span kind of s.
StartTime returns the time at which s was started. This will be the wall-clock time unless a specific start time was provided.
StatusCode returns the code of the status most recently set on s, or codes.OK if no status has been explicitly set. It cannot be changed after End has been called on s.
StatusMessage returns the status message most recently set on s or the empty string if no status message was set.
Tracer returns the Tracer that created s.
type SpanRecorder interface { // OnStart is called by the Tracer when it starts a Span. OnStart(span *Span) // OnEnd is called by the Span when it ends. OnEnd(span *Span) }
SpanRecorder performs operations to record a span as it starts and ends.
type StandardSpanRecorder struct {
// contains filtered or unexported fields
}
StandardSpanRecorder is a SpanRecorder that records all started and ended spans in an ordered recording. StandardSpanRecorder is designed to be concurrent safe and can by used by multiple goroutines.
func (ssr *StandardSpanRecorder) Completed() []*Span
Completed returns a copy of all ended Spans in the order they were ended.
func (ssr *StandardSpanRecorder) OnEnd(span *Span)
OnEnd records span as completed.
func (ssr *StandardSpanRecorder) OnStart(span *Span)
OnStart records span as started.
func (ssr *StandardSpanRecorder) Started() []*Span
Started returns a copy of all started Spans in the order they were started.
type Sync struct { Instrument }
type TextMapCarrier struct {
// contains filtered or unexported fields
}
TextMapCarrier provides a testing storage medium to for a TextMapPropagator. It records all the operations it performs.
func NewTextMapCarrier(data map[string]string) *TextMapCarrier
NewTextMapCarrier returns a new *TextMapCarrier populated with data.
func (c *TextMapCarrier) Get(key string) string
Get returns the value associated with the passed key.
GotKey tests if c.Get has been called for key.
GotN tests if n calls to c.Get have been made.
func (c *TextMapCarrier) Reset()
Reset zeros out the internal state recording of c.
func (c *TextMapCarrier) Set(key, value string)
Set stores the key-value pair.
SetKeyValue tests if c.Set has been called for the key-value pair.
SetN tests if n calls to c.Set have been made.
func NewTextMapPropagator(name string) *TextMapPropagator
func (p *TextMapPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context
Extract reads cross-cutting concerns for p from the carrier into a Context.
ExtractedN tests if p has made n extractions from the lineage of ctx. nolint (context is not first arg)
func (p *TextMapPropagator) Fields() []string
Fields returns p.Name as the key who's value is set with Inject.
func (p *TextMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier)
Inject set cross-cutting concerns for p from the Context into the carrier.
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n int) bool
InjectedN tests if p has made n injections to carrier.
type Tracer struct { // Name is the instrumentation name. Name string // Version is the instrumentation version. Version string // contains filtered or unexported fields }
Tracer is an OpenTelemetry Tracer implementation used for testing.
func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.SpanOption) (context.Context, trace.Span)
Start creates a span. If t is configured with a SpanRecorder its OnStart method will be called after the created Span has been initialized.
type TracerProvider struct {
// contains filtered or unexported fields
}
TracerProvider is a testing TracerProvider. It is an functioning implementation of an OpenTelemetry TracerProvider and can be configured with a SpanRecorder that it configure all Tracers it creates to record their Spans with.
func NewTracerProvider(options ...Option) *TracerProvider
NewTracerProvider returns a *TracerProvider configured with options.
func (p *TracerProvider) Tracer(instName string, opts ...trace.TracerOption) trace.Tracer
Tracer returns an OpenTelemetry Tracer used for testing.
Package oteltest imports 19 packages (graph). Updated 2021-01-21. Refresh now. Tools for package owners.