JAX-RS instrumentation provides nice visibility into your app, however, it is often
necessary to add custom data to the trace to see what is happening in the service or database layer.
The following code snippet shows how the service layer can create and add data to the trace:
public class BackendService {
@Inject
private io.opentracing.Tracer tracer;
public String action() throws InterruptedException {
int random = new Random().nextInt(200);
try (ActiveSpan span = tracer.buildSpan("action").startActive()) {
anotherAction();
Thread.sleep(random);
}
return String.valueOf(random);
}
private void anotherAction() {
tracer.activeSpan().setTag("anotherAction", "data");
}
Note that it’s not necessary to manually pass a span instance around. The method anotherAction
accesses
the current active span from the tracer.
With the additional instrumentation shown above, an invocation of the REST endpoint would result in
a trace consisting of two spans, one representing the inbound server request, and the other the business logic.
The span representing server processing is automatically considered as the parent for span created in business layer.
If we created span in anotherAction
then its parent would be span created in action
method.
Figure 1: Jaeger showing reported spans.