Skip to main content

Overview

The otel middleware integrates with OpenTelemetry for distributed tracing, metrics, and observability. Use it when you need:
  • Distributed tracing
  • OpenTelemetry integration
  • Observability platforms (Jaeger, Zipkin, etc.)

Installation

Quick Start

Configuration

Options

Examples

Basic Setup

With Jaeger

Custom Spans

Propagate to Downstream

API Reference

Functions

Technical Details

Architecture

The otel middleware is a lightweight OpenTelemetry-compatible implementation that provides distributed tracing without external dependencies. It implements the OpenTelemetry specification for trace context propagation.

Components

SpanContext: Holds the trace context with TraceID (16 bytes), SpanID (8 bytes), TraceFlags, and optional TraceState. Span: Represents a single operation with metadata including:
  • Name (HTTP method + path)
  • Parent/child relationships
  • Start/end timestamps
  • Status (Unset, OK, Error)
  • Attributes (key-value pairs)
  • Events (timestamped logs)
  • Links (connections to other spans)
SpanProcessor: Interface for processing completed spans. Implementations include:
  • InMemoryProcessor: Stores spans in memory for testing
  • PrintProcessor: Prints spans to stdout
  • Custom processors can export to external systems

Propagation Formats

W3C Trace Context (default):
  • Header: Traceparent: 00-{traceId}-{spanId}-{flags}
  • Optional: Tracestate for vendor-specific data
  • Format: version-traceId-spanId-flags
B3 Propagation:
  • Single header: B3: {traceId}-{spanId}-{sampled}
  • Multi-header: X-B3-Traceid, X-B3-Spanid, X-B3-Sampled
  • Compatible with Zipkin and other B3-based systems

ID Generation

  • TraceID: 128-bit (16 bytes) cryptographically random hex string
  • SpanID: 64-bit (8 bytes) cryptographically random hex string
  • Generated using crypto/rand for uniqueness

Request Flow

  1. Extract parent context from incoming request headers
  2. Create new span with inherited TraceID or generate new one
  3. Generate new SpanID for current span
  4. Add default HTTP attributes (method, URL, status, etc.)
  5. Call OnStart hook if configured
  6. Inject trace context into response headers
  7. Store span in request context
  8. Execute handler
  9. Set final status and attributes based on response
  10. Call OnEnd hook and SpanProcessor

Thread Safety

  • Span methods use mutex locks for concurrent access
  • InMemoryProcessor is thread-safe with mutex protection
  • Safe for use with concurrent requests

Best Practices

  • Use consistent service names
  • Add meaningful span attributes
  • Propagate context to all calls
  • Sample appropriately in production

Testing

The otel middleware includes comprehensive test coverage for all features: