trace

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// HeaderXRequestID is the standard header name for request tracing
	HeaderXRequestID = "X-Request-ID"
	// HeaderTraceParent is the W3C trace context header name
	HeaderTraceParent = "traceparent"
	// HeaderTraceState is the W3C trace context "tracestate" header name
	HeaderTraceState = "tracestate"
)

Variables

This section is empty.

Functions

func EnsureTraceID

func EnsureTraceID(ctx context.Context) string

EnsureTraceID returns an existing trace ID from context or generates a new one

func ExtractFromHeaders

func ExtractFromHeaders(ctx context.Context, headers HeaderAccessor) context.Context

ExtractFromHeaders extracts trace context from transport headers

func GenerateTraceParent

func GenerateTraceParent() string

GenerateTraceParent creates a minimal W3C traceparent header value. Format: version(2)-trace-id(32)-span-id(16)-flags(2), e.g., "00-<32>-<16>-01"

func IDFromContext

func IDFromContext(ctx context.Context) (string, bool)

TraceIDFromContext returns a trace ID from context if present

func InjectIntoHeaders

func InjectIntoHeaders(ctx context.Context, headers HeaderAccessor)

InjectIntoHeaders writes the trace context (X-Request-ID, traceparent, and optionally tracestate) into the given transport headers. Always overwrites any existing values — the trace ID is forced to align with the traceparent so log/metric correlation across services stays consistent.

Historically this routed through an InjectIntoHeadersWithOptions variant that supported a Preserve mode (set-if-missing). Preserve mode had zero callers across the framework, tools, and tests, so the mode-selector API was removed in W4-H. Add it back with a fresh design if a real consumer need surfaces.

func ParentFromContext

func ParentFromContext(ctx context.Context) (string, bool)

TraceParentFromContext returns a traceparent from context if present

func StateFromContext

func StateFromContext(ctx context.Context) (string, bool)

TraceStateFromContext returns a tracestate from context if present

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID adds a trace ID to the context

func WithTraceParent

func WithTraceParent(ctx context.Context, traceParent string) context.Context

WithTraceParent adds a W3C traceparent value to the context

func WithTraceState

func WithTraceState(ctx context.Context, traceState string) context.Context

WithTraceState adds a W3C tracestate value to the context

Types

type HeaderAccessor

type HeaderAccessor interface {
	Get(key string) any
	Set(key string, value any)
}

HeaderAccessor provides a simple interface for reading and writing headers

Jump to

Keyboard shortcuts

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