Skip to content
Switch branches/tags
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Build Status Released Version

OpenTracing CDI Instrumentation

This library provides instrumentation for Contexts and Dependency Injection (CDI) modules in Java EE applications.


For a concrete and comprehensive set of examples, refer to the opentracing-cdi-example module.

Injecting a tracer

In your managed beans, you can get an instance of the appropriate tracer via:

Tracer tracer;

If you have your own Tracer producer, you'll need to mark your own as an @Alternative

@Produces @Alternative
public Tracer getMyTracer() {
    return MyTracer.getInstance();

Injecting a SpanContext

If you have the OpenTracing Servlet integration on the classpath and registered the filter, it's possible to get the request's SpanContext via CDI:

SpanContext spanContext;

Injecting a Scope

This integration also exposes a producer that allows the injection of a Scope:

Scope scope;

All the caveats of using the Scope apply. As a rule of thumb, it can be considered safe to use on synchronous applications, but should not be used for async processing. You can safely mix sync and async if you pass the context from the sync object to the async explicitly, like:

@Inject Scope scope

public void myAction() {

Tracer registration via GlobalTracer

This integration also features a registration of the Tracer with the GlobalTracer, so that other integrations have access to the Tracer producer from your own application. For that to work, make sure that @Inject Tracer tracer injects your own tracer, probably using CDI alternatives.

If there are no Tracer producers on the classpath, the registration will attempt to use the TracerResolver and register its outcome. This means that, in the common scenario, you should be able to just add a tracer implementation to the classpath and everything would work as expected.

If you have your own registration procedure with the TracerResolver, you might want to skip this by setting the system property skipCdiTracerInitializer, otherwise, you might get an exception from the GlobalTracer when trying to register your Tracer, as this integration might have registered one already.


To use the interceptor, add a beans.xml to your deployment (webapp/WEB-INF Maven WAR projects), like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""



Once the interceptor is in place, simply annotate your CDI beans with @Traced, like:

public class InventoryService {
    public String placeOrder() {

or annotate a single method, like:

public class InventoryService {
    public String placeOrder() {

It's also possible to specify that a class should be traced, except for one method, like:

public class InventoryService {
    // this method is not traced
    public String placeOrder() {

    // this method is traced
    public String requestMoreFromSupplier() {


./mvnw clean install


Follow instructions in RELEASE


No description, website, or topics provided.




No packages published