otelt

package
v0.4.44 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package otelt provides helpers for testing the results of OpenTelemetry metrics and traces.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEvents

func AssertEvents(tb testing.TB, expected, actual []trace.Event) bool

AssertEvents tests whether a span event has the expected name and attributes.

func AssertSpanEqual

func AssertSpanEqual(
	tb testing.TB,
	span trace.ReadOnlySpan,
	name string,
	attributes []attribute.KeyValue,
	events []trace.Event,
) bool

AssertSpanEqual tests whether the given span has the expected name, attributes and events.

func MustSpan

func MustSpan(ctx context.Context, traceID, spanID string, sampled bool) context.Context

MustSpan wraps NewSpan, panicking if an error occurs. Errors can only occur if an invalid trace ID or span ID is provided.

func NewSpan

func NewSpan(ctx context.Context, traceID, spanID string, sampled bool) (context.Context, error)

NewSpan creates a trace.Span using the give trace ID, span ID and sampled value and adds it to the given context. It can be used to produce a span with a deterministic value.

Types

type MetricCollectFn

type MetricCollectFn func(context.Context) (metricdata.ResourceMetrics, error)

Type aliases for collect functions.

func NewMeterProvider

func NewMeterProvider() (metric.MeterProvider, MetricCollectFn)

NewMeterProvider provides a MeterProvider suitable for use in unit tests.

Example
package main

import (
	"testing"

	"go.opentelemetry.io/otel/sdk/instrumentation"
	"go.opentelemetry.io/otel/sdk/metric/metricdata"
	"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
	"go.opentelemetry.io/otel/sdk/resource"

	"github.com/mattdowdell/sandbox/pkg/otelt"
)

func main() {
	t := new(testing.T)

	provider, collect := otelt.NewMeterProvider()

	counter, err := provider.Meter("path/to/package").Int64Counter("my_counter")
	if err != nil {
		t.Fatal(err)
	}

	counter.Add(t.Context(), 1)

	got, err := collect(t.Context())
	if err != nil {
		t.Fatal(err)
	}

	want := metricdata.ResourceMetrics{
		Resource: resource.Default(),
		ScopeMetrics: []metricdata.ScopeMetrics{
			{
				Scope: instrumentation.Scope{
					Name: "path/to/package",
				},
				Metrics: []metricdata.Metrics{
					{
						Name: "my_counter",
						Data: metricdata.Sum[int64]{
							Temporality: metricdata.CumulativeTemporality,
							IsMonotonic: true,
							DataPoints: []metricdata.DataPoint[int64]{
								{
									Value: 1,
								},
							},
						},
					},
				},
			},
		},
	}

	metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp())
}

type TraceCollectFn

type TraceCollectFn func() []sdktrace.ReadOnlySpan

Type aliases for collect functions.

func NewTracerProvider

func NewTracerProvider() (trace.TracerProvider, TraceCollectFn)

NewTracerProvider provides a TracerProvider suitable for use in unit tests.

Example
t := new(testing.T)

provider, collect := otelt.NewTracerProvider()

_, span := provider.Tracer(testPackage).Start(t.Context(), testSpanName)

span.SetAttributes(attribute.Bool("example", true))
span.AddEvent("event")

span.End()

got := collect()
if len(got) != 1 {
	t.Fatal("unexpected length:", len(got))
}

attrs := []attribute.KeyValue{attribute.Bool("example", true)}
events := []trace.Event{{Name: "event"}}

otelt.AssertSpanEqual(t, got[0], testSpanName, attrs, events)

Jump to

Keyboard shortcuts

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