Skip to content

pavolloffay/java-spring-web

 
 

Repository files navigation

Build Status Released Version

OpenTracing Spring Web Instrumentation

This library provides instrumentation for Spring Web applications. It creates tracing data for server requests and also client requests (RestTemplate and AsyncRestTemplate). Active server span can be accessed via SpanManager.

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.

Configuration

Auto-configuration

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

<dependency>
  <groupId>io.opentracing.contrib</groupId>
  <artifactId>opentracing-spring-web-autoconfigure</artifactId>
</dependency>

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

Server

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:

@Configuration
@Import({TracingHandlerInterceptor.class})
public class MVCConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private Tracer tracer;

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

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

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(tracingFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(Integer.MIN_VALUE);
        filterRegistrationBean.setAsyncSupported(true);

        return filterRegistrationBean;
    }
}

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

Client

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

// the same applies for AsyncRestTemplate 

Access server span

@RequestMapping("/hello")
public String hello(HttpServletRequest request) {
    // using SpanManager
    SpanManager.ManagedSpan parentSpan = DefaultSpanManager.getInstance().current();

    // or 
    SpanContext serverSpanContext = TracingHandlerInterceptor.serverSpanContext(request);

    Span span = tracer.buildSpan("localSpan");
            .asChildOf(serverSpanContext)
            .start();

    span.finish();
    return "Hello world!";
}

Development

./mvnw clean install

Release

Follow instructions in RELEASE

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 85.4%
  • Shell 10.0%
  • Batchfile 4.6%