Documentation
¶
Overview ¶
Package trace provides lightweight distributed tracing via context-propagated trace IDs and span tracking. It is dependency-free and designed to be upgradeable to OpenTelemetry if full OTLP export is needed later.
Usage:
// In an HTTP middleware, start a trace from the request: ctx = trace.WithTraceID(r.Context(), trace.NewTraceID()) // In a handler or engine pipeline, start a span: ctx, span := trace.Start(ctx, "modbus.Read") defer span.End() // logs duration and records the span // The trace ID is available via: tid := trace.TraceIDFromContext(ctx)
Index ¶
- func InjectTraceparent(ctx context.Context, req *http.Request)
- func Middleware(next http.Handler) http.Handler
- func NewSpanID() string
- func NewTraceID() string
- func SpanNameFromContext(ctx context.Context) string
- func TraceIDFromContext(ctx context.Context) string
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type Span
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InjectTraceparent ¶
InjectTraceparent sets the traceparent header on an outbound HTTP request so that the trace context propagates across service boundaries. It uses the trace ID from the context and generates a new parent span ID for this hop. If no trace ID is in the context, the header is not set (the caller's trace context is not invented).
func Middleware ¶
Middleware injects a trace ID into the request context. It implements W3C Trace Context propagation by parsing the incoming `traceparent` header (format: version-trace_id-parent_id-trace_flags). If a valid traceparent is present, its trace-id is reused so that this service's logs and spans are correlated with the upstream caller's trace. If no traceparent is present, it falls back to the chi request ID, and if that is also empty, generates a new W3C-compliant trace ID.
The trace ID is available via trace.TraceIDFromContext(r.Context()) in downstream handlers and is automatically included in slog records when using a context-aware logger.
Outbound HTTP requests can propagate the trace context by using trace.InjectTraceparent(ctx, req) before sending the request.
func NewSpanID ¶
func NewSpanID() string
NewSpanID generates an 8-byte hex-encoded span ID (16 chars), matching W3C Trace Context format for the parent-id field.
func NewTraceID ¶
func NewTraceID() string
NewTraceID generates a 16-byte hex-encoded trace ID (32 chars), matching W3C Trace Context format.
func SpanNameFromContext ¶
SpanNameFromContext extracts the current span name from the context.
func TraceIDFromContext ¶
TraceIDFromContext extracts the trace ID from the context. Returns empty string if no trace ID is set.
Types ¶
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span represents a named, timed operation within a trace.
func Start ¶
Start creates a new span within the trace identified by the context. If no trace ID is in the context, a new one is generated. The returned context carries both the trace ID and span name.
When slog's debug level is disabled (the default), Start short-circuits: it returns the original context unchanged together with the shared no-op Span, avoiding the *Span and spanKey valueCtx allocations. The trace ID is only consumed by the debug log emitted on End, so propagating it when debug logging is off would be wasted work on the readFromDriver hot path.