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 3 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
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 $*
14 changes: 7 additions & 7 deletions java/src/main/java/lesson01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class Hello {
}

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 +85,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 @@ -101,7 +101,7 @@ Our `pom.xml` already imports Jaeger:
<dependency>
<groupId>com.uber.jaeger</groupId>
<artifactId>jaeger-core</artifactId>
<version>0.26.0</version>
<version>0.27.0</version>
Copy link
Owner

Choose a reason for hiding this comment

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

we should change it to io.jaegertracing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, ok. I didn't know this package has been moved to io.jaegertracing:jaeger-core. The java package names have changed, so imports and fully qualified class references would have to be updated too. I can update this pull request with these changes as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

I think you have noticed as well, but we are on 0.310. already, so, a version bump to that one would be good.

</dependency>
```

Expand All @@ -113,9 +113,9 @@ 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);
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 (com.uber.jaeger.Tracer) config.getTracer();
}
```
Expand Down Expand Up @@ -177,7 +177,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
10 changes: 5 additions & 5 deletions java/src/main/java/lesson01/solution/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,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 @@ -32,8 +32,8 @@ public static void main(String[] args) {
throw new IllegalArgumentException("Expecting one argument");
}
String helloTo = args[0];
Tracer tracer = Tracing.init("hello-world");
new Hello(tracer).sayHello(helloTo);
tracer.close();
try (Tracer tracer = Tracing.init("hello-world")) {
Copy link
Owner

Choose a reason for hiding this comment

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

is the tracer closable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tracer is com.uber.jaeger.Tracer in this class, which implements the Closeable interface. io.opentracing.Tracer, however, does not extend the Closeable interface.

Copy link
Collaborator

Choose a reason for hiding this comment

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

[off-topic] Would be a good improvement for OpenTracing :)

new Hello(tracer).sayHello(helloTo);
}
}
}
10 changes: 5 additions & 5 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 Down Expand Up @@ -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 Down Expand Up @@ -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
10 changes: 5 additions & 5 deletions java/src/main/java/lesson02/solution/HelloActive.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lib.Tracing;

public class HelloActive {

private final Tracer tracer;

private HelloActive(Tracer tracer) {
Expand All @@ -17,7 +17,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 @@ -43,8 +43,8 @@ public static void main(String[] args) {
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 (Tracer tracer = Tracing.init("hello-world")) {
new HelloActive(tracer).sayHello(helloTo);
}
}
}
16 changes: 8 additions & 8 deletions java/src/main/java/lesson02/solution/HelloManual.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
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 +36,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 @@ -50,8 +50,8 @@ public static void main(String[] args) {
throw new IllegalArgumentException("Expecting one argument");
}
String helloTo = args[0];
Tracer tracer = Tracing.init("hello-world");
new HelloManual(tracer).sayHello(helloTo);
tracer.close();
try (Tracer tracer = Tracing.init("hello-world")) {
new HelloManual(tracer).sayHello(helloTo);
}
}
}
6 changes: 3 additions & 3 deletions java/src/main/java/lesson03/exercise/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static void main(String[] args) {
throw new IllegalArgumentException("Expecting one argument");
}
String helloTo = args[0];
Tracer tracer = Tracing.init("hello-world");
new Hello(tracer).sayHello(helloTo);
tracer.close();
try (Tracer tracer = Tracing.init("hello-world")) {
new Hello(tracer).sayHello(helloTo);
}
safris marked this conversation as resolved.
Show resolved Hide resolved
}
}
6 changes: 3 additions & 3 deletions java/src/main/java/lesson03/solution/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public static void main(String[] args) {
throw new IllegalArgumentException("Expecting one argument");
}
String helloTo = args[0];
Tracer tracer = Tracing.init("hello-world");
new Hello(tracer).sayHello(helloTo);
tracer.close();
try (Tracer tracer = Tracing.init("hello-world")) {
new Hello(tracer).sayHello(helloTo);
}
}
}
6 changes: 3 additions & 3 deletions java/src/main/java/lesson04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static void main(String[] args) {
}
String helloTo = args[0];
String greeting = args[1];
Tracer tracer = Tracing.init("hello-world");
new Hello(tracer).sayHello(helloTo, greeting);
tracer.close();
try (Tracer tracer = Tracing.init("hello-world")) {
new Hello(tracer).sayHello(helloTo, greeting);
}
}
```

Expand Down
6 changes: 3 additions & 3 deletions java/src/main/java/lesson04/solution/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public static void main(String[] args) {
}
String helloTo = args[0];
String greeting = args[1];
Tracer tracer = Tracing.init("hello-world");
new Hello(tracer).sayHello(helloTo, greeting);
tracer.close();
try (Tracer tracer = Tracing.init("hello-world")) {
new Hello(tracer).sayHello(helloTo, greeting);
}
}
}