tracing

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TraceparentHeader = "traceparent"
	TracestateHeader  = "tracestate"
)

TraceparentHeader and TracestateHeader are the W3C Trace Context headers.

Variables

This section is empty.

Functions

func ContextWithRemote

func ContextWithRemote(ctx context.Context, sc SpanContext) context.Context

ContextWithRemote returns ctx carrying a parent received from another process (traceparent header, queue job metadata).

func ContextWithSpan

func ContextWithSpan(ctx context.Context, s *Span) context.Context

ContextWithSpan returns ctx carrying span.

func Extract

func Extract(ctx context.Context, h http.Header) context.Context

Extract returns ctx with the remote parent from the request's traceparent header, if it has a valid one.

func Inject

func Inject(ctx context.Context, h http.Header)

Inject writes the traceparent (and tracestate) of the span in ctx.

func TraceIDFromContext

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext returns the current trace ID as hex, or "".

func Transport

func Transport(base http.RoundTripper) http.RoundTripper

Transport wraps base (nil = http.DefaultTransport) so every outbound request gets a client span and a traceparent header.

Types

type Event

type Event struct {
	Name  string
	Time  time.Time
	Attrs map[string]any
}

Event is a timestamped annotation on a span.

type Exporter

type Exporter interface {
	ExportSpans(spans []SpanData)
	Shutdown(ctx context.Context) error
}

Exporter receives finished, sampled spans.

type OTLPConfig

type OTLPConfig struct {
	// Endpoint is the full traces URL, e.g. http://localhost:4318/v1/traces.
	Endpoint string
	// Headers are sent with every export (API keys etc.).
	Headers map[string]string
	// ServiceName is the service.name resource attribute.
	ServiceName string
	// Resource holds extra resource attributes (deployment.environment, ...).
	Resource map[string]string
	// BatchSize flushes once this many spans are buffered (default 512).
	BatchSize int
	// FlushInterval flushes buffered spans at least this often (default 5s).
	FlushInterval time.Duration
	// MaxQueue drops spans beyond this many unsent (default 8192).
	MaxQueue int
	// Client overrides the HTTP client (default: 10s timeout).
	Client *http.Client
}

OTLPConfig configures the OTLP/HTTP (JSON) exporter.

type OTLPExporter

type OTLPExporter struct {
	// contains filtered or unexported fields
}

OTLPExporter batches spans and POSTs them as OTLP/HTTP JSON.

func ConfigureFromEnv

func ConfigureFromEnv() *OTLPExporter

ConfigureFromEnv installs an OTLP exporter on the global tracer when an OTLP endpoint is set (see the package doc) and returns it, or nil. Spans and traceparent propagation work either way; without an exporter they are simply not sent anywhere.

func NewOTLPExporter

func NewOTLPExporter(cfg OTLPConfig) *OTLPExporter

NewOTLPExporter starts an exporter. Call Shutdown to flush on exit.

func (*OTLPExporter) ExportSpans

func (e *OTLPExporter) ExportSpans(spans []SpanData)

ExportSpans implements Exporter. It never blocks on the network.

func (*OTLPExporter) Flush

func (e *OTLPExporter) Flush(ctx context.Context) error

Flush sends everything buffered now.

func (*OTLPExporter) Shutdown

func (e *OTLPExporter) Shutdown(ctx context.Context) error

Shutdown flushes and stops the exporter.

type Span

type Span struct {
	// contains filtered or unexported fields
}

Span is one timed operation. Methods are safe for concurrent use and are no-ops on a nil span.

func SpanFromContext

func SpanFromContext(ctx context.Context) *Span

SpanFromContext returns the current span, or nil.

func Start

func Start(ctx context.Context, name string, opts ...StartOption) (context.Context, *Span)

Start begins a span on the global tracer.

func (*Span) AddEvent

func (s *Span) AddEvent(name string, attrs map[string]any)

AddEvent records a timestamped event.

func (*Span) End

func (s *Span) End()

End finishes the span and hands it to the exporter. Calling it again does nothing.

func (*Span) RecordError

func (s *Span) RecordError(err error)

RecordError marks the span as failed.

func (*Span) SetAttr

func (s *Span) SetAttr(key string, value any)

SetAttr records an attribute (string, bool, int, int64, float64 or anything else, which is formatted with %v).

func (*Span) SetError

func (s *Span) SetError(description string)

SetError marks the span as failed with a description (e.g. "HTTP 503").

func (*Span) SpanContext

func (s *Span) SpanContext() SpanContext

SpanContext returns the span's propagation context.

type SpanContext

type SpanContext struct {
	TraceID    TraceID
	SpanID     SpanID
	Sampled    bool
	TraceState string // opaque vendor data, passed through unchanged
	Remote     bool
}

SpanContext is the part of a span that crosses process boundaries.

func ParseTraceparent

func ParseTraceparent(h string) (SpanContext, error)

ParseTraceparent parses a W3C traceparent header value.

func SpanContextFromContext

func SpanContextFromContext(ctx context.Context) SpanContext

SpanContextFromContext returns the current span's context, or a remote parent's, or an invalid SpanContext.

func (SpanContext) IsValid

func (sc SpanContext) IsValid() bool

IsValid reports whether both IDs are set.

func (SpanContext) Traceparent

func (sc SpanContext) Traceparent() string

Traceparent renders the W3C traceparent header value.

type SpanData

type SpanData struct {
	TraceID     TraceID
	SpanID      SpanID
	Parent      SpanID
	TraceState  string
	Name        string
	Kind        SpanKind
	Start, End  time.Time
	Attrs       map[string]any
	Events      []Event
	Error       bool
	ErrorString string
}

SpanData is a finished span as handed to an Exporter.

type SpanID

type SpanID [8]byte

SpanID identifies a span (8 bytes).

func (SpanID) IsValid

func (s SpanID) IsValid() bool

IsValid reports whether the ID is non-zero.

func (SpanID) String

func (s SpanID) String() string

type SpanKind

type SpanKind int

SpanKind follows OpenTelemetry's span kinds.

const (
	KindInternal SpanKind = 1
	KindServer   SpanKind = 2
	KindClient   SpanKind = 3
	KindProducer SpanKind = 4
	KindConsumer SpanKind = 5
)

type StartOption

type StartOption func(*Span)

StartOption configures a span.

func WithAttrs

func WithAttrs(attrs map[string]any) StartOption

WithAttrs sets initial attributes.

func WithKind

func WithKind(k SpanKind) StartOption

WithKind sets the span kind (default internal).

func WithParent

func WithParent(sc SpanContext) StartOption

WithParent starts the span under sc instead of the span in ctx.

type TraceID

type TraceID [16]byte

TraceID identifies a trace (16 bytes).

func (TraceID) IsValid

func (t TraceID) IsValid() bool

IsValid reports whether the ID is non-zero.

func (TraceID) String

func (t TraceID) String() string

type Tracer

type Tracer struct {
	// contains filtered or unexported fields
}

Tracer starts spans and sends them to an exporter.

func Global

func Global() *Tracer

Global returns the process-wide tracer.

func (*Tracer) SetExporter

func (t *Tracer) SetExporter(e Exporter)

SetExporter sets where finished spans go (nil drops them).

func (*Tracer) SetSampleRatio

func (t *Tracer) SetSampleRatio(r float64)

SetSampleRatio sets the share of new traces that are recorded (0..1). Traces continued from a caller follow the caller's sampling decision.

func (*Tracer) Start

func (t *Tracer) Start(ctx context.Context, name string, opts ...StartOption) (context.Context, *Span)

Start begins a span under the span (or remote parent) in ctx, or a new trace if there is none, and returns ctx carrying it.

Jump to

Keyboard shortcuts

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