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

add lesson05 for okhttp extra credit #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions java/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.project
.settings/
target/
.idea
*.iml
6 changes: 6 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-okhttp3</artifactId>
<version>0.1.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Empty file.
58 changes: 58 additions & 0 deletions java/src/main/java/lesson05/solution/Formatter.java
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);
}
}
}
83 changes: 83 additions & 0 deletions java/src/main/java/lesson05/solution/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
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 OkHttpClient client;
private final Call.Factory traceClient;

private Hello(Tracer tracer) {
this.tracer = tracer;
this.client = new OkHttpClient();
taobaorun marked this conversation as resolved.
Show resolved Hide resolved
traceClient = new TracingCallFactory(this.client, tracer);
taobaorun marked this conversation as resolved.
Show resolved Hide resolved
}

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);
}
}
}
54 changes: 54 additions & 0 deletions java/src/main/java/lesson05/solution/Publisher.java
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")) {
Copy link
Owner

Choose a reason for hiding this comment

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