-
-
Notifications
You must be signed in to change notification settings - Fork 405
Update lessons #30
Update lessons #30
Changes from 4 commits
82c9b53
15419f9
560464c
06b33b4
b748e2f
06c8847
77377a2
8af3052
ea77699
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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 | ||
|
||
|
@@ -94,40 +94,41 @@ 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> | ||
<groupId>io.jaegertracing</groupId> | ||
<artifactId>jaeger-core</artifactId> | ||
<version>0.26.0</version> | ||
<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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we use a simple There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you change this to use |
||
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; | ||
import io.jaegertracing.internal.JaegerTracer; | ||
|
||
Tracer tracer = initTracer("hello-world"); | ||
new Hello(tracer).sayHello(helloTo); | ||
tracer.close(); | ||
try (JaegerTracer tracer = initTracer("hello-world")) { | ||
new Hello(tracer).sayHello(helloTo); | ||
} | ||
``` | ||
|
||
Note that we are passing a string `hello-world` to the init method. It is used to mark all spans emitted by | ||
|
@@ -137,9 +138,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. | ||
|
@@ -177,7 +178,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); | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package lesson01.solution; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.uber.jaeger.Tracer; | ||
|
||
import io.jaegertracing.internal.JaegerTracer; | ||
import io.opentracing.Span; | ||
import lib.Tracing; | ||
|
||
public class Hello { | ||
|
||
private final Tracer tracer; | ||
private final JaegerTracer tracer; | ||
|
||
private Hello(Tracer tracer) { | ||
private Hello(JaegerTracer 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 = String.format("Hello, %s!", helloTo); | ||
span.log(ImmutableMap.of("event", "string-format", "value", helloStr)); | ||
|
||
|
@@ -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 (JaegerTracer tracer = Tracing.init("hello-world")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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) { | ||
|
@@ -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); | ||
} | ||
|
@@ -45,6 +46,5 @@ public static void main(String[] args) { | |
String helloTo = args[0]; | ||
Tracer tracer = Tracing.init("hello-world"); | ||
new HelloActive(tracer).sayHello(helloTo); | ||
tracer.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😢
There was a problem hiding this comment.
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 :)