Help us shape the future of Go! Take the 2026 Go Developer Survey

tracing

package
v0.5.3 Latest Latest
Warning

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

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

Documentation

Overview

Package tracing preserves bounded W3C trace context without global providers, recording backends, request payloads or authentication capabilities.

Index

Constants

View Source
const MaxParentBytes = 512
View Source
const MaxStateBytes = 512
View Source
const ParentHeader = "traceparent"
View Source
const StateHeader = "tracestate"

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx context.Context, value Context) (context.Context, error)

WithContext attaches a validated trace value to a nonnil context.

func WithoutContext

func WithoutContext(ctx context.Context) context.Context

WithoutContext starts a new trace boundary while preserving cancellation and all unrelated context values. HTTP ingress uses it to prevent a long-lived kernel span from becoming the parent of every independent incoming request.

Types

type Context

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

Context is an immutable propagation snapshot. Trace IDs correlate operations, not users; do not infer authentication or authorization from trace metadata. Tracestate is opaque vendor data and is omitted from ordinary formatting.

func FromContext

func FromContext(ctx context.Context) Context

FromContext returns the attached trace context, or its zero value when absent or ctx is nil.

func New

func New(sampled bool) (Context, error)

New creates random trace and span identifiers with the supplied sampling flag.

func Parse

func Parse(parent, state string) (Context, error)

Parse accepts traceparent version 00 and the known prefix of future versions. Malformed parents fail without retaining their values. Invalid tracestate is discarded independently, as required by the W3C processing model. Adapters choose whether to trust propagation at their network boundary.

func Start

func Start(ctx context.Context, sampled bool) (context.Context, Context, error)

Start derives one child operation, or starts a new root when the context has no trace. It preserves the caller's cancellation, deadlines and other values. An already-cancelled context still receives trace identity: the operation's cancellation remains the caller's concern and its observation is not dropped.

func StartSampled

func StartSampled(ctx context.Context, sampler Sampler) (context.Context, Context, error)

StartSampled is Start with a trace-ID based local sampling policy, such as RatioSampler. Incoming sampled flags never override the local decision.

func (Context) Child

func (c Context) Child(sampled bool) (Context, error)

Child creates a new operation in the same trace. Its explicit sampling choice is local policy, not a requirement imposed by an untrusted caller's flag.

func (Context) Format

func (Context) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing trace identifiers.

func (Context) IsZero

func (c Context) IsZero() bool

IsZero reports whether identifiers, sampling flag, and trace state are all empty.

func (Context) LogValue

func (c Context) LogValue() slog.Value

LogValue keeps opaque vendor state out of structured logs even though an explicit MarshalJSON propagation snapshot includes it.

func (Context) MarshalJSON

func (c Context) MarshalJSON() ([]byte, error)

MarshalJSON is an explicit propagation boundary and includes vendor state. An absent trace should be omitted by its enclosing versioned envelope.

func (Context) Sampled

func (c Context) Sampled() bool

Sampled returns the trace sampling flag.

func (Context) SpanID

func (c Context) SpanID() SpanID

SpanID returns the span identifier.

func (Context) TraceID

func (c Context) TraceID() TraceID

TraceID returns the trace identifier.

func (Context) TraceParent

func (c Context) TraceParent() string

TraceParent emits the supported version 00 representation, retaining only the defined sampled flag. Zero context emits no propagation header.

func (Context) TraceState

func (c Context) TraceState() string

TraceState returns the trace state header value.

func (*Context) UnmarshalJSON

func (c *Context) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a bounded traceparent and optional tracestate object, replacing the receiver only after validation.

func (Context) Validate

func (c Context) Validate() error

Validate checks that trace and span identifiers are nonzero.

func (Context) WithState

func (c Context) WithState(state string) (Context, error)

WithState replaces vendor metadata after validation. Call Child before modifying propagation; opaque vendor data must not enter ordinary logs.

type Sampler

type Sampler func(TraceID) bool

Sampler makes the local sampling decision for a new span from its trace ID. Deciding from the trace ID lets every span of one trace agree.

func RatioSampler

func RatioSampler(ratio float64) (Sampler, error)

RatioSampler samples the given fraction of traces deterministically by the random low 64 bits of the trace ID, so all processes using the same ratio agree for one trace. Zero samples nothing and one samples every trace.

type SpanID

type SpanID [8]byte

SpanID is an eight-byte span identifier.

func (SpanID) IsZero

func (id SpanID) IsZero() bool

IsZero reports whether the span identifier is all zero bytes.

func (SpanID) MarshalText

func (id SpanID) MarshalText() ([]byte, error)

MarshalText encodes the span identifier as lowercase hexadecimal text.

func (SpanID) String

func (id SpanID) String() string

String returns the span identifier as lowercase hexadecimal.

func (*SpanID) UnmarshalText

func (id *SpanID) UnmarshalText(text []byte) error

UnmarshalText decodes exactly 16 lowercase hexadecimal characters, replacing the receiver only on success.

type TraceID

type TraceID [16]byte

TraceID is a sixteen-byte trace identifier.

func (TraceID) IsZero

func (id TraceID) IsZero() bool

IsZero reports whether the trace identifier is all zero bytes.

func (TraceID) MarshalText

func (id TraceID) MarshalText() ([]byte, error)

MarshalText encodes the trace identifier as lowercase hexadecimal text.

func (TraceID) String

func (id TraceID) String() string

String returns the trace identifier as lowercase hexadecimal.

func (*TraceID) UnmarshalText

func (id *TraceID) UnmarshalText(text []byte) error

Text decoding supports owned diagnostics snapshots, including a zero parent ID for root spans. Propagation Context.Validate separately rejects zero IDs.

Jump to

Keyboard shortcuts

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