Prompt
I’m building distributed tracing into a Go service architecture
Latest observation
Great project. In a Go service architecture, distributed tracing is most useful when you want to follow a request across multiple services, queues, and external dependencies.
Here’s a practical way to think about it:
1) Pick a tracing standard
Use OpenTelemetry. It’s the current standard and works well in Go.
Why:
- vendor-neutral
- supports traces, metrics, logs
- easy export to Jaeger, Grafana Tempo, Honeycomb, Datadog, etc.
2) Define the trace flow
A typical request should:
- enter at an HTTP/gRPC boundary
- create or continue a trace
- propagate context to downstream services
- create spans for key operations
- export spans to your backend
3) Instrument the entry points
For Go HTTP:
- use middleware to extract incoming trace headers
- start a server span
- attach the span to
context.Context
For gRPC:
- use unary and stream interceptors
4) Propagate context everywhere
In Go, pass context.Context through:
- HTTP calls
- DB queries
- message publishing/consuming
- internal function calls that do work
Avoid using global trace state.
5) Add spans around important work
Capture spans for:
- request handling
- outbound HTTP/gRPC calls
- DB queries
- cache calls
- message queue publish/consume
- slow business logic steps
Keep spans meaningful; don’t create a span for every tiny function unless it matters.
6) Use semantic conventions
Name spans and attributes consistently:
- service name
- operation name
- HTTP method/path/status
- peer service
- DB system/query type
- queue/topic name
- error details
7) Export traces
Common setup:
- Go app -> OpenTelemetry SDK -> OTLP exporter -> Collector -> backend
A collector is strongly recommended so you can:
- batch spans
- sample centrally
- route to multiple backends
- avoid tight coupling to one vendor
8) Sample intelligently
At scale, you usually don’t want 100% tracing. Options:
- head sampling in the app
- tail sampling in the collector
- always sample errors and slow requests
9) Correlate logs and traces
Include trace/span IDs in logs so you can jump between them easily.
10) Measure the overhead
Tracing adds:
- CPU
- memory
- network/export cost
Use batching, sensible sampling, and avoid excessive attributes.
Minimal Go setup example
Using OpenTelemetry for HTTP service instrumentation:
import (
"context"
"log"
"net/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"
)
func initTracer(ctx context.Context) func() {
exp, err := otlptracegrpc.New(ctx)
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName("my-service"),
)),
)
otel.SetTracerProvider(tp)
return func() {
_ = tp.Shutdown(ctx)
}
}
Then in your handler:
func handler(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer("my-service").Start(r.Context(), "handleRequest")
defer span.End()
_ = ctx
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
You’d typically add middleware to:
- extract incoming trace context
- create server spans automatically
- instrument outbound calls too
Recommended architecture
Service A
- receives request
- starts/continues trace
- calls Service B / DB / queue
- records span data
Service B
- extracts incoming context
- continues same trace
- records its own spans
Collector
- receives OTLP from all services
- batches/samples/exports to backend
Backend
- Jaeger / Tempo / Honeycomb / Datadog / New Relic, etc.
Common pitfalls
- forgetting to pass
context.Context - creating spans but not exporting them correctly
- naming spans poorly
- tracing too much low-value internal code
- not instrumenting async messaging paths
- sampling too aggressively and losing important traces
- not correlating logs with trace IDs
If you want, I can help with any of these next:
- a full Go OpenTelemetry starter setup
- HTTP + gRPC tracing examples
- trace propagation through Kafka/RabbitMQ
- best practices for span naming and sampling
- adding trace IDs to structured logs