How to Propagate Trace Context Across Go Services

Use OpenTelemetry libraries to automatically inject and extract trace context headers for distributed tracing across Go services.

Go does not natively propagate trace context across services; you must use an OpenTelemetry-compatible library like go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp to inject and extract context headers. Install the library and wrap your HTTP handlers and clients to automatically handle trace propagation via W3C Trace Context headers.

go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
  1. Import the otelhttp package in your server and client code.
  2. Wrap your HTTP server handler with otelhttp.NewHandler to extract incoming trace context.
  3. Wrap your HTTP client with otelhttp.NewTransport to inject trace context into outgoing requests.
  4. Ensure both services use the same OpenTelemetry exporter to visualize the distributed trace.