io.opentracing.contrib:opentracing-spring-web

Support for integrating OpenTracing in Spring Web applications

License

License

GroupId

GroupId

io.opentracing.contrib
ArtifactId

ArtifactId

opentracing-spring-web
Last Version

Last Version

4.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Support for integrating OpenTracing in Spring Web applications

Download opentracing-spring-web

How to add to project

<!-- https://jarcasting.com/artifacts/io.opentracing.contrib/opentracing-spring-web/ -->
<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-spring-web</artifactId>
    <version>4.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.opentracing.contrib/opentracing-spring-web/
implementation 'io.opentracing.contrib:opentracing-spring-web:4.1.0'
// https://jarcasting.com/artifacts/io.opentracing.contrib/opentracing-spring-web/
implementation ("io.opentracing.contrib:opentracing-spring-web:4.1.0")
'io.opentracing.contrib:opentracing-spring-web:jar:4.1.0'
<dependency org="io.opentracing.contrib" name="opentracing-spring-web" rev="4.1.0">
  <artifact name="opentracing-spring-web" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.opentracing.contrib', module='opentracing-spring-web', version='4.1.0')
)
libraryDependencies += "io.opentracing.contrib" % "opentracing-spring-web" % "4.1.0"
[io.opentracing.contrib/opentracing-spring-web "4.1.0"]

Dependencies

compile (2)

Group / Artifact Type Version
io.opentracing.contrib : opentracing-web-servlet-filter jar 0.4.0
org.springframework : spring-webflux Optional jar

provided (2)

Group / Artifact Type Version
org.springframework : spring-webmvc jar
javax.servlet : javax.servlet-api jar

test (7)

Group / Artifact Type Version
io.opentracing : opentracing-mock jar 0.33.0
org.springframework : spring-test jar 5.2.9.RELEASE
com.github.tomakehurst : wiremock-jre8 jar 2.21.0
io.projectreactor.netty : reactor-netty jar 0.9.12.RELEASE
org.awaitility : awaitility jar 4.0.3
org.mockito : mockito-core jar 2.23.4
junit : junit jar 4.13.1

Project Modules

There are no modules declared in this project.

Build Status Released Version

OpenTracing Spring Web Instrumentation

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

Comparison with opentracing-spring-cloud

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.

For reactive applications, it is especially recommended to use reactor tracing from opentracing-spring-cloud, as that will ensure that the Span is activated in reactor handler functions. (Without that, one would have to extract the Span from the subscriber context.)

How does the server tracing work?

Servlet

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.

Reactive

Server span is started in TracingWebFilter (upon subscription), then onNext(), onError(), etc. handlers add Spring WebFlux related tags and logs.

Library versions

Versions 1.x.y, 2.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

Configuration

Spring Boot 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-starter</artifactId>
</dependency>

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

Manual configuration

Servlet and MVC 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.

Reactive Server

Configuration needs to add the TracingWebFilter bean.

@Configuration
class TracingConfiguration {
    @Bean
    public TracingWebFilter tracingWebFilter(Tracer tracer) {
        return new TracingWebFilter(
                tracer,
                Integer.MIN_VALUE,               // Order
                Pattern.compile(""),             // Skip pattern
                Collections.emptyList(),         // URL patterns, empty list means all
                Arrays.asList(new WebFluxSpanDecorator.StandardTags(), new WebFluxSpanDecorator.WebFluxTags())
        );
    }
}

Client

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

// the same applies for AsyncRestTemplate 

Reactive Client

WebClient webClient = WebClient.builder()
        .filter(new TracingExchangeFilterFunction(tracer, Collections.singletonList(new WebClientSpanDecorator.StandardTags())))
        .build();

Access server span

@RequestMapping("/hello")
public String hello(HttpServletRequest request) {
    Span serverSpan = tracer.activeSpan();

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

Development

./mvnw clean install

Release

Follow instructions in RELEASE

io.opentracing.contrib

3rd-Party OpenTracing API Contributions

3rd-party contributions that use OpenTracing. **The repositories in this org are *not* affiliated with the CNCF.**

Versions

Version
4.1.0
4.0.0
3.0.1
3.0.0
2.1.3
2.1.1
2.1.0
2.0.0
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
0.1.0-RC1
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2