How to Trace requests with RestEasy

When working with RESTful web services, it is often useful to be able to trace requests to better understand what is happening. Fortunately, this is relatively easy to do with WildFly and Resteasy. In this article, we will look at how to configure tracing for REST requests using an enhancement available in WildFly 28.

There are several strategies to simplify the debugging of Rest Requests. For example, ContainerResponseFilter and ContainerRequestFilter provide a way to intercept and log incoming requests and outgoing responses. This article explains more in detail how to do it: How to Log the REST Request and Response

On the other hand, if you want to have a fine grained control over the Request with different levels or threshold you might want to use this new feature available since WildFly 28.

Using the Tracing Feature

The tracing feature provides more internal states of the Jakarta RESTful Web Services container. For example, it could be able to show what filters a request is going through, or how long time a request is processed and other kinds of information.

The tracing feature can be enabled by setting the tracing-type attribute to ALL or ON_DEMAND. You can also control the threshold with the tracing-threshold attribute. The following is an example of enabling tracing using CLI:

tracing-type

  • “ALL”: Enables the tracing feature.
  • “ON_DEMAND” mode will give the control to client side: A client can send a tracing request via HTTP header and get the tracing info back from response headers.
  • “OFF” will disable the tracing feature, and this is the default mode. Note that this it is suggested this is set to “OFF” or production servers.

jaxrs.tracing-threshold

  • “SUMMARY” threshold will emit some brief tracing information.
  • “TRACE” threshold will produce more detailed tracing information
  • “VERBOSE” threshold will generate extremely detailed tracing information.

Here is, for example, how to set the tracing-type attribute from WildFly CLI:

/subsystem=jaxrs:write-attribute(name=tracing-type, value=ALL)
:reload

You can also configure the tracing feature with the context parameters resteasy.server.tracing.type and resteasy.server.tracing.threshold in your deployments web.xml file.

For example, if you were to create an Arquillian test which uses these Parameters:

 @Deployment
    public static Archive<?> deploy() {
        return ShrinkWrap.create(WebArchive.class, "tracing.war")
                .addClasses(TracingConfigResource.class, TracingApp.class)
                .addAsWebInfResource(WebXml.get(
                        "    <context-param>\n" +
                                "        <param-name>resteasy.server.tracing.type</param-name>\n" +
                                "        <param-value>ALL</param-value>\n" +
                                "    </context-param>\n" +
                                "    <context-param>\n" +
                                "        <param-name>resteasy.server.tracing.threshold</param-name>\n" +
                                "        <param-value>VERBOSE</param-value>\n" +
                                "    </context-param>\n"), "web.xml");
    }

Checking the Trace attribute programmatically

Finally, we will mention that it is also possible to access at runtime the Tracing Attributes through the Class RESTEasyTracingLogger, which contains a set of static methods to acces the Config or the Threshold. For example:

@GET
@Path("/type")
public String type(@Context Configuration config) {
    return RESTEasyTracingLogger.getTracingConfig(config);
}

@GET
@Path("/level")
public String level(@Context Configuration config) {
    return RESTEasyTracingLogger.getTracingThreshold(config);
}

@GET
@Path("/logger")
public String logger(@Context HttpServletRequest request) {
    RESTEasyTracingLogger logger = (RESTEasyTracingLogger) request.getAttribute(RESTEasyTracing.PROPERTY_NAME);
    if (logger == null) {
        return "";
    } else {
        return RESTEasyTracingLogger.class.getName();
    }
}

Conclusion

Tracing REST requests is an important tool for understanding what is happening in our web services. With WildFly and Resteasy, it is relatively easy to configure tracing for our RESTful applications. By following the steps outlined above, we can quickly and easily add tracing to our applications and gain deeper insight into how they are functioning.

Found the article helpful? if so please follow us on Socials