span

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package span provides small helpers for running functions inside OpenTelemetry spans.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilFunc = errors.New("xtrace: nil span function")

Functions

func Call

func Call[T any](ctx context.Context, tracer trace.Tracer, name string, f Func1[T], opts ...Option) (ret T, err error)

Call is Run with one return value.

func Call2

func Call2[T, U any](ctx context.Context, tracer trace.Tracer, name string, f Func2[T, U], opts ...Option) (ret T, ret2 U, err error)

Call2 is Run with two return values.

func Run

func Run(ctx context.Context, tracer trace.Tracer, name string, f Func, opts ...Option) (err error)

Run starts name on tracer, executes f, records returned errors, and ends the span. A nil tracer falls back to the global OpenTelemetry tracer.

Types

type Func

type Func func(ctx context.Context, span trace.Span) error

Func is a function executed with a started span.

type Func1

type Func1[T any] func(ctx context.Context, span trace.Span) (T, error)

Func1 is a function executed with a started span and one return value.

type Func2

type Func2[T, U any] func(ctx context.Context, span trace.Span) (T, U, error)

Func2 is a function executed with a started span and two return values.

type Option

type Option func(*options)

Option configures span helper behavior.

func WithAttrs

func WithAttrs(attrs ...attribute.KeyValue) Option

WithAttrs starts the span with the provided attributes.

func WithErrorFilter

func WithErrorFilter(filter func(error) bool) Option

WithErrorFilter decides which returned errors should be recorded on the span.

func WithErrorRenderer added in v1.3.0

func WithErrorRenderer(render func(error) string) Option

WithErrorRenderer renders a recorded error's text through render: the exception event's exception.message and the span status description carry render(err) instead of err.Error(). The exception type and stack stay. Use it to keep URLs with tokens or database values out of traces (the same renderer as xlog.WithErrorRenderer). A nil render keeps err.Error().

func WithRecoverPanics

func WithRecoverPanics() Option

WithRecoverPanics converts a panic into a PanicError return value after recording it on the span. Without this option panics are recorded and re-panicked.

func WithSpanOptions

func WithSpanOptions(opts ...trace.SpanStartOption) Option

WithSpanOptions appends OpenTelemetry span start options.

func WithStackTrace added in v1.1.5

func WithStackTrace(extract func(error) string) Option

WithStackTrace records the stack an error carries as the exception's exception.stacktrace: extract returns the stack captured where the error was made (e.g. by its wrapping package), or "" for none, and then no stacktrace is recorded — a stack taken at the span's end would only show the helper's own frames.

func WithoutErrorStatus

func WithoutErrorStatus() Option

WithoutErrorStatus prevents setting span status to Error for returned errors.

func WithoutRecordError

func WithoutRecordError() Option

WithoutRecordError prevents RecordError calls for returned errors.

type PanicError

type PanicError struct {
	Value any
}

PanicError is returned when WithRecoverPanics catches a panic.

func (PanicError) Error

func (e PanicError) Error() string

Jump to

Keyboard shortcuts

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