Skip to content

Commit

Permalink
Merge pull request #3 from jpkrohling/Update-Scenarios-based-on-tutorial
Browse files Browse the repository at this point in the history
Updated tutorial to latest versions
  • Loading branch information
BenHall authored Oct 19, 2018
2 parents b062faf + 5f8bf25 commit 5a7a7cd
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 171 deletions.
7 changes: 4 additions & 3 deletions opentracing-tutorial-lesson01/02-create-trace.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ Let's create a trace that consists of just a single span. To do that we need an
<pre class="file" data-filename="opentracing-tutorial/java/src/main/java/lesson01/exercise/Hello.java" data-target="replace">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);
Expand Down
78 changes: 30 additions & 48 deletions opentracing-tutorial-lesson01/03-initialize-real-tracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,29 @@ Let's create an instance of a real tracer, such as Jaeger (http://github.com/ube

```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.32.0</version>
</dependency>
```

We need to add some imports:

<pre class="file" data-target="clipboard">
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;
</pre>

And we define a helper function that will create a tracer.

<pre class="file" data-target="clipboard">
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();
}
</pre>

Expand All @@ -42,7 +33,6 @@ To use this instance, let's change the main function:
<pre class="file" data-target="clipboard">
Tracer tracer = initTracer("hello-world");
new Hello(tracer).sayHello(helloTo);
tracer.close();
</pre>

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.
Expand All @@ -51,60 +41,52 @@ This is how our `Hello` class looks like now:

<pre class="file" data-filename="java/src/main/java/lesson01/exercise/Hello.java" data-target="replace">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);

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

}</pre>

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!
Check also the [Jaeger UI](https://[[HOST_SUBDOMAIN]]-16686-[[KATACODA_HOST]].environments.katacoda.com/search?service=hello-world) for the newly created trace!
34 changes: 18 additions & 16 deletions opentracing-tutorial-lesson01/04-annotate-trace-with-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<pre class="file" data-target="clipboard">
Span span = tracer.buildSpan("say-hello").startManual();
Span span = tracer.buildSpan("say-hello").start();
span.setTag("hello-to", helloTo);
</pre>

Expand Down Expand Up @@ -46,11 +46,15 @@ If you run the program with these changes, then find the trace in the UI and exp
<pre class="file" data-filename="opentracing-tutorial/java/src/main/java/lesson01/exercise/Hello.java" data-target="replace">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 {

Expand All @@ -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));

Expand All @@ -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();
}
}</pre>

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
[google-logging]: https://www.google.com/search?q=structured-logging
8 changes: 2 additions & 6 deletions opentracing-tutorial-lesson01/env-init.sh
Original file line number Diff line number Diff line change
@@ -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
55 changes: 38 additions & 17 deletions opentracing-tutorial-lesson02/01-tracing-individual-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ In Lesson 1 we wrote a program that creates a trace that consists of a single sp
<pre class="file" data-filename="opentracing-tutorial/java/src/main/java/lesson02/exercise/Hello.java" data-target="replace">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 {

Expand All @@ -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);
Expand All @@ -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();
}
}</pre>

Expand Down Expand Up @@ -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.

<pre class="file" data-target="clipboard">
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));
Expand All @@ -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"));
Expand Down Expand Up @@ -107,11 +119,14 @@ For reference, here's how our final code looks like:
<pre class="file" data-filename="opentracing-tutorial/java/src/main/java/lesson02/exercise/Hello.java" data-target="replace">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 {

Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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"));
Expand All @@ -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();
}
}</pre>
8 changes: 2 additions & 6 deletions opentracing-tutorial-lesson02/env-init.sh
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 5a7a7cd

Please sign in to comment.