Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #30 from SevaSafris/master
Browse files Browse the repository at this point in the history
Update lessons
  • Loading branch information
jpkrohling authored Oct 19, 2018
2 parents daf2a0c + ea77699 commit fcf83f0
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 103 deletions.
6 changes: 3 additions & 3 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jaeger.version>0.26.0</jaeger.version>
<jaeger.version>0.31.0</jaeger.version>
<slf4j.version>1.7.5</slf4j.version>
<guava.version>23.0</guava.version>
<dropwizard.version>1.1.4</dropwizard.version>
Expand Down Expand Up @@ -40,8 +40,8 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.uber.jaeger</groupId>
<artifactId>jaeger-core</artifactId>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
<version>${jaeger.version}</version>
</dependency>
<dependency>
Expand Down
11 changes: 8 additions & 3 deletions java/run.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

if [ "$1" == "" ]; then
echo "Usage: run.sh qualified-class-name [args]"
exit 1
echo "Usage: run.sh qualified-class-name [args]"
exit 1
fi

className=$1
Expand All @@ -17,4 +17,9 @@ for jar in $(ls target/dependency/*.jar target/java-opentracing-tutorial-*.jar);
CLASSPATH=$CLASSPATH:$jar
done

java -cp $CLASSPATH $className $*
ADD_MODULES=""
if [ "$(java -version 2>&1 | head -1 | grep '\"1\.[78].\+\"')" = "" ]; then
ADD_MODULES="--add-modules=java.xml.bind"
fi

java $ADD_MODULES -cp $CLASSPATH $className $*
45 changes: 22 additions & 23 deletions java/src/main/java/lesson01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ We can use a global instance returned by `io.opentracing.util.GlobalTracer.get()

```java
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);
Expand All @@ -85,7 +86,7 @@ public class Hello {
We are using the following basic features of the OpenTracing API:
* a `tracer` instance is used to create a span builder via `buildSpan()`
* each `span` is given an _operation name_, `"say-hello"` in this case
* builder is used to create a span via `startManual()`
* builder is used to create a span via `start()`
* each `span` must be finished by calling its `finish()` function
* the start and end timestamps of the span will be captured automatically by the tracer implementation

Expand All @@ -94,40 +95,38 @@ That's because the function `GlobalTracer.get()` returns a no-op tracer by defau

### Initialize a real tracer

Let's create an instance of a real tracer, such as Jaeger (http://github.com/uber/jaeger-client-java).
Let's create an instance of a real tracer, such as Jaeger (https://github.com/jaegertracing/jaeger-client-java).
Our `pom.xml` already imports Jaeger:

```xml
<dependency>
<groupId>com.uber.jaeger</groupId>
<artifactId>jaeger-core</artifactId>
<version>0.26.0</version>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
<version>0.31.0</version>
</dependency>
```

First let's define a helper function that will create a tracer.

```java
import com.uber.jaeger.Configuration;
import com.uber.jaeger.Configuration.ReporterConfiguration;
import com.uber.jaeger.Configuration.SamplerConfiguration;

public static com.uber.jaeger.Tracer initTracer(String service) {
SamplerConfiguration samplerConfig = new SamplerConfiguration("const", 1);
ReporterConfiguration reporterConfig = new ReporterConfiguration(true, null, null, null, null);
Configuration config = new Configuration(service, samplerConfig, reporterConfig);
return (com.uber.jaeger.Tracer) config.getTracer();
import io.jaegertracing.Configuration;
import io.jaegertracing.Configuration.ReporterConfiguration;
import io.jaegertracing.Configuration.SamplerConfiguration;
import io.jaegertracing.internal.JaegerTracer;

public static JaegerTracer initTracer(String service) {
SamplerConfiguration samplerConfig = new SamplerConfiguration().withType("const").withParam(1);
ReporterConfiguration reporterConfig = new ReporterConfiguration().withLogSpans(true);
Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
return config.getTracer();
}
```

To use this instance, let's change the main function:

```java
import com.uber.jaeger.Tracer;

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
Expand All @@ -137,9 +136,9 @@ If we run the program now, we should see a span logged:

```
$ ./run.sh lesson01.exercise.Hello Bryan
[lesson01.exercise.Hello.main()] INFO com.uber.jaeger.Configuration - Initialized tracer=Tracer(...)
[lesson01.exercise.Hello.main()] INFO io.jaegertracing.Configuration - Initialized tracer=Tracer(...)
Hello, Bryan!
[lesson01.exercise.Hello.main()] INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 76509ca0cd333055:76509ca0cd333055:0:1 - say-hello
[lesson01.exercise.Hello.main()] INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 76509ca0cd333055:76509ca0cd333055:0:1 - say-hello
```

If you have Jaeger backend running, you should be able to see the trace in the UI.
Expand Down Expand Up @@ -177,7 +176,7 @@ In the case of `Hello Bryan`, the string `"Bryan"` is a good candidate for a spa
to the whole span and not to a particular moment in time. We can record it like this:

```java
Span span = tracer.buildSpan("say-hello").startManual();
Span span = tracer.buildSpan("say-hello").start();
span.setTag("hello-to", helloTo);
```

Expand Down
14 changes: 8 additions & 6 deletions java/src/main/java/lesson01/solution/Hello.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package lesson01.solution;

import com.google.common.collect.ImmutableMap;
import com.uber.jaeger.Tracer;

import io.jaegertracing.internal.JaegerTracer;
import io.opentracing.Span;
import io.opentracing.Tracer;
import lib.Tracing;

public class Hello {
Expand All @@ -15,9 +16,9 @@ private Hello(Tracer tracer) {
}

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));

Expand All @@ -31,9 +32,10 @@ public static void main(String[] args) {
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();
try (JaegerTracer tracer = Tracing.init("hello-world")) {
new Hello(tracer).sayHello(helloTo);
}
}
}
22 changes: 11 additions & 11 deletions java/src/main/java/lesson02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Of course, this does not change the outcome. What we really want to do is to wra

```java
private String formatString(Span rootSpan, String helloTo) {
Span span = tracer.buildSpan("formatString").startManual();
Span span = tracer.buildSpan("formatString").start();
try {
String helloStr = String.format("Hello, %s!", helloTo);
span.log(ImmutableMap.of("event", "string-format", "value", helloStr));
Expand All @@ -53,7 +53,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"));
Expand All @@ -67,10 +67,10 @@ Let's run it:

```
$ ./run.sh lesson02.exercise.Hello Bryan
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 12c92a6604499c25:12c92a6604499c25:0:1 - formatString
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 12c92a6604499c25:12c92a6604499c25:0:1 - formatString
Hello, Bryan!
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 14aaaf7a377e5147:14aaaf7a377e5147:0:1 - printHello
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: a25cf88369793b9b:a25cf88369793b9b:0:1 - say-hello
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 14aaaf7a377e5147:14aaaf7a377e5147:0:1 - printHello
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: a25cf88369793b9b:a25cf88369793b9b:0:1 - say-hello
```

We got three spans, but there is a problem here. The first hexadecimal segment of the output represents
Expand All @@ -81,7 +81,7 @@ What we really wanted was to establish causal relationship between the two new s
span started in `main()`. We can do that by passing an additional option `asChildOf` to the span builder:

```java
Span span = tracer.buildSpan("formatString").asChildOf(rootSpan).startManual();
Span span = tracer.buildSpan("formatString").asChildOf(rootSpan).start();
```

If we think of the trace as a directed acyclic graph where nodes are the spans and edges are
Expand All @@ -100,10 +100,10 @@ spans now belong to the same trace:

```
$ ./run.sh lesson02.exercise.Hello Bryan
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 4ca67017b68d14c:42d38965612a195a:4ca67017b68d14c:1 - formatString
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 4ca67017b68d14c:42d38965612a195a:4ca67017b68d14c:1 - formatString
Hello, Bryan!
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 4ca67017b68d14c:19af156b64c22d23:4ca67017b68d14c:1 - printHello
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 4ca67017b68d14c:4ca67017b68d14c:0:1 - say-hello
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 4ca67017b68d14c:19af156b64c22d23:4ca67017b68d14c:1 - printHello
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 4ca67017b68d14c:4ca67017b68d14c:0:1 - say-hello
```

We can also see that instead of `0` in the 3rd position the first two reported spans display
Expand All @@ -121,7 +121,7 @@ You may have noticed a few unpleasant side effects of our recent changes
* we also had to write somewhat verbose try/finally code to finish the spans

OpenTracing API for Java provides a better way. Using thread-locals and the notion of an "active span",
we can avoid passing the span through our code and just access it via `tracer.
we can avoid passing the span through our code and just access it via `tracer`.

```java
private void sayHello(String helloTo) {
Expand Down Expand Up @@ -150,7 +150,7 @@ private void printHello(String helloStr) {
```

In the above code we're making the following changes:
* We use `startActive()` method of the span builder instead of `startManual()`,
* We use `startActive()` method of the span builder instead of `start()`,
which makes the span "active" by storing it in a thread-local storage.
* `startActive()` returns a `Scope` object instead of a `Span`. Scope is a container of the currently
active span. We access the active span via `scope.span()`. Once the scope is closed, the previous
Expand Down
14 changes: 8 additions & 6 deletions java/src/main/java/lesson02/solution/HelloActive.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package lesson02.solution;

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;

public class HelloActive {

private final Tracer tracer;

private HelloActive(Tracer tracer) {
Expand All @@ -17,7 +18,7 @@ private HelloActive(Tracer tracer) {
private void sayHello(String helloTo) {
try (Scope scope = tracer.buildSpan("say-hello").startActive(true)) {
scope.span().setTag("hello-to", helloTo);

String helloStr = formatString(helloTo);
printHello(helloStr);
}
Expand All @@ -42,9 +43,10 @@ public static void main(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException("Expecting one argument");
}

String helloTo = args[0];
Tracer tracer = Tracing.init("hello-world");
new HelloActive(tracer).sayHello(helloTo);
tracer.close();
try (JaegerTracer tracer = Tracing.init("hello-world")) {
new HelloActive(tracer).sayHello(helloTo);
}
}
}
20 changes: 11 additions & 9 deletions java/src/main/java/lesson02/solution/HelloManual.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package lesson02.solution;

import com.google.common.collect.ImmutableMap;
import com.uber.jaeger.Tracer;

import io.jaegertracing.internal.JaegerTracer;
import io.opentracing.Span;
import io.opentracing.Tracer;
import lib.Tracing;

public class HelloManual {

private final Tracer tracer;

private HelloManual(Tracer tracer) {
this.tracer = tracer;
}

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);
printHello(span, helloStr);

span.finish();
}

private String formatString(Span rootSpan, String helloTo) {
Span span = tracer.buildSpan("formatString").asChildOf(rootSpan).startManual();
Span span = tracer.buildSpan("formatString").asChildOf(rootSpan).start();
try {
String helloStr = String.format("Hello, %s!", helloTo);
span.log(ImmutableMap.of("event", "string-format", "value", helloStr));
Expand All @@ -36,7 +37,7 @@ private String formatString(Span rootSpan, String helloTo) {
}

private void printHello(Span rootSpan, String helloStr) {
Span span = tracer.buildSpan("printHello").asChildOf(rootSpan).startManual();
Span span = tracer.buildSpan("printHello").asChildOf(rootSpan).start();
try {
System.out.println(helloStr);
span.log(ImmutableMap.of("event", "println"));
Expand All @@ -49,9 +50,10 @@ public static void main(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException("Expecting one argument");
}

String helloTo = args[0];
Tracer tracer = Tracing.init("hello-world");
new HelloManual(tracer).sayHello(helloTo);
tracer.close();
try (JaegerTracer tracer = Tracing.init("hello-world")) {
new HelloManual(tracer).sayHello(helloTo);
}
}
}
12 changes: 6 additions & 6 deletions java/src/main/java/lesson03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,23 +219,23 @@ Then run `lesson03.exercise.Hello`. You should see the outputs like this:
$ ./run.sh lesson03.exercise.Formatter server
[skip noise]
INFO org.eclipse.jetty.server.Server: Started @3968ms
INFO com.uber.jaeger.reporters.LoggingReporter: Span reported: 5fe2d9de96c3887a:b73ff97ea68a36f8:72910f6018b1bd09:1 - format
INFO io.jaegertracing.reporters.LoggingReporter: Span reported: 5fe2d9de96c3887a:b73ff97ea68a36f8:72910f6018b1bd09:1 - format
127.0.0.1 - - "GET /format?helloTo=Bryan HTTP/1.1" 200 13 "-" "okhttp/3.9.0" 3
# publisher
$ ./run.sh lesson03.exercise.Publisher server
[skip noise]
INFO org.eclipse.jetty.server.Server: Started @3388ms
Hello, Bryan!
INFO com.uber.jaeger.reporters.LoggingReporter: Span reported: 5fe2d9de96c3887a:4a2c39e462cb2a92:62d73167c129ecd7:1 - publish
INFO io.jaegertracing.reporters.LoggingReporter: Span reported: 5fe2d9de96c3887a:4a2c39e462cb2a92:62d73167c129ecd7:1 - publish
127.0.0.1 - - "GET /publish?helloStr=Hello,%20Bryan! HTTP/1.1" 200 9 "-" "okhttp/3.9.0" 80
# client
$ ./run.sh lesson03.exercise.Hello Bryan
INFO com.uber.jaeger.Configuration - Initialized tracer=Tracer(...)
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 5fe2d9de96c3887a:72910f6018b1bd09:5fe2d9de96c3887a:1 - formatString
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 5fe2d9de96c3887a:62d73167c129ecd7:5fe2d9de96c3887a:1 - printHello
INFO com.uber.jaeger.reporters.LoggingReporter - Span reported: 5fe2d9de96c3887a:5fe2d9de96c3887a:0:1 - say-hello
INFO io.jaegertracing.Configuration - Initialized tracer=Tracer(...)
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 5fe2d9de96c3887a:72910f6018b1bd09:5fe2d9de96c3887a:1 - formatString
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 5fe2d9de96c3887a:62d73167c129ecd7:5fe2d9de96c3887a:1 - printHello
INFO io.jaegertracing.reporters.LoggingReporter - Span reported: 5fe2d9de96c3887a:5fe2d9de96c3887a:0:1 - say-hello
```

Note how all recorded spans show the same trace ID `5fe2d9de96c3887a`. This is a sign
Expand Down
Loading

0 comments on commit fcf83f0

Please sign in to comment.