trace

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func InjectTraceparent

func InjectTraceparent(ctx context.Context, req *http.Request)

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

func Middleware(next http.Handler) http.Handler

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

func SpanNameFromContext(ctx context.Context) string

SpanNameFromContext extracts the current span name from the context.

func TraceIDFromContext

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext extracts the trace ID from the context. Returns empty string if no trace ID is set.

func WithTraceID

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

WithTraceID returns a context with the given trace ID.

Types

type Span

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

Span represents a named, timed operation within a trace.

func Start

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

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.

func (*Span) End

func (s *Span) End()

End records the span duration and logs it at debug level. Calling End more than once is a no-op.

func (*Span) SetAttr

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

SetAttr adds a key-value attribute to the span. It is a no-op on an ended span, including the shared no-op Span returned by Start when debug logging is disabled.

Jump to

Keyboard shortcuts

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