OpenTracing Spring Web instrumentation

Build Status Released Version

OpenTracing Spring Web Instrumentation

This library provides instrumentation for Spring Web applications (Boot and MVC). It creates tracing data for server requests and also client requests (RestTemplate and AsyncRestTemplate).

Use opentracing-spring-cloud instead

As it was mentioned above this library traces only inbound/outbound HTTP requests. If you would like to get automatically traced different set of technologies e.g. spring-cloud-netflix, JMS or even more then use project opentracing-spring-cloud instead.

How does the server tracing work?

Server span is started in Web Servlet Filter, then tracing interceptor adds spring related tags and logs. There are use case when spring boot invokes a handler after a request processing in filter finished, in this case interceptor starts a new span as followsFrom which references the initial span created in the servlet filter.

Library versions

Versions 1.x.y of the library are meant to target Spring Framework 5.x and Spring Boot 2.x while versions 0.x.y are meant to be used with Spring Framework 4.3 and Spring Boot 1.5


Spring Boot Auto-configuration

If you are using Spring Boot the easiest way how to configure OpenTracing instrumentation is to use auto-configuration:


Just provide an OpenTracing tracer bean and all required configuration is automatically done for you. It also instruments all RestTemplate and AsyncRestTemplate beans.

Manual configuration


Configuration needs to add TracingFilter and TracingHandlerInterceptor. Both of these classes are required!

Tracing interceptor can be instantiated manually or injected via CDI, but it needs bean of type Tracer configured.

Java based configuration:

public class MVCConfiguration extends WebMvcConfigurerAdapter {

    private Tracer tracer;

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TracingHandlerInterceptor(tracer));

    public FilterRegistrationBean tracingFilter() {
        TracingFilter tracingFilter = new TracingFilter(tracer);

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(tracingFilter);

        return filterRegistrationBean;

XML based configuration can be used too. Filter can be also directly defined in web.xml.


RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new TracingRestTemplateInterceptor(tracer)));

// the same applies for AsyncRestTemplate 

Access server span

public String hello(HttpServletRequest request) {
    Span serverSpan = tracer.activeSpan();

    Span span = tracer.buildSpan("localSpan")
    try {
        // Traced work happens between start() and deactivate();
        return "Hello world!";
    } finally {


./mvnw clean install


Follow instructions in RELEASE