tracing

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 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)

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

func New

func New(sampled bool) (Context, error)

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)

func (Context) IsZero

func (c Context) IsZero() bool

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

func (Context) SpanID

func (c Context) SpanID() SpanID

func (Context) TraceID

func (c Context) TraceID() TraceID

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

func (*Context) UnmarshalJSON

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

func (Context) Validate

func (c Context) Validate() error

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

func (SpanID) IsZero

func (id SpanID) IsZero() bool

func (SpanID) MarshalText

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

func (SpanID) String

func (id SpanID) String() string

func (*SpanID) UnmarshalText

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

type TraceID

type TraceID [16]byte

func (TraceID) IsZero

func (id TraceID) IsZero() bool

func (TraceID) MarshalText

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

func (TraceID) String

func (id TraceID) String() string

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