Commit 36f6d07c authored by Alvaro Hernandez's avatar Alvaro Hernandez
Browse files

Add class TracingUtils to factor tracing code



Wrap all methods that want to be traced with a simpler to use lambda.
Provide utility method to initialize a class' tracer.

[202009-opentelemetry_jaeger_java]
Signed-off-by: Alvaro Hernandez's avatarÁlvaro Hernández <aht@ongres.com>
parent b46a58b1
package com.ongres.blog.oteljaeger.http;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;
import javax.ws.rs.GET;
......@@ -11,37 +8,37 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.ThreadLocalRandom;
import static com.ongres.blog.oteljaeger.http.TracingUtils.doInTracerSpanScope;
@Path("/traceme")
public class Http {
private static final Tracer TRACER = OpenTelemetry.getTracerProvider().get(Http.class.getSimpleName());
private static final Tracer TRACER = TracingUtils.getClassTracer(Http.class);
private ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello1() throws InterruptedException {
Span span = TRACER.spanBuilder("hello1").startSpan();
try(Scope scope = TRACER.withSpan(span)) {
public String hello1() {
return doInTracerSpanScope(TRACER, "hello1", (span, scope) -> {
// Introduce some random delay
Thread.sleep(threadLocalRandom.nextInt(1000));
try {
Thread.sleep(threadLocalRandom.nextInt(1000));
} catch (InterruptedException e) {}
// Call nested function
return hello2();
} finally {
span.end();
}
});
}
private String hello2() throws InterruptedException {
Span span = TRACER.spanBuilder("hello2").startSpan();
try(Scope scope = TRACER.withSpan(span)) {
private String hello2() {
return doInTracerSpanScope(TRACER, "hello2", (span, scope) -> {
// Introduce some random delay
Thread.sleep(threadLocalRandom.nextInt(1000));
try {
Thread.sleep(threadLocalRandom.nextInt(1000));
} catch (InterruptedException e) {}
return "hello";
} finally {
span.end();
}
});
}
}
\ No newline at end of file
package com.ongres.blog.oteljaeger.http;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class TracingUtils {
public static Tracer getClassTracer(Class<?> clazz) {
return OpenTelemetry.getTracerProvider().get(clazz.getSimpleName());
}
public static <T> T doInTracerSpanScope(Tracer tracer, String spanName, BiFunction<Span,Scope,T> function) {
Span span = tracer.spanBuilder(spanName).startSpan();
try(Scope scope = tracer.withSpan(span)) {
return function.apply(span, scope);
} finally {
span.end();
}
}
public static void doInTracerSpanScope(Tracer tracer, String spanName, BiConsumer<Span,Scope> function) {
Span span = tracer.spanBuilder(spanName).startSpan();
try(Scope scope = tracer.withSpan(span)) {
function.accept(span, scope);
} finally {
span.end();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment