Tracing
Tracing
Section titled “Tracing”go-zero uses OpenTelemetry for distributed tracing, automatically creating spans for every HTTP request and gRPC call.
Configuration
Section titled “Configuration”Telemetry: Name: order-api Endpoint: http://jaeger:14268/api/traces Sampler: 1.0 # 0.0–1.0; use 0.1 in production Batcher: jaeger # "jaeger" | "zipkin" | "otlpgrpc" | "otlphttp"What Is Traced Automatically
Section titled “What Is Traced Automatically”- Every inbound HTTP request → span with URL, method, status code
- Every outbound zrpc call → child span with service/method
- SQL queries (via
sqlx) → child span with query string
Custom Spans
Section titled “Custom Spans”import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute")
func (l *OrderLogic) processPayment(amount int64) error { tracer := otel.Tracer("order-service") _, span := tracer.Start(l.ctx, "process-payment") defer span.End()
span.SetAttributes( attribute.Int64("amount", amount), attribute.String("currency", "USD"), )
if err := chargeCard(amount); err != nil { span.RecordError(err) return err } return nil}Backends
Section titled “Backends”| Backend | Batcher value | Notes |
|---|---|---|
| Jaeger | jaeger | All-in-one image available |
| Zipkin | zipkin | Lightweight alternative |
| OTLP gRPC | otlpgrpc | OpenTelemetry Collector |
| OTLP HTTP | otlphttp | OpenTelemetry Collector |