Getting Started

Hello World examples

Both of these examples assume that Jaeger is running locally via Docker:

  1. docker run -d -p 5775:5775/udp -p 16686:16686 jaegertracing/all-in-one:latest

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

Java

Add the Jaeger libraries to your dependencies -

  1. <dependency>
  2. <groupId>io.jaegertracing</groupId>
  3. <artifactId>jaeger-client</artifactId>
  4. <version>0.32.0</version>
  5. </dependency>
  1. import io.jaegertracing.Configuration;
  2. import io.jaegertracing.Configuration.ReporterConfiguration;
  3. import io.jaegertracing.Configuration.SamplerConfiguration;
  4. import io.jaegertracing.internal.JaegerTracer;
  5. import io.jaegertracing.internal.samplers.ConstSampler;
  6. import io.opentracing.Span;
  7. import io.opentracing.util.GlobalTracer;
  8. ...
  9. SamplerConfiguration samplerConfig = SamplerConfiguration.fromEnv()
  10. .withType(ConstSampler.TYPE)
  11. .withParam(1);
  12. ReporterConfiguration reporterConfig = ReporterConfiguration.fromEnv()
  13. .withLogSpans(true);
  14. Configuration config = new Configuration("helloWorld")
  15. .withSampler(samplerConfig)
  16. .withReporter(reporterConfig);
  17. GlobalTracer.register(config.getTracer());
  18. ...
  19. Span parent = GlobalTracer.get().buildSpan("hello").start();
  20. try (Scope scope = GlobalTracer.get().scopeManager()
  21. .activate(parent)) {
  22. Span child = GlobalTracer.get().buildSpan("world")
  23. .asChildOf(parent).start();
  24. try (Scope scope = GlobalTracer.get().scopeManager()
  25. .activate(child)) {
  26. }
  27. }

Go

  1. import (
  2. "github.com/opentracing/opentracing-go"
  3. "github.com/uber/jaeger-client-go"
  4. "github.com/uber/jaeger-client-go/config"
  5. )
  6. ...
  7. func main() {
  8. ...
  9. cfg := config.Configuration{
  10. Sampler: &config.SamplerConfig{
  11. Type: "const",
  12. Param: 1,
  13. },
  14. Reporter: &config.ReporterConfig{
  15. LogSpans: true,
  16. BufferFlushInterval: 1 * time.Second,
  17. },
  18. }
  19. tracer, closer, err := cfg.New(
  20. "your_service_name",
  21. config.Logger(jaeger.StdLogger),
  22. )
  23. opentracing.SetGlobalTracer(tracer)
  24. defer closer.Close()
  25. someFunction()
  26. ...
  27. }
  28. ...
  29. func someFunction() {
  30. parent := opentracing.GlobalTracer().StartSpan("hello")
  31. defer parent.Finish()
  32. child := opentracing.GlobalTracer().StartSpan(
  33. "world", opentracing.ChildOf(parent.Context()))
  34. defer child.Finish()
  35. }

Self-guided Walkthrough Tutorials

Go

Take OpenTracing for a HotROD Ride involves successive optimizations of a Go-based Ride-on-Demand demonstration service, all informed by tracing data.

Java

MicroDonuts shows the reader how to get tracing instrumentation added to a multi-service app, and includes properly-configured initialization of several OpenTracing-compatible Tracers.

Multiple languages

The OpenTracing Tutorial provides an example combining Go, Java, Python, and Node.js.