How to: Microprofile Opentracing with Jaeger

In this post I will explain how can we use Microprofile OpenTracing with Jaeger. This post will be practical and will not explain about Microprofile OpenTracing concept deeply. If you wanna know more about this concept you can access my post here.

To use OpenTracing in our application, we need to do two steps, these steps are:

  1. Prepare the application to expose the data in compliance with OpenTracing.
  2. Configure a monitor to get those data and expose information about application ( in this post we’ll use Jaeger).

Preparing the Application

First of all, to make the application able to communicating with Jaeger, we need configure the environment of the application. To do that we need configure some environment’s variable. Below we have some environment’s variable:

$ export JAEGER_AGENT_HOST=jaeger
$ export JAEGER_SERVICE_NAME=speaker
$ export JAEGER_REPORTER_LOG_SPANS=true
$ export JAEGER_SAMPLER_TYPE=const
$ export JAEGER_SAMPLER_PARAM=1

In example above, we are making a configuration to connect the service called speaker into Jaeger.

Now, we need to configure our JAX-RS resource and JAX-RS client (or RestClient starting at Microprofile OpenTracing 1.3) to propagate trace information across services. To do that we need uses an annotation called @Trace on JAX-RS resource (and Rest Client when use Microprofile OpenTracing 1.3). Below we have an example of that.

Configuring the JAX-RS endpoint to be traced

@Path("speakers")
@Traced
public class SpeakerEndpoint {

...

}

Configuring the JAX-RS Client

When we call another service, the information of tracing need be propagated to it service (service called). With this, we need to configure the JAX-RS Client to propagate the information of tracing. To do that, we need registering the JAX-RS Client to OpenTracing.

public Speaker findById( String speakerId ){
    Client client = ClientTracingRegistrar
                      .configure(ClientBuilder.newBuilder())
                      .build();
    Speaker response = null;
    try {
       response = client.target(path)
               .path("/speakers/"+speakerId)
               .request()
               .get(Speaker.class);

    }
    finally {
       client.close();
    }
    return response;
}

Configuring the Rest Client (OpenTracing 1.3)

Since version 1.3 Microprofile OpenTracing integrates with RestClient . With this, you can use the @Traced to configure its RestClient interfaces to be traced. Thus, the information about tracing is propagated to service that was called.

@Path("/speakers")
@RegisterRestClient
@Traced
public interface SpeakerClientService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Speaker findById( String speakerId );
}

 

Configuring Jaeger

Now, we need to configure the Jaeger. When the Jaeger is configured and started. Then the applications send information about tracing to Jaeger and the Jaeger cover those information with an UI. In our example we’ll use docker to start the Jaeger. Below we have a Docker command.

$ docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.11

Then, access http://localhost:16686 to see the Jaeger UI. Below we have a figure with Jaeger UI screem used in our example.

Screenshot from 2019-02-09 19-13-22

When you start  Jaeger, it’ll start to receive service’s data and show in its UI. With this, each access to your services will be shown there.

Conclusion

Using Microprofile Opentracing we can use distributed trace with easier way, without write several line codes and connecting to Jaeger to shows these traces with a good UI. If you want to know more about this Microprofile spec click here.

To see the complete example used here access: https://github.com/rhuan080/microprofile-example

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s