OpenTracing - CDI - Integration

Support for integrating OpenTracing in CDI modules

License

License

GroupId

GroupId

io.opentracing.contrib
ArtifactId

ArtifactId

opentracing-cdi
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

OpenTracing - CDI - Integration
Support for integrating OpenTracing in CDI modules

Download opentracing-cdi

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
io.opentracing : opentracing-api jar 0.31.0
io.opentracing.contrib : opentracing-tracerresolver jar 0.1.4
io.opentracing : opentracing-util jar 0.31.0
io.opentracing.contrib : opentracing-ejb jar 0.0.7

provided (2)

Group / Artifact Type Version
javax : javaee-api jar 7.0
io.opentracing.contrib : opentracing-web-servlet-filter jar 0.1.0-RC1

test (6)

Group / Artifact Type Version
junit : junit jar 4.12
org.jboss.arquillian.junit : arquillian-junit-container jar
org.wildfly.arquillian : wildfly-arquillian-container-managed jar 2.0.0.Final
org.jboss.shrinkwrap.resolver : shrinkwrap-resolver-api jar 2.2.6
org.jboss.shrinkwrap.resolver : shrinkwrap-resolver-impl-maven jar 2.2.6
io.opentracing : opentracing-mock jar 0.31.0

Project Modules

There are no modules declared in this project.

Build Status Released Version

OpenTracing CDI Instrumentation

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

Usage

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:

@Inject
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:

@Inject
SpanContext spanContext;

Injecting a Scope

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

@Inject
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() {
    myAsyncAction.perform(scpe.context());
}

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.

Interceptor

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="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">

    <interceptors>
        <class>io.opentracing.contrib.cdi.CdiOpenTracingInterceptor</class>
    </interceptors>

</beans>

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

@Traced
public class InventoryService {
    public String placeOrder() {
    }
}

or annotate a single method, like:

public class InventoryService {
    @Traced
    public String placeOrder() {
    }
}

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

@Traced
public class InventoryService {
    // this method is not traced
    @Traced(false)
    public String placeOrder() {
    }

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

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
0.1.0
0.0.2