This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathHello.java
More file actions
79 lines (68 loc) · 2.78 KB
/
Copy pathHello.java
File metadata and controls
79 lines (68 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
}
}
}