Overview
Thetrace middleware propagates distributed tracing context (trace ID, span ID) across service boundaries for observability.
Use it when you need:
- Distributed tracing
- Request correlation
- Service observability
Installation
Quick Start
Configuration
Options
| Option | Type | Default | Description |
|---|---|---|---|
TraceHeader | string | "X-Trace-ID" | Trace ID header |
SpanHeader | string | "X-Span-ID" | Span ID header |
ParentHeader | string | "X-Parent-ID" | Parent span header |
Generator | func() string | UUID | ID generator |
Examples
Basic Tracing
W3C Trace Context
Custom Headers
Access Trace Info
Propagate to Downstream
API Reference
Functions
Technical Details
Implementation Architecture
The trace middleware implements distributed tracing through:- Context Propagation: Uses Go’s
context.Contextto store span information throughout the request lifecycle - Span Generation: Creates unique trace and span IDs using cryptographic randomness (16 bytes encoded as hex)
- Header-based Propagation: Extracts and injects trace context via HTTP headers for cross-service communication
Core Components
Span StructureTraceID: Unique identifier for the entire trace across servicesSpanID: Unique identifier for this specific operationParentID: Reference to the parent span for hierarchyStartTime/EndTime: Timing information for duration calculationStatus: Enumerated status (Unset, OK, Error)Tags: Key-value metadata for contextEvents: Timestamped events within the span
crypto/rand to generate 16-byte random values, encoded as 32-character hexadecimal strings, ensuring globally unique identifiers with negligible collision probability.
Span Lifecycle
- Extract or generate trace ID from request headers
- Create new span with generated span ID
- Store span in request context
- Set response headers for downstream propagation
- Execute handler chain
- Calculate duration and set final status
- Invoke OnSpan callback if configured
Collector provides a simple way to aggregate spans for testing or custom backends:
- Thread-safe span collection
- In-memory storage
- Clear method for test isolation
Configuration Defaults
| Field | Default Value | Purpose |
|---|---|---|
ServiceName | "mizu-service" | Identifies the service in traces |
TraceHeader | "X-Trace-ID" | HTTP header for trace ID |
ParentHeader | "X-Parent-ID" | HTTP header for parent span ID |
OnSpan | nil | Optional callback for completed spans |
Sampler | nil | When nil, all requests are traced |
Best Practices
- Use consistent format across services
- Include trace ID in all logs
- Propagate to all downstream calls
- Consider W3C standard for interoperability
Testing
The trace middleware includes comprehensive test coverage for all features:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Basic middleware creation | Creates span with trace ID and span ID for each request |
TestWithOptions_ServiceName | Custom service name configuration | Sets service tag to configured value |
TestWithOptions_PropagateTraceID | Existing trace ID propagation | Preserves incoming trace ID from request headers |
TestWithOptions_ParentID | Parent span ID propagation | Extracts and stores parent span ID from request headers |
TestWithOptions_OnSpan | Span completion callback | Invokes OnSpan callback with completed span including duration |
TestWithOptions_Sampler | Sampling control | Skips span creation when sampler returns false |
TestAddTag | Custom tag addition | Adds custom key-value tags to span |
TestAddEvent | Event logging | Records timestamped events with metadata in span |
TestTraceID | Trace ID retrieval | Returns current trace ID from context |
TestSpanID | Span ID retrieval | Returns current span ID from context |
TestCollector | Span collection | Collects multiple spans and supports clearing |
TestResponseHeader | Response header propagation | Sets X-Trace-ID header in response |
TestHTTPHeaders | Downstream header generation | Generates headers for propagating trace context to downstream services |