Quick Start

The examples on this page use Jaeger(an OpenTracing compatible tracer). These examples assume that the Jaeger all-in-one image is running locally via Docker:

$ docker run -d -p 6831:6831/udp -p 16686:16686 jaegertracing/all-in-one:latest

These can easily be adapted to use other OpenTracing-compatible Tracers by adjusting the initialization code to match the particular implementation.

Setting up your tracer

  1. import (
  2. "log"
  3. "os"
  4. opentracing "github.com/opentracing/opentracing-go"
  5. "github.com/uber/jaeger-lib/metrics"
  6. "github.com/uber/jaeger-client-go"
  7. jaegercfg "github.com/uber/jaeger-client-go/config"
  8. jaegerlog "github.com/uber/jaeger-client-go/log"
  9. )
  10. ...
  11. func main() {
  12. // Sample configuration for testing. Use constant sampling to sample every trace
  13. // and enable LogSpan to log every span via configured Logger.
  14. cfg := jaegercfg.Configuration{
  15. ServiceName: "your_service_name",
  16. Sampler: &jaegercfg.SamplerConfig{
  17. Type: jaeger.SamplerTypeConst,
  18. Param: 1,
  19. },
  20. Reporter: &jaegercfg.ReporterConfig{
  21. LogSpans: true,
  22. },
  23. }
  24. // Example logger and metrics factory. Use github.com/uber/jaeger-client-go/log
  25. // and github.com/uber/jaeger-lib/metrics respectively to bind to real logging and metrics
  26. // frameworks.
  27. jLogger := jaegerlog.StdLogger
  28. jMetricsFactory := metrics.NullFactory
  29. // Initialize tracer with a logger and a metrics factory
  30. tracer, closer, err := cfg.NewTracer(
  31. jaegercfg.Logger(jLogger),
  32. jaegercfg.Metrics(jMetricsFactory),
  33. )
  34. // Set the singleton opentracing.Tracer with the Jaeger tracer.
  35. opentracing.SetGlobalTracer(tracer)
  36. defer closer.Close()
  37. // continue main()
  38. }

Start a Trace

  1. import (
  2. opentracing "github.com/opentracing/opentracing-go"
  3. )
  4. ...
  5. tracer := opentracing.GlobalTracer()
  6. span := tracer.StartSpan("say-hello")
  7. println(helloStr)
  8. span.Finish()

Create a Child Span

  1. import (
  2. opentracing "github.com/opentracing/opentracing-go"
  3. )
  4. ...
  5. tracer := opentracing.GlobalTracer()
  6. parentSpan := tracer.StartSpan("parent")
  7. defer parentSpan.Finish()
  8. ...
  9. // Create a Child Span. Note that we're using the ChildOf option.
  10. childSpan := tracer.StartSpan(
  11. "child",
  12. opentracing.ChildOf(parentSpan.Context()),
  13. )
  14. defer childSpan.Finish()

Make an HTTP request

To get traces across service boundaries, we propagate context by injecting the context into http headers. Once the downstream service receives the http request, it must extract the context and continue the trace. (The code example doesn’t handle errors correctly, please don’t do this in production code; this is just an example)

The upstream(client) service:

  1. import (
  2. "net/http"
  3. opentracing "github.com/opentracing/opentracing-go"
  4. "github.com/opentracing/opentracing-go/ext"
  5. )
  6. ...
  7. tracer := opentracing.GlobalTracer()
  8. clientSpan := tracer.StartSpan("client")
  9. defer clientSpan.Finish()
  10. url := "http://localhost:8082/publish"
  11. req, _ := http.NewRequest("GET", url, nil)
  12. // Set some tags on the clientSpan to annotate that it's the client span. The additional HTTP tags are useful for debugging purposes.
  13. ext.SpanKindRPCClient.Set(clientSpan)
  14. ext.HTTPUrl.Set(clientSpan, url)
  15. ext.HTTPMethod.Set(clientSpan, "GET")
  16. // Inject the client span context into the headers
  17. tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
  18. resp, _ := http.DefaultClient.Do(req)

The downstream(server) service:

  1. import (
  2. "log"
  3. "net/http"
  4. opentracing "github.com/opentracing/opentracing-go"
  5. "github.com/opentracing/opentracing-go/ext"
  6. )
  7. func main() {
  8. // Tracer initialization, etc.
  9. ...
  10. http.HandleFunc("/publish", func(w http.ResponseWriter, r *http.Request) {
  11. // Extract the context from the headers
  12. spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
  13. serverSpan := tracer.StartSpan("server", ext.RPCServerOption(spanCtx))
  14. defer serverSpan.Finish()
  15. })
  16. log.Fatal(http.ListenAndServe(":8082", nil))
  17. }

View your trace

If you have Jaeger all-in-one running, you can view your trace at localhost:16686.

Link to GO walkthroughs / tutorials