This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 407
add lesson05 for okhttp extra credit #50
Open
taobaorun
wants to merge
3
commits into
yurishkuro:master
Choose a base branch
from
taobaorun:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
.project | ||
.settings/ | ||
target/ | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Lesson 5 - Using Existing Open Source Instrumentation | ||
|
||
## Objectives | ||
|
||
* Using Okhttp Open Source Instrumentation | ||
|
||
|
||
### Walkthrough | ||
|
||
|
||
### okhttp | ||
|
||
Adding manual instrumentation to okhttp like we did in [Lesson 3](../lesson03) | ||
is tedious. Fortunately, we don't need to do that because that instrumentation itself already exists | ||
as open source modules: | ||
|
||
* https://github.com/opentracing-contrib/java-okhttp | ||
|
||
For an extra credit, try to use these modules to avoid instrumenting your code manually. | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package lesson05.solution; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.dropwizard.Application; | ||
import io.dropwizard.Configuration; | ||
import io.dropwizard.setup.Environment; | ||
import io.jaegertracing.internal.JaegerTracer; | ||
import io.opentracing.Scope; | ||
import io.opentracing.Tracer; | ||
import lib.Tracing; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
public class Formatter extends Application<Configuration> { | ||
|
||
private final Tracer tracer; | ||
|
||
private Formatter(Tracer tracer) { | ||
this.tracer = tracer; | ||
} | ||
|
||
@Path("/format") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public class FormatterResource { | ||
|
||
@GET | ||
public String format(@QueryParam("helloTo") String helloTo, @Context HttpHeaders httpHeaders) { | ||
try (Scope scope = Tracing.startServerSpan(tracer, httpHeaders, "format")) { | ||
String greeting = scope.span().getBaggageItem("greeting"); | ||
if (greeting == null) { | ||
greeting = "Hello"; | ||
} | ||
String helloStr = String.format("%s, %s!", greeting, helloTo); | ||
scope.span().log(ImmutableMap.of("event", "string-format", "value", helloStr)); | ||
return helloStr; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void run(Configuration configuration, Environment environment) throws Exception { | ||
environment.jersey().register(new FormatterResource()); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
System.setProperty("dw.server.applicationConnectors[0].port", "8081"); | ||
System.setProperty("dw.server.adminConnectors[0].port", "9081"); | ||
try (JaegerTracer tracer = Tracing.init("formatter")) { | ||
new Formatter(tracer).run(args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package lesson05.solution; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.jaegertracing.internal.JaegerTracer; | ||
import io.opentracing.Scope; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.contrib.okhttp3.TracingCallFactory; | ||
import lib.Tracing; | ||
import okhttp3.Call; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
public class Hello { | ||
|
||
private final Tracer tracer; | ||
private final Call.Factory traceClient; | ||
|
||
private Hello(Tracer tracer) { | ||
this.tracer = tracer; | ||
traceClient = new TracingCallFactory(new OkHttpClient(), tracer); | ||
} | ||
|
||
private String getHttp(int port, String path, String param, String value) { | ||
try { | ||
HttpUrl url = new HttpUrl.Builder().scheme("http").host("localhost").port(port).addPathSegment(path) | ||
.addQueryParameter(param, value).build(); | ||
Request.Builder requestBuilder = new Request.Builder().url(url); | ||
Request request = requestBuilder.build(); | ||
Response response = traceClient.newCall(request).execute(); | ||
tracer.activeSpan().setTag("invoked", "okhttptracer"); | ||
if (response.code() != 200) { | ||
throw new RuntimeException("Bad HTTP result: " + response); | ||
} | ||
return response.body().string(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void sayHello(String helloTo, String greeting) { | ||
try (Scope scope = tracer.buildSpan("say-hello").startActive(true)) { | ||
scope.span().setTag("hello-to", helloTo); | ||
scope.span().setBaggageItem("greeting", greeting); | ||
|
||
String helloStr = formatString(helloTo); | ||
printHello(helloStr); | ||
} | ||
} | ||
|
||
private String formatString(String helloTo) { | ||
try (Scope scope = tracer.buildSpan("formatString").startActive(true)) { | ||
String helloStr = getHttp(8081, "format", "helloTo", helloTo); | ||
scope.span().log(ImmutableMap.of("event", "string-format", "value", helloStr)); | ||
return helloStr; | ||
} | ||
} | ||
|
||
private void printHello(String helloStr) { | ||
try (Scope scope = tracer.buildSpan("printHello").startActive(true)) { | ||
getHttp(8082, "publish", "helloStr", helloStr); | ||
scope.span().log(ImmutableMap.of("event", "println")); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
if (args.length != 2) { | ||
throw new IllegalArgumentException("Expecting two arguments, helloTo and greeting"); | ||
} | ||
String helloTo = args[0]; | ||
String greeting = args[1]; | ||
try (JaegerTracer tracer = Tracing.init("hello-world")) { | ||
new Hello(tracer).sayHello(helloTo, greeting); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package lesson05.solution; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.dropwizard.Application; | ||
import io.dropwizard.Configuration; | ||
import io.dropwizard.setup.Environment; | ||
import io.jaegertracing.internal.JaegerTracer; | ||
import io.opentracing.Scope; | ||
import io.opentracing.Tracer; | ||
import lib.Tracing; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
public class Publisher extends Application<Configuration> { | ||
|
||
private final Tracer tracer; | ||
|
||
private Publisher(Tracer tracer) { | ||
this.tracer = tracer; | ||
} | ||
|
||
@Path("/publish") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public class PublisherResource { | ||
|
||
@GET | ||
public String format(@QueryParam("helloStr") String helloStr, @Context HttpHeaders httpHeaders) { | ||
try (Scope scope = Tracing.startServerSpan(tracer, httpHeaders, "publish")) { | ||
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. If you really want to implement the fill solution for the extra credit, then this should also use the opentracing-contrib library for Dropwizard. The point of extracredit is to not do instrumentation manually. |
||
System.out.println(helloStr); | ||
scope.span().log(ImmutableMap.of("event", "println", "value", helloStr)); | ||
return "published"; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void run(Configuration configuration, Environment environment) throws Exception { | ||
environment.jersey().register(new PublisherResource()); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
System.setProperty("dw.server.applicationConnectors[0].port", "8082"); | ||
System.setProperty("dw.server.adminConnectors[0].port", "9082"); | ||
try (JaegerTracer tracer = Tracing.init("publisher")) { | ||
new Publisher(tracer).run(args); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I prefer not to call this 'lesson05'. In all tutorials this is called 'extracredit, and there already exists a package for that, with a readme: https://github.com/yurishkuro/opentracing-tutorial/tree/master/java/src/main/java/extracredit
You can add everything there.