Enabling distributed tracing in microservices

duration 20 minutes

Explore how to enable and customize tracing of JAX-RS and non-JAX-RS methods by using MicroProfile OpenTracing.

What you’ll learn

You will learn how to enable automatic tracing for JAX-RS methods as well as create custom tracers for non-JAX-RS methods by using MicroProfile OpenTracing.

OpenTracing is a standard API for instrumenting microservices for distributed tracing. Distributed tracing helps troubleshoot microservices by examining and logging requests as they propagate through a distributed system, allowing developers to tackle the otherwise difficult task of debugging these requests. Without a distributed tracing system in place, analyzing the workflows of operations becomes difficult, particularly in regard to pinpointing when and by whom a request is received, as well as when a response is sent back.

MicroProfile OpenTracing enables distributed tracing in microservices without adding any explicit distributed tracing code to the application. Note that the MicroProfile OpenTracing specification does not address the problem of defining, implementing, or configuring the underlying distributed tracing system. Rather, the specification makes it easy to instrument services with distributed tracing given an existing distributed tracing system.

You will configure the provided inventory and system services to use distributed tracing with MicroProfile OpenTracing. You will run these services in two separate JVMs made of two server instances to demonstrate tracing in a distributed environment. If all the components were to run on a single server, then any logging software would do the trick.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone https://github.com/openliberty/guide-microprofile-opentracing.git
cd guide-microprofile-opentracing

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project, which is what you will build.

For this guide, use Zipkin as your distributed tracing system. You can find the installation instructions for Zipkin at the Zipkin quickstart page. You are not required to use Zipkin, but keep in mind that you might need more instructions that are not listed here if you choose to use another tracing system.

Before you proceed, make sure that your Zipkin server is up and running. By default, Zipkin can be found at the http://localhost:9411 URL.

Try what you’ll build

The finish directory in the root directory of this guide contains two services that are configured to use MicroProfile OpenTracing. Feel free to give them a try before you proceed.

To try out the services, navigate to the finish directory and run the Maven install phase and the Maven liberty:start-server goal to build the services and run them in two Open Liberty servers:

mvn install
mvn liberty:start-server

Make sure that your Zipkin server is running and point your browser to the http://localhost:9081/inventory/systems/localhost URL. When you visit this endpoint, you make two GET HTTP requests, one to the system service and one to the inventory service. Both of these requests are configured to be traced, so two new traces will be recorded in Zipkin. Visit the http://localhost:9411 URL or another location where you configured Zipkin to run and sort the traces by newest first. Verify that two new traces have been recorded, one for system and one for inventory, and that they contain spans with the following names:

The system trace:

  • get:io.openliberty.guides.system.systemresource.getproperties

The inventory trace:

  • get:io.openliberty.guides.inventory.inventoryresource.getpropertiesforhost

  • addtoinventory() span

You can inspect each span by clicking it to reveal more detailed information, such as the time at which the request was received and the time at which a response was sent back.

If you examine the other traces, you might notice a red trace entry, which happens when an error is caught by the span. In this case, since one of the tests accesses the /inventory/systems/badhostname endpoint, which is invalid, an error is thrown. This behavior is expected.

When you are done checking out the traces, stop both Open Liberty servers using the Maven liberty:stop-server goal:

mvn liberty:stop-server

Running the services

You’ll need to start the services to see basic traces appear in Zipkin. So, before you proceed, build and start the provided system and inventory services in the starting project. Navigate to the start directory and run the Maven install and liberty:start-server goals:

mvn install
mvn liberty:start-server

When the servers start, you can find the system and inventory services at the following URLs:

If you make changes to the code, use the Maven compile goal to rebuild the application and have the running Open Liberty server pick them up automatically. Make sure your application is using loose config for this to work:

mvn compile

To stop the Open Liberty server, run the Maven liberty:stop-server goal:

mvn liberty:stop-server

Existing Tracer implementation

To collect traces across your systems, you need to implement the OpenTracing Tracer interface. For this guide, you can access a bare-bones Tracer implementation for the Zipkin server in the form of a user feature for Open Liberty.

This feature is already configured for you in your pom.xml and server.xml files. It will be downloaded and installed automatically into each service when you run a Maven build. You can find it enabled in your server.xml files:

<feature>usr:opentracingZipkin-0.30</feature>

The following Maven plug-in is responsible for downloading and installing the feature:

<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>${version.download-maven-plugin}</version>
<executions>
    <execution>
        <id>install-tracer</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>wget</goal>
        </goals>
        <configuration>
            <url>https://repo1.maven.org/maven2/net/wasdev/wlp/tracer/liberty-opentracing-zipkintracer/1.0/liberty-opentracing-zipkintracer-1.0-sample.zip</url>
            <unpack>true</unpack>
            <outputDirectory>${project.build.directory}/liberty/wlp/usr</outputDirectory>
        </configuration>
    </execution>
</executions>
</plugin>

If you want to install this feature yourself, see Enabling distributed tracing in the IBM Knowledge Centre.

Enabling distributed tracing

The mpOpenTracing-1.0 MicroProfile OpenTracing feature enables tracing of all JAX-RS methods by default. To further control and customize these traces, use the @Traced annotation to enable and disable tracing of particular methods. You can also inject a custom Tracer object to create and customize spans.

Enabling distributed tracing without code instrumentation

Because tracing of all JAX-RS methods is enabled by default, you need only to enable MicroProfile OpenTracing and the Zipkin user feature in the server.xml file to see some basic traces in Zipkin.

Both of these features are already enabled in the inventory/src/main/liberty/config/server.xml and system/src/main/liberty/config/server.xml files:

<feature>mpOpenTracing-1.0</feature>
<feature>usr:opentracingZipkin-0.30</feature>

Make sure that your services are running. Then, simply point your browser to any of their endpoints and check your Zipkin server for traces.

Enabling explicit distributed tracing

Use the @Traced annotation to define explicit span creation for specific classes and methods. If you place the annotation on a class, then it’s automatically applied to all methods within that class. If you place the annotation on a method, then it overrides the class annotation if one exists.

The @Traced annotation can be configured with the following two parameters:

  • The value=[true|false] parameter indicates whether or not a particular class or method is traced. For example, while all JAX-RS methods are traced by default, you can disable their tracing by using the @Traced(false) annotation. This parameter is set to true by default.

  • The operationName=<Span name> parameter indicates the name of the span that is assigned to the particular method that is traced. If you omit this parameter, the span will be named with the following form: <package name>.<class name>.<method name>. If you use this parameter at a class level, then all methods within that class will have the same span name unless they are explicitly overridden by another @Traced annotation.

Enable tracing of the list() non-JAX-RS method in the inventory/src/main/java/io/openliberty/guides/inventory/InventoryManager.java file:

package io.openliberty.guides.inventory;

import java.util.Properties;
import io.openliberty.guides.inventory.client.SystemClient;
import io.openliberty.guides.inventory.model.InventoryList;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.opentracing.Traced;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.opentracing.ActiveSpan;
import io.opentracing.Tracer;

@ApplicationScoped
public class InventoryManager {

    private InventoryList invList = new InventoryList();
    private InventoryUtils invUtils = new InventoryUtils();


    public Properties get(String hostname) {
        Properties properties = invUtils.getProperties(hostname);

        if (properties != null) {
                invList.addToInventoryList(hostname, properties);
        }
        return properties;
    }

    @Traced(value = true, operationName = "InventoryManager.list")
    public InventoryList list() {
        return invList;
    }

}

Next, run the mvn compile command from the start directory to recompile your services. Point to the http://localhost:9081/inventory/systems URL, check your Zipkin server, and sort the traces by newest first. You see a new trace record that is two spans long with one span for the listContents() JAX-RS method in the InventoryResource class and another span for the list() method in the InventoryManager class. Verify that these spans have the following names:

  • get:io.openliberty.guides.inventory.inventoryresource.listcontents

  • inventorymanager.list

Now, disable tracing of the listContents() JAX-RS method in the inventory/src/main/java/io/openliberty/guides/inventory/InventoryResource.java file:

package io.openliberty.guides.inventory;

import java.util.Properties;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.opentracing.Traced;

import io.openliberty.guides.inventory.model.InventoryList;

@RequestScoped
@Path("/systems")
public class InventoryResource {

    @Inject
    InventoryManager manager;

    @GET
    @Path("/{hostname}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getPropertiesForHost(@PathParam("hostname") String hostname) {
        Properties props = manager.get(hostname);
        if (props == null) {
            return Response.status(Response.Status.NOT_FOUND)
                            .entity("ERROR: Unknown hostname or the system service may "
                                + "not be running on " + hostname)
                            .build();
        }
        return Response.ok(props).build();
    }

    @GET
    @Traced(false)
    @Produces(MediaType.APPLICATION_JSON)
    public InventoryList listContents() {
        return manager.list();
    }

}

Again, run the mvn compile command from the start directory to recompile your services. Point to the http://localhost:9081/inventory/systems URL, check your Zipkin server, and sort the traces by newest first. You see a new trace record that is just one span long for the remaining list() method in the InventoryManager class. Verify that this span has the following name:

  • inventorymanager.list

Injecting a custom Tracer object

The MicroProfile OpenTracing specification also makes the underlying OpenTracing Tracer instance available for use. You can access the configured Tracer by injecting it into a bean by using the @Inject annotation from the Contexts and Dependency Injections API.

Inject the Tracer object into the inventory/src/main/java/io/openliberty/guides/inventory/InventoryManager.java file. Then, use it to define a new child span in the get() method around the addToInventoryList() call:

package io.openliberty.guides.inventory;

import java.util.Properties;
import io.openliberty.guides.inventory.client.SystemClient;
import io.openliberty.guides.inventory.model.InventoryList;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.opentracing.Traced;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.opentracing.ActiveSpan;
import io.opentracing.Tracer;

@ApplicationScoped
public class InventoryManager {

    private InventoryList invList = new InventoryList();
    private InventoryUtils invUtils = new InventoryUtils();

    @Inject
    Tracer tracer;

    public Properties get(String hostname) {
        Properties properties = invUtils.getProperties(hostname);

        if (properties != null) {
            try (ActiveSpan childSpan = tracer.buildSpan("addToInventory() Span")
                                              .startActive()) {
                invList.addToInventoryList(hostname, properties);
            }
        }
        return properties;
    }

    @Traced(value = true, operationName = "InventoryManager.list")
    public InventoryList list() {
        return invList;
    }

}

The try block that you see here is called a try-with-resources statement, meaning that the childSpan object is closed at the end of the statement. It’s good practice to define custom spans inside such statements. Otherwise, any exceptions that are thrown before the span is closed will leak the active span.

Next, run the mvn compile command from the start directory to recompile your services. Point to the http://localhost:9081/inventory/systems/localhost URL, check your Zipkin server, and sort the traces by newest first. You see two new trace records, one for the system service and one for the inventory service. The system trace contains one span for the getProperties() method in the SystemResource class. The inventory trace contains two spans. The first span is for the getPropertiesForHost() method in the InventoryResource class. The second span is the custom span that you created around the addToInventoryList() call. Verify that all of these spans have the following names:

The system trace:

  • get:io.openliberty.guides.system.systemresource.getproperties

The inventory trace:

  • get:io.openliberty.guides.inventory.inventoryresource.getpropertiesforhost

  • addtoinventory() span

This simple example shows what you can do with the injected Tracer object. More configuration options are available to you, including setting a timestamp for when a span was created and destroyed. However, these options require an implementation of their own, which does not come as a part of the Zipkin user feature that is provided. In a real-world scenario, implement all the OpenTracing interfaces that you deem necessary, which might include the SpanBuilder interface. You can use this interface for span creation and customization, including setting timestamps.

Testing the services

No automated tests are provided to verify the correctness of the traces. Manually verify these traces by viewing them on the Zipkin server.

A few tests are included for you to test the basic functionality of the services. If a test failure occurs, then you might have introduced a bug into the code. These tests will run automatically as a part of the Maven build process when you run the mvn install command. You can also run these tests separately from the build by using the mvn verify command, but first make sure that the servers are stopped.

Great work! You’re done!

You have just used MicroProfile OpenTracing to customize how and which traces are delivered to Zipkin.

Feel free to try one of the related MicroProfile guides. They demonstrate additional technologies that you can learn to expand on top of what you built here.

Contribute to this guide

Is something missing or broken? Raise an issue, or send us a pull request.