From 5f8bf254be4fbe3f86bd4fca3074f4e01df16221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Tue, 16 Oct 2018 14:47:22 +0200 Subject: [PATCH] Updated tutorial to latest versions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Juraci Paixão Kröhling --- .../02-create-trace.md | 7 +- .../03-initialize-real-tracer.md | 78 +++++++----------- .../04-annotate-trace-with-tags.md | 34 ++++---- opentracing-tutorial-lesson01/env-init.sh | 8 +- .../01-tracing-individual-functions.md | 55 +++++++++---- opentracing-tutorial-lesson02/env-init.sh | 8 +- .../01-hello-world-microservices-app.md | 38 +++++---- .../04-instrumenting-the-servers.md | 10 +-- .../05-take-it-for-a-spin.md | 79 ++++++++++--------- opentracing-tutorial-lesson03/env-init.sh | 8 +- .../01-set-baggage-in-the-client.md | 2 - opentracing-tutorial-lesson04/env-init.sh | 8 +- 12 files changed, 164 insertions(+), 171 deletions(-) diff --git a/opentracing-tutorial-lesson01/02-create-trace.md b/opentracing-tutorial-lesson01/02-create-trace.md index aeb4aac..9c1b6f1 100644 --- a/opentracing-tutorial-lesson01/02-create-trace.md +++ b/opentracing-tutorial-lesson01/02-create-trace.md @@ -5,18 +5,19 @@ Let's create a trace that consists of just a single span. To do that we need an
package lesson01.exercise;
 
 import io.opentracing.Span;
+import io.opentracing.Tracer;
 import io.opentracing.util.GlobalTracer;
 
 public class Hello {
 
-    private final io.opentracing.Tracer tracer;
+    private final Tracer tracer;
 
-    private Hello(io.opentracing.Tracer tracer) {
+    private Hello(Tracer tracer) {
         this.tracer = tracer;
     }
 
     private void sayHello(String helloTo) {
-        Span span = tracer.buildSpan("say-hello").startManual();
+        Span span = tracer.buildSpan("say-hello").start();
 
         String helloStr = String.format("Hello, %s!", helloTo);
         System.out.println(helloStr);
diff --git a/opentracing-tutorial-lesson01/03-initialize-real-tracer.md b/opentracing-tutorial-lesson01/03-initialize-real-tracer.md
index 90f8b5b..d10fcbf 100644
--- a/opentracing-tutorial-lesson01/03-initialize-real-tracer.md
+++ b/opentracing-tutorial-lesson01/03-initialize-real-tracer.md
@@ -2,38 +2,29 @@ Let's create an instance of a real tracer, such as Jaeger (http://github.com/ube
 
 ```xml
 
-    com.uber.jaeger
-    jaeger-core
-    0.26.0
+    io.jaegertracing
+    jaeger-client
+    0.32.0
 
 ```
 
 We need to add some imports:
 
 
-import com.uber.jaeger.Configuration;
-import com.uber.jaeger.Configuration.ReporterConfiguration;
-import com.uber.jaeger.Configuration.SamplerConfiguration;
-import com.uber.jaeger.Tracer;
-import com.uber.jaeger.samplers.ConstSampler;
+import io.jaegertracing.Configuration;
+import io.jaegertracing.Configuration.ReporterConfiguration;
+import io.jaegertracing.Configuration.SamplerConfiguration;
+import io.jaegertracing.internal.JaegerTracer;
 
And we define a helper function that will create a tracer.
-public static com.uber.jaeger.Tracer initTracer(String service) {
-    SamplerConfiguration samplerConfig = SamplerConfiguration.fromEnv()
-            .withType(ConstSampler.TYPE)
-            .withParam(1);
-
-    ReporterConfiguration reporterConfig = ReporterConfiguration.fromEnv()
-            .withLogSpans(true);
-
-    Configuration config = new Configuration(service)
-            .withSampler(samplerConfig)
-            .withReporter(reporterConfig);
-
-    return (com.uber.jaeger.Tracer) config.getTracer();
+public static JaegerTracer initTracer(String service) {
+    SamplerConfiguration samplerConfig = new SamplerConfiguration().fromEnv().withType("const").withParam(1);
+    ReporterConfiguration reporterConfig = new ReporterConfiguration().fromEnv().withLogSpans(true);
+    Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
+    return config.getTracer();
 }
 
@@ -42,7 +33,6 @@ To use this instance, let's change the main function:
 Tracer tracer = initTracer("hello-world");
 new Hello(tracer).sayHello(helloTo);
-tracer.close();
 
Note that we are passing a string `hello-world` to the init method. It is used to mark all spans emitted by the tracer as originating from a `hello-world` service. @@ -51,23 +41,25 @@ This is how our `Hello` class looks like now:
package lesson01.exercise;
 
-import com.uber.jaeger.Configuration;
-import com.uber.jaeger.Configuration.ReporterConfiguration;
-import com.uber.jaeger.Configuration.SamplerConfiguration;
-import com.uber.jaeger.Tracer;
-import com.uber.jaeger.samplers.ConstSampler;
 import io.opentracing.Span;
+import io.opentracing.Tracer;
+import io.opentracing.util.GlobalTracer;
+
+import io.jaegertracing.Configuration;
+import io.jaegertracing.Configuration.ReporterConfiguration;
+import io.jaegertracing.Configuration.SamplerConfiguration;
+import io.jaegertracing.internal.JaegerTracer;
 
 public class Hello {
 
-    private final io.opentracing.Tracer tracer;
+    private final Tracer tracer;
 
-    private Hello(io.opentracing.Tracer tracer) {
+    private Hello(Tracer tracer) {
         this.tracer = tracer;
     }
 
     private void sayHello(String helloTo) {
-        Span span = tracer.buildSpan("say-hello").startManual();
+        Span span = tracer.buildSpan("say-hello").start();
 
         String helloStr = String.format("Hello, %s!", helloTo);
         System.out.println(helloStr);
@@ -75,36 +67,26 @@ public class Hello {
         span.finish();
     }
 
-    public static com.uber.jaeger.Tracer initTracer(String service) {
-        SamplerConfiguration samplerConfig = SamplerConfiguration.fromEnv()
-                .withType(ConstSampler.TYPE)
-                .withParam(1);
-
-        ReporterConfiguration reporterConfig = ReporterConfiguration.fromEnv()
-                .withLogSpans(true);
-
-        Configuration config = new Configuration(service)
-                .withSampler(samplerConfig)
-                .withReporter(reporterConfig);
-
-        return (com.uber.jaeger.Tracer) config.getTracer();
-    }
-
     public static void main(String[] args) {
         if (args.length != 1) {
             throw new IllegalArgumentException("Expecting one argument");
         }
         String helloTo = args[0];
-
         Tracer tracer = initTracer("hello-world");
         new Hello(tracer).sayHello(helloTo);
-        tracer.close();
+    }
 
+    public static JaegerTracer initTracer(String service) {
+        SamplerConfiguration samplerConfig = new SamplerConfiguration().fromEnv().withType("const").withParam(1);
+        ReporterConfiguration reporterConfig = new ReporterConfiguration().fromEnv().withLogSpans(true);
+        Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
+        return config.getTracer();
     }
+
 }
NOTE: as this scenario runs on Docker, we need to specify the Agent's hostname to the Jaeger client. We can do that by setting this: `export JAEGER_ENDPOINT=http://host01:14268/api/traces`{{execute}}. Running the program now, we see a span logged. Try it out: `./run.sh lesson01.exercise.Hello Bryan`{{execute}}. -Check also the [Jaeger UI](https://[[HOST_SUBDOMAIN]]-16686-[[KATACODA_HOST]].environments.katacoda.com/search?service=hello-world) for the newly created trace! \ No newline at end of file +Check also the [Jaeger UI](https://[[HOST_SUBDOMAIN]]-16686-[[KATACODA_HOST]].environments.katacoda.com/search?service=hello-world) for the newly created trace! diff --git a/opentracing-tutorial-lesson01/04-annotate-trace-with-tags.md b/opentracing-tutorial-lesson01/04-annotate-trace-with-tags.md index eab39a2..2755e93 100644 --- a/opentracing-tutorial-lesson01/04-annotate-trace-with-tags.md +++ b/opentracing-tutorial-lesson01/04-annotate-trace-with-tags.md @@ -12,7 +12,7 @@ Using Tags In the case of `Hello Bryan`, the string `"Bryan"` is a good candidate for a span tag, since it applies to the whole span and not to a particular moment in time. We can record it like this:
-Span span = tracer.buildSpan("say-hello").startManual();
+Span span = tracer.buildSpan("say-hello").start();
 span.setTag("hello-to", helloTo);
 
@@ -46,11 +46,15 @@ If you run the program with these changes, then find the trace in the UI and exp
package lesson01.exercise;
 
 import io.opentracing.Span;
+import io.opentracing.Tracer;
+import io.opentracing.util.GlobalTracer;
+
+import io.jaegertracing.Configuration;
+import io.jaegertracing.Configuration.ReporterConfiguration;
+import io.jaegertracing.Configuration.SamplerConfiguration;
+import io.jaegertracing.internal.JaegerTracer;
+
 import com.google.common.collect.ImmutableMap;
-import com.uber.jaeger.Configuration;
-import com.uber.jaeger.Configuration.ReporterConfiguration;
-import com.uber.jaeger.Configuration.SamplerConfiguration;
-import com.uber.jaeger.Tracer;
 
 public class Hello {
 
@@ -61,9 +65,9 @@ public class Hello {
     }
 
     private void sayHello(String helloTo) {
-        Span span = tracer.buildSpan("say-hello").startManual();
+        Span span = tracer.buildSpan("say-hello").start();
         span.setTag("hello-to", helloTo);
-        
+
         String helloStr = String.format("Hello, %s!", helloTo);
         span.log(ImmutableMap.of("event", "string-format", "value", helloStr));
 
@@ -78,21 +82,19 @@ public class Hello {
             throw new IllegalArgumentException("Expecting one argument");
         }
         String helloTo = args[0];
-
-        com.uber.jaeger.Tracer tracer = initTracer("hello-world");
+        Tracer tracer = initTracer("hello-world");
         new Hello(tracer).sayHello(helloTo);
-        tracer.close();
     }
 
-    public static com.uber.jaeger.Tracer initTracer(String service) {
-        SamplerConfiguration samplerConfig = new SamplerConfiguration("const", 1);
-        ReporterConfiguration reporterConfig = ReporterConfiguration.fromEnv();
-        Configuration config = new Configuration(service, samplerConfig, reporterConfig);
-        return (com.uber.jaeger.Tracer) config.getTracer();
+    public static JaegerTracer initTracer(String service) {
+        SamplerConfiguration samplerConfig = new SamplerConfiguration().fromEnv().withType("const").withParam(1);
+        ReporterConfiguration reporterConfig = new ReporterConfiguration().fromEnv().withLogSpans(true);
+        Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
+        return config.getTracer();
     }
 }
Try it out: `./run.sh lesson01.exercise.Hello Bryan`{{execute}} [semantic-conventions]: https://github.com/opentracing/specification/blob/master/semantic_conventions.md -[google-logging]: https://www.google.com/search?q=structured-logging \ No newline at end of file +[google-logging]: https://www.google.com/search?q=structured-logging diff --git a/opentracing-tutorial-lesson01/env-init.sh b/opentracing-tutorial-lesson01/env-init.sh index 338bc13..e86c760 100755 --- a/opentracing-tutorial-lesson01/env-init.sh +++ b/opentracing-tutorial-lesson01/env-init.sh @@ -1,10 +1,6 @@ git clone https://github.com/yurishkuro/opentracing-tutorial \ && docker run -d \ - -p5775:5775/udp \ - -p6831:6831/udp \ - -p6832:6832/udp \ - -p5778:5778 \ -p16686:16686 \ -p14268:14268 \ - -p9411:9411 \ - jaegertracing/all-in-one:1.3 + jaegertracing/all-in-one:1.7 \ + --log-level=debug diff --git a/opentracing-tutorial-lesson02/01-tracing-individual-functions.md b/opentracing-tutorial-lesson02/01-tracing-individual-functions.md index d7f7f56..0362fb0 100644 --- a/opentracing-tutorial-lesson02/01-tracing-individual-functions.md +++ b/opentracing-tutorial-lesson02/01-tracing-individual-functions.md @@ -3,9 +3,15 @@ In Lesson 1 we wrote a program that creates a trace that consists of a single sp
package lesson02.exercise;
 
 import io.opentracing.Span;
+import io.opentracing.Tracer;
+import io.opentracing.util.GlobalTracer;
+
+import io.jaegertracing.Configuration;
+import io.jaegertracing.Configuration.ReporterConfiguration;
+import io.jaegertracing.Configuration.SamplerConfiguration;
+import io.jaegertracing.internal.JaegerTracer;
+
 import com.google.common.collect.ImmutableMap;
-import com.uber.jaeger.Tracer;
-import lib.Tracing;
 
 public class Hello {
 
@@ -16,7 +22,7 @@ public class Hello {
     }
 
     private void sayHello(String helloTo) {
-        Span span = tracer.buildSpan("say-hello").startManual();
+        Span span = tracer.buildSpan("say-hello").start();
         span.setTag("hello-to", helloTo);
 
         String helloStr = String.format("Hello, %s!", helloTo);
@@ -33,9 +39,15 @@ public class Hello {
             throw new IllegalArgumentException("Expecting one argument");
         }
         String helloTo = args[0];
-        Tracer tracer = Tracing.init("hello-world");
+        Tracer tracer = initTracer("hello-world");
         new Hello(tracer).sayHello(helloTo);
-        tracer.close();
+    }
+
+    public static JaegerTracer initTracer(String service) {
+        SamplerConfiguration samplerConfig = new SamplerConfiguration().fromEnv().withType("const").withParam(1);
+        ReporterConfiguration reporterConfig = new ReporterConfiguration().fromEnv().withLogSpans(true);
+        Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
+        return config.getTracer();
     }
 }
@@ -66,8 +78,8 @@ private void printHello(Span span, String helloStr) { Of course, this does not change the outcome. What we really want to do is to wrap each function into its own span.
-private String formatString(Span rootSpan, String helloTo) {
-    Span span = tracer.buildSpan("formatString").startManual();
+private  String formatString(Span rootSpan, String helloTo) {
+    Span span = tracer.buildSpan("formatString").start();
     try {
         String helloStr = String.format("Hello, %s!", helloTo);
         span.log(ImmutableMap.of("event", "string-format", "value", helloStr));
@@ -78,7 +90,7 @@ private String formatString(Span rootSpan, String helloTo) {
 }
 
 private void printHello(Span rootSpan, String helloStr) {
-    Span span = tracer.buildSpan("printHello").startManual();
+    Span span = tracer.buildSpan("printHello").start();
     try {
         System.out.println(helloStr);
         span.log(ImmutableMap.of("event", "println"));
@@ -107,11 +119,14 @@ For reference, here's how our final code looks like:
 
package lesson02.exercise;
 
 import io.opentracing.Span;
-import io.opentracing.util.GlobalTracer;
+import io.opentracing.Tracer;
+
+import io.jaegertracing.Configuration;
+import io.jaegertracing.Configuration.ReporterConfiguration;
+import io.jaegertracing.Configuration.SamplerConfiguration;
+import io.jaegertracing.internal.JaegerTracer;
 
 import com.google.common.collect.ImmutableMap;
-import com.uber.jaeger.Tracer;
-import lib.Tracing;
 
 public class Hello {
 
@@ -122,7 +137,7 @@ public class Hello {
     }
 
     private void sayHello(String helloTo) {
-        Span span = tracer.buildSpan("say-hello").startManual();
+        Span span = tracer.buildSpan("say-hello").start();
         span.setTag("hello-to", helloTo);
 
         String helloStr = formatString(span, helloTo);
@@ -131,8 +146,8 @@ public class Hello {
         span.finish();
     }
 
-    private  String formatString(Span rootSpan, String helloTo) {
-        Span span = tracer.buildSpan("formatString").asChildOf(rootSpan).startManual();
+    private String formatString(Span rootSpan, String helloTo) {
+        Span span = tracer.buildSpan("formatString").start();
         try {
             String helloStr = String.format("Hello, %s!", helloTo);
             span.log(ImmutableMap.of("event", "string-format", "value", helloStr));
@@ -143,7 +158,7 @@ public class Hello {
     }
 
     private void printHello(Span rootSpan, String helloStr) {
-        Span span = tracer.buildSpan("printHello").asChildOf(rootSpan).startManual();
+        Span span = tracer.buildSpan("printHello").start();
         try {
             System.out.println(helloStr);
             span.log(ImmutableMap.of("event", "println"));
@@ -157,8 +172,14 @@ public class Hello {
             throw new IllegalArgumentException("Expecting one argument");
         }
         String helloTo = args[0];
-        Tracer tracer = Tracing.init("hello-world");
+        Tracer tracer = initTracer("hello-world");
         new Hello(tracer).sayHello(helloTo);
-        tracer.close();
+    }
+
+    public static JaegerTracer initTracer(String service) {
+        SamplerConfiguration samplerConfig = new SamplerConfiguration().fromEnv().withType("const").withParam(1);
+        ReporterConfiguration reporterConfig = new ReporterConfiguration().fromEnv().withLogSpans(true);
+        Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
+        return config.getTracer();
     }
 }
diff --git a/opentracing-tutorial-lesson02/env-init.sh b/opentracing-tutorial-lesson02/env-init.sh index 338bc13..e86c760 100755 --- a/opentracing-tutorial-lesson02/env-init.sh +++ b/opentracing-tutorial-lesson02/env-init.sh @@ -1,10 +1,6 @@ git clone https://github.com/yurishkuro/opentracing-tutorial \ && docker run -d \ - -p5775:5775/udp \ - -p6831:6831/udp \ - -p6832:6832/udp \ - -p5778:5778 \ -p16686:16686 \ -p14268:14268 \ - -p9411:9411 \ - jaegertracing/all-in-one:1.3 + jaegertracing/all-in-one:1.7 \ + --log-level=debug diff --git a/opentracing-tutorial-lesson03/01-hello-world-microservices-app.md b/opentracing-tutorial-lesson03/01-hello-world-microservices-app.md index 9d72c0c..5097e1e 100644 --- a/opentracing-tutorial-lesson03/01-hello-world-microservices-app.md +++ b/opentracing-tutorial-lesson03/01-hello-world-microservices-app.md @@ -2,17 +2,19 @@ We'll start this lesson based on where we left the last lesson, plus a small ref
package lesson03.exercise;
 
+import java.io.IOException;
+
 import com.google.common.collect.ImmutableMap;
-import com.uber.jaeger.Tracer;
+
+import io.jaegertracing.internal.JaegerTracer;
 import io.opentracing.Scope;
+import io.opentracing.Tracer;
 import lib.Tracing;
 import okhttp3.HttpUrl;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.Response;
 
-import java.io.IOException;
-
 public class Hello {
 
     private final Tracer tracer;
@@ -67,28 +69,29 @@ public class Hello {
         if (args.length != 1) {
             throw new IllegalArgumentException("Expecting one argument");
         }
+
         String helloTo = args[0];
-        Tracer tracer = Tracing.init("hello-world");
-        new Hello(tracer).sayHello(helloTo);
-        tracer.close();
-        System.exit(0); // okhttpclient sometimes hangs maven otherwise
+        try (JaegerTracer tracer = Tracing.init("hello-world")) {
+            new Hello(tracer).sayHello(helloTo);
+        }
     }
-}
+} +
Let's add a `formatter` service, which is a Dropwizard-based HTTP server that responds to a request like `GET 'http://localhost:8081/format?helloTo=Bryan'` and returns `"Hello, Bryan!"` string
package lesson03.exercise;
 
-import io.dropwizard.Application;
-import io.dropwizard.Configuration;
-import io.dropwizard.setup.Environment;
-
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 
+import io.dropwizard.Application;
+import io.dropwizard.Configuration;
+import io.dropwizard.setup.Environment;
+
 public class Formatter extends Application< Configuration> {
 
     @Path("/format")
@@ -118,16 +121,16 @@ And finally, a `publisher` service, that is another HTTP server that responds to
 
 
package lesson03.exercise;
 
-import io.dropwizard.Application;
-import io.dropwizard.Configuration;
-import io.dropwizard.setup.Environment;
-
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 
+import io.dropwizard.Application;
+import io.dropwizard.Configuration;
+import io.dropwizard.setup.Environment;
+
 public class Publisher extends Application< Configuration> {
 
     @Path("/publish")
@@ -151,7 +154,8 @@ public class Publisher extends Application< Configuration> {
         System.setProperty("dw.server.adminConnectors[0].port", "9082");
         new Publisher().run(args);
     }
-}
+} +
With all that in place, let's switch to the Java version of the tutorial: `cd opentracing-tutorial/java`{{execute}}. diff --git a/opentracing-tutorial-lesson03/04-instrumenting-the-servers.md b/opentracing-tutorial-lesson03/04-instrumenting-the-servers.md index 1946962..7986efe 100644 --- a/opentracing-tutorial-lesson03/04-instrumenting-the-servers.md +++ b/opentracing-tutorial-lesson03/04-instrumenting-the-servers.md @@ -31,13 +31,13 @@ import lib.Tracing; #### Extract the span context from the incoming request using `tracer.extract` -First, let's add a helper function on the Hello class: +First, let's add a helper function on the Tracing class:
 public static Scope startServerSpan(Tracer tracer, javax.ws.rs.core.HttpHeaders httpHeaders, String operationName) {
     // format the headers for extraction
-    MultivaluedMap<String, String> rawHeaders = httpHeaders.getRequestHeaders();
-    final HashMap<String, String> headers = new HashMap<String, String>();
+    MultivaluedMap< String, String> rawHeaders = httpHeaders.getRequestHeaders();
+    final HashMap< String, String> headers = new HashMap< String, String>();
     for (String key : rawHeaders.keySet()) {
         headers.put(key, rawHeaders.get(key).get(0));
     }
@@ -59,6 +59,7 @@ public static Scope startServerSpan(Tracer tracer, javax.ws.rs.core.HttpHeaders
 
 And import the newly used classes:
 
+import io.opentracing.Scope;
 import io.opentracing.SpanContext;
 import io.opentracing.propagation.Format;
 import io.opentracing.propagation.TextMapExtractAdapter;
@@ -74,7 +75,7 @@ Now change the `FormatterResource` handler method to use `startServerSpan`:
 
 @GET
 public String format(@QueryParam("helloTo") String helloTo, @Context HttpHeaders httpHeaders) {
-    try (Scope scope = Tracing.startServerSpan(tracer, httpHeaders, "format")) {
+    try (Scope scope = startServerSpan(tracer, httpHeaders, "format")) {
         String helloStr = String.format("Hello, %s!", helloTo);
         scope.span().log(ImmutableMap.of("event", "string-format", "value", helloStr));
         return helloStr;
@@ -84,7 +85,6 @@ public String format(@QueryParam("helloTo") String helloTo, @Context HttpHeaders
 
 And import the newly used classes:
 
-import io.opentracing.Scope;
 import com.google.common.collect.ImmutableMap;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
diff --git a/opentracing-tutorial-lesson03/05-take-it-for-a-spin.md b/opentracing-tutorial-lesson03/05-take-it-for-a-spin.md
index 8681a2e..f8a50a0 100644
--- a/opentracing-tutorial-lesson03/05-take-it-for-a-spin.md
+++ b/opentracing-tutorial-lesson03/05-take-it-for-a-spin.md
@@ -3,9 +3,13 @@ As before, first run the `formatter` and `publisher` apps in separate terminals.
 For reference, here's how client looks like:
 
package lesson03.exercise;
 
+import java.io.IOException;
+
 import com.google.common.collect.ImmutableMap;
-import com.uber.jaeger.Tracer;
+
+import io.jaegertracing.internal.JaegerTracer;
 import io.opentracing.Scope;
+import io.opentracing.Tracer;
 import io.opentracing.propagation.Format;
 import io.opentracing.tag.Tags;
 import lib.Tracing;
@@ -14,8 +18,6 @@ import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.Response;
 
-import java.io.IOException;
-
 public class Hello {
 
     private final Tracer tracer;
@@ -76,21 +78,34 @@ public class Hello {
         if (args.length != 1) {
             throw new IllegalArgumentException("Expecting one argument");
         }
+
         String helloTo = args[0];
-        Tracer tracer = Tracing.init("hello-world");
-        new Hello(tracer).sayHello(helloTo);
-        tracer.close();
-        System.exit(0); // okhttpclient sometimes hangs maven otherwise
+        try (JaegerTracer tracer = Tracing.init("hello-world")) {
+            new Hello(tracer).sayHello(helloTo);
+        }
     }
 }
At this point, our Formatter is like this:
package lesson03.exercise;
 
+import java.util.HashMap;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
 import com.google.common.collect.ImmutableMap;
+
 import io.dropwizard.Application;
 import io.dropwizard.Configuration;
 import io.dropwizard.setup.Environment;
+import io.jaegertracing.internal.JaegerTracer;
 import io.opentracing.Scope;
 import io.opentracing.SpanContext;
 import io.opentracing.Tracer;
@@ -99,16 +114,6 @@ import io.opentracing.propagation.TextMapExtractAdapter;
 import io.opentracing.tag.Tags;
 import lib.Tracing;
 
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import java.util.HashMap;
-
 public class Formatter extends Application< Configuration> {
     private final Tracer tracer;
 
@@ -119,16 +124,14 @@ public class Formatter extends Application< Configuration> {
     @Path("/format")
     @Produces(MediaType.TEXT_PLAIN)
     public class FormatterResource {
-
         @GET
         public String format(@QueryParam("helloTo") String helloTo, @Context HttpHeaders httpHeaders) {
-            try (Scope scope = startServerSpan(tracer, httpHeaders, "format")) {
+            try (Scope scope = Tracing.startServerSpan(tracer, httpHeaders, "format")) {
                 String helloStr = String.format("Hello, %s!", helloTo);
                 scope.span().log(ImmutableMap.of("event", "string-format", "value", helloStr));
                 return helloStr;
             }
-        }
-
+        }        
     }
 
     @Override
@@ -151,7 +154,7 @@ public class Formatter extends Application< Configuration> {
         for (String key : rawHeaders.keySet()) {
             headers.put(key, rawHeaders.get(key).get(0));
         }
-
+    
         Tracer.SpanBuilder spanBuilder;
         try {
             SpanContext parentSpan = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headers));
@@ -164,13 +167,23 @@ public class Formatter extends Application< Configuration> {
             spanBuilder = tracer.buildSpan(operationName);
         }
         return spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).startActive(true);
-    }
+    }  
 }
And our Publisher:
package lesson03.exercise;
 
-import com.google.common.collect.ImmutableMap;
+import java.util.HashMap;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
 import io.dropwizard.Application;
 import io.dropwizard.Configuration;
 import io.dropwizard.setup.Environment;
@@ -182,18 +195,7 @@ import io.opentracing.propagation.TextMapExtractAdapter;
 import io.opentracing.tag.Tags;
 import lib.Tracing;
 
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import java.util.HashMap;
-
 public class Publisher extends Application< Configuration> {
-
     private final Tracer tracer;
 
     private Publisher(Tracer tracer) {
@@ -203,12 +205,10 @@ public class Publisher extends Application< Configuration> {
     @Path("/publish")
     @Produces(MediaType.TEXT_PLAIN)
     public class PublisherResource {
-
         @GET
         public String format(@QueryParam("helloStr") String helloStr, @Context HttpHeaders httpHeaders) {
             try (Scope scope = startServerSpan(tracer, httpHeaders, "publish")) {
                 System.out.println(helloStr);
-                scope.span().log(ImmutableMap.of("event", "println", "value", helloStr));
                 return "published";
             }
         }
@@ -222,6 +222,7 @@ public class Publisher extends Application< Configuration> {
     public static void main(String[] args) throws Exception {
         System.setProperty("dw.server.applicationConnectors[0].port", "8082");
         System.setProperty("dw.server.adminConnectors[0].port", "9082");
+
         Tracer tracer = Tracing.init("publisher");
         new Publisher(tracer).run(args);
     }
@@ -233,7 +234,7 @@ public class Publisher extends Application< Configuration> {
         for (String key : rawHeaders.keySet()) {
             headers.put(key, rawHeaders.get(key).get(0));
         }
-
+    
         Tracer.SpanBuilder spanBuilder;
         try {
             SpanContext parentSpan = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headers));
@@ -246,7 +247,7 @@ public class Publisher extends Application< Configuration> {
             spanBuilder = tracer.buildSpan(operationName);
         }
         return spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).startActive(true);
-    }
+    }  
 }
Formatter: `./run.sh lesson03.exercise.Formatter server`{{execute}} diff --git a/opentracing-tutorial-lesson03/env-init.sh b/opentracing-tutorial-lesson03/env-init.sh index 338bc13..e86c760 100755 --- a/opentracing-tutorial-lesson03/env-init.sh +++ b/opentracing-tutorial-lesson03/env-init.sh @@ -1,10 +1,6 @@ git clone https://github.com/yurishkuro/opentracing-tutorial \ && docker run -d \ - -p5775:5775/udp \ - -p6831:6831/udp \ - -p6832:6832/udp \ - -p5778:5778 \ -p16686:16686 \ -p14268:14268 \ - -p9411:9411 \ - jaegertracing/all-in-one:1.3 + jaegertracing/all-in-one:1.7 \ + --log-level=debug diff --git a/opentracing-tutorial-lesson04/01-set-baggage-in-the-client.md b/opentracing-tutorial-lesson04/01-set-baggage-in-the-client.md index 48f5216..ea34397 100644 --- a/opentracing-tutorial-lesson04/01-set-baggage-in-the-client.md +++ b/opentracing-tutorial-lesson04/01-set-baggage-in-the-client.md @@ -11,8 +11,6 @@ public static void main(String[] args) { String greeting = args[1]; Tracer tracer = Tracing.init("hello-world"); new Hello(tracer).sayHello(helloTo, greeting); - tracer.close(); - System.exit(0); // okhttpclient sometimes hangs maven otherwise }
diff --git a/opentracing-tutorial-lesson04/env-init.sh b/opentracing-tutorial-lesson04/env-init.sh index 338bc13..e86c760 100755 --- a/opentracing-tutorial-lesson04/env-init.sh +++ b/opentracing-tutorial-lesson04/env-init.sh @@ -1,10 +1,6 @@ git clone https://github.com/yurishkuro/opentracing-tutorial \ && docker run -d \ - -p5775:5775/udp \ - -p6831:6831/udp \ - -p6832:6832/udp \ - -p5778:5778 \ -p16686:16686 \ -p14268:14268 \ - -p9411:9411 \ - jaegertracing/all-in-one:1.3 + jaegertracing/all-in-one:1.7 \ + --log-level=debug