How to Use Jaeger for Tracing in Go

Instrument your Go application with the OpenTelemetry SDK to send distributed traces to a Jaeger collector for performance monitoring.

Use the OpenTelemetry Go SDK to instrument your application and send traces to a Jaeger collector. Install the SDK, configure the tracer provider with a Jaeger exporter, and wrap your business logic in spans.

import (
    "go.opentelemetry.io/otel/exporters/jaeger"
    "go.opentelemetry.io/otel/sdk/trace"
)

func initTracer() {
    exporter, _ := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint("http://localhost:14268/api/traces")))
    tp := trace.NewTracerProvider(trace.WithSyncer(exporter))
    otel.SetTracerProvider(tp)
}
  1. Initialize the tracer provider in your main function before starting your application logic.
  2. Create a tracer instance using otel.Tracer("your-service-name").
  3. Start a span for each operation using tracer.Start(ctx, "operation-name").
  4. Call span.End() when the operation completes to send the trace data.