tracingtest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 6 Imported by: 0

README

tracingtest

import "github.com/brpaz/lib-go/tracing/tracingtest"

Package tracingtest provides OpenTelemetry test helpers. Use ContextWithFakeSpan to inject a span into a context for unit tests, and NewRecorder to capture and assert on spans emitted by production code.

Index

func ContextWithFakeSpan

func ContextWithFakeSpan(ctx context.Context) (context.Context, string, string)

ContextWithFakeSpan returns a context carrying a fake OTel span with randomly-generated trace and span IDs. It returns the context plus the hex-encoded trace ID and span ID so callers can assert on them without hardcoding values.

type Recorder

Recorder captures all spans emitted during a test. Install it with NewRecorder and call Cleanup when the test ends.

type Recorder struct {
    *tracetest.SpanRecorder
    // contains filtered or unexported fields
}

func NewRecorder
func NewRecorder() *Recorder

NewRecorder installs an in-memory OTel provider that records all spans. The previous global provider is restored on Cleanup.

rec := tracingtest.NewRecorder()
t.Cleanup(rec.Cleanup)

func (*Recorder) Cleanup
func (r *Recorder) Cleanup()

Cleanup restores the global tracer provider to the value it had before NewRecorder was called.

Generated by gomarkdoc

Documentation

Overview

Package tracingtest provides OpenTelemetry test helpers. Use ContextWithFakeSpan to inject a span into a context for unit tests, and NewRecorder to capture and assert on spans emitted by production code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithFakeSpan

func ContextWithFakeSpan(ctx context.Context) (context.Context, string, string)

ContextWithFakeSpan returns a context carrying a fake OTel span with randomly-generated trace and span IDs. It returns the context plus the hex-encoded trace ID and span ID so callers can assert on them without hardcoding values.

Types

type Recorder

type Recorder struct {
	*tracetest.SpanRecorder
	// contains filtered or unexported fields
}

Recorder captures all spans emitted during a test. Install it with NewRecorder and call Cleanup when the test ends.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder installs an in-memory OTel provider that records all spans. The previous global provider is restored on Cleanup.

rec := tracingtest.NewRecorder()
t.Cleanup(rec.Cleanup)

func (*Recorder) Cleanup

func (r *Recorder) Cleanup()

Cleanup restores the global tracer provider to the value it had before NewRecorder was called.

Jump to

Keyboard shortcuts

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