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

Update lessons #30

Merged
merged 9 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.32.0, otherwise, you'll need to add the #close() calls. The 0.32.0 implements the "close on JVM shutdown" feature.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavolloffay, is 0.32.0 available on maven central already?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be, if it's not I don't want to redo the release 😢

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's there, just confirmed :)

Suggested change
<version>0.31.0</version>
<version>0.32.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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we use a simple Configuration.fromEnv(serviceName).getTracer() here in the code and export the relevant env vars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpkrohling, I just started learning OpenTracing, so I'm not familiar with "export the relevant env vars", yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. This is particular to Jaeger (not generic OpenTracing), and it might indeed confuse users. For reference, I'm talking about JAEGER_REPORTER_LOG_SPANS, JAEGER_SAMPLER_TYPE and JAEGER_SAMPLER_PARAM

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to use fromEnv(), like what we are doing in the Katacoda scenario? We override some env vars there, so, fromEnv() is required there (even though it's not required here).

https://github.com/katacoda-scenarios/opentracing-scenarios/blob/master/opentracing-tutorial-lesson01/03-initialize-real-tracer.md

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")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the tutorial should be using OT's Tracer instead of JaegerTracer as much as possible. The side effect is that, at least for now, we'd need to close the tracer manually (see jaegertracing/jaeger-client-java#544).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, we could change the tutorial later, once we fix the mentioned issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely agree! I have changed the code and lesson01/README.md to reflect this.

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`.
safris marked this conversation as resolved.
Show resolved Hide resolved

```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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll still need this until we release 0.31.1, which includes the JVM shutdown hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been waiting for opentracing/opentracing-java#250 and opentracing/specification#128 to move forward with the Closeable spec, but it seems this will take a while. I've updated this pull request to upcast to JaegerTracer in a try-resource block.

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