package lesson03.exercise;

import java.io.IOException;

import com.google.common.collect.ImmutableMap;
import com.uber.jaeger.Tracer;

import io.opentracing.Scope;
import lib.Tracing;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Hello {

    private final Tracer tracer;
    private final OkHttpClient client;

    private Hello(Tracer tracer) {
        this.tracer = tracer;
        this.client = new OkHttpClient();
    }

    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 = client.newCall(request).execute();
            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) {
        try (Scope scope = tracer.buildSpan("say-hello").startActive(true)) {
            scope.span().setTag("hello-to", helloTo);

            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 != 1) {
            throw new IllegalArgumentException("Expecting one argument");
        }
        String helloTo = args[0];
        try (Tracer tracer = Tracing.init("hello-world")) {
            new Hello(tracer).sayHello(helloTo);
        }
    }
}

@@ -101,39 +101,37 @@
 ap;%0A
+%0A
 import 
-com.uber.jaeger.Tracer;%0A
+io.opentracing.Scope;
 %0Aimp
@@ -141,37 +141,38 @@
  io.opentracing.
-Scope
+Tracer
 ;%0Aimport lib.Tra
@@ -2325,21 +2325,16 @@
         
-try (
 Tracer t
@@ -2368,31 +2368,26 @@
 -world%22)
-) %7B%0A   
+;%0A
         
  new Hel
@@ -2378,17 +2378,16 @@
         
- 
 new Hell
@@ -2415,26 +2415,16 @@
 lloTo);%0A
-        %7D%0A
     %7D%0A%7D%0A

Make solutions OT vendor agnostic