Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
java
java
final SenderConfiguration senderConfiguration = new SenderConfiguration() {
    @Override
    public Sender getSender() {
        return ...; /* the desired Sender implementation */
    }
}

...

Code Block
java
java
final Tracer tracer = new Configuration("web-client")
    .withSampler(
        new SamplerConfiguration() 
            .withType(ConstSampler.TYPE) /* or any other Sampler */
            .withParam(1)
        )
    .withReporter(
        new ReporterConfiguration()
            .withSender(
                new SenderConfiguration() /* or any other Sender configuration */
                    .withEndpoint("http://localhost:14268/api/traces")
            )
    )
    .getTracer();
                
Response response = WebClient
    .create("http://localhost:9000/catalog", Arrays.asList(new OpenTracingClientProvider(tracer)))
    .accept(MediaType.APPLICATION_JSON)
    .get();

...

Code Block
java
java
final Tracer tracer = new Configuration("jaxrs-client",) 
     .withSampler(
        new Configuration.SamplerConfiguration(ConstSampler.TYPE, 1), /* or
 any other Sampler */
        new Configuration.ReporterConfiguration(new HttpSender("http://localhost:14268/api/traces")withType(ConstSampler.TYPE) /* or any other SenderSampler */
    ).getTracer();
        .withParam(1)
        )
final OpenTracingClientProvider provider =  .withReporter(
        new OpenTracingClientProviderReporterConfiguration(tracer);
final Client client = ClientBuilder.newClient().register(provider);
    
final Response response = client
  .target
            .withSender(
                new SenderConfiguration() /* or any other Sender configuration */
                    .withEndpoint("http://localhost:900014268/api/catalogtraces")
  .request(          )
    )
                
final OpenTracingClientProvider provider = new OpenTracingClientProvider(tracer);
final Client client = ClientBuilder.newClient().register(provider);
    
final Response response = client
  .target("http://localhost:9000/catalog")
  .request()
  .accept(MediaType.APPLICATION_JSON)
  .get();

...

Code Block
java
java
final Tracer tracer = new Configuration("jaxrs-client")
    .withSampler(
        new SamplerConfiguration() 
            .withType(ConstSampler.TYPE)
 /* or any other Sampler */
            .withParam(1)
        )
    .withReporter(
        new ReporterConfiguration()
            .withSender(
                new SenderConfiguration() /* or any other Sender configuration */
                    .withEndpoint("http://localhost:14268/api/traces")
            )
    )
    .getTracer();

// This method should only be called once during the application initialization phase.
GlobalTracer.register(tracer);

// No explicit Tracer instance is required, it will be picked off the GlobalTracer using get() method
final OpenTracingClientProvider provider = new OpenTracingClientProvider();

...

Code Block
java
java
@ApplicationPath( "/" )
public class CatalogApplication extends Application {
    @Override
    public Set<Object> getSingletons() {
        final Tracer tracer = new Configuration("tracer-server") 
            .withSampler(
                new SamplerConfiguration()
                    .withType(ConstSampler.TYPE) /* or any other Sampler */
                    .withParam(1)
            )
            .withReporter(
                new ReporterConfiguration()
                    .withSender(
                        new SenderConfiguration() /* or any other Sender configuration */
                            .withEndpoint("http://localhost:14268/api/traces")
                    )
            )
            .getTracer();
            
        return new HashSet<>(
                Arrays.asList(
                    new OpenTracingFeature(tracer)
                )
            );
    } 
}

...

Code Block
java
java
final Tracer tracer = new Configuration("tracer-server") 
    .withSampler(
        new SamplerConfiguration()
            .withType(ConstSampler.TYPE) /* or any other Sampler */
            .withParam(1)
        )
    .withReporter(
        new ReporterConfiguration()
            .withSender(
                new SenderConfiguration() /* or any other Sender configuration */
                    .withEndpoint("http://localhost:14268/api/traces")
            )
    )
    .getTracer();

final JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint(/* application instance */, JAXRSServerFactoryBean.class);
factory.setProvider(new OpenTracingFeature(tracer));
...
return factory.create();

...

The actual invocation of the request by the client (with service name tracer-client) and consequent invocation of the service on the server side (service name traceser-server) is going to generate the following sample traces (taken from Jaeger UI):

Image Modified

Example #4: Client and Server with annotations (key/value)

In this example server-side implementation of the JAX-RS service is going to add key/value annotations to the active span. The client-side code stays unchanged.

Code Block
java
java
@Produces( { MediaType.APPLICATION_JSON } )
@GET
public Collection<Book> getBooks(@Context final TracerContext tracer) throws Exception {
    final Collection<Book> books = Arrays.asList(
        new Book("Apache CXF Web Service Development", "Naveen Balani, Rajeev Hathi")
    );
         
    tracer.annotate("# of books", Integer.toString(books.size()));
    return books;
}

The actual invocation of the request by the client (with service name tracer-client) and consequent invocation of the service on the server side (service name tracer-server) is going to generate the following sample server trace properties (taken from Jaeger UI):

Image Added

Example #5: Client and Server with parallel trace (involving thread pools)

In this example server-side implementation of the JAX-RS service is going to offload some work into thread pool and then return the response to the client, simulating parallel execution. The client-side code stays unchanged.

Code Block
java
java
@Produces( { MediaType.APPLICATION_JSON } )
@GET
public Collection<Book> getBooks(@Context final TracerContext tracer) throws Exception {
    final Future<Book> book1 = executor.submit(
        tracer.wrap("Getting Book 1", new Traceable<Book>() {
            public Book call(final TracerContext context) throws Exception {
                // Simulating a delay of 100ms required to call external system
                Thread.sleep(100);
                     
                return new Book("Apache CXF Web Service Development", 
                    "Naveen Balani, Rajeev Hathi");
            }
        })
    );
         
    final Future<Book> book2 = executor.submit(
        tracer.wrap("Getting Book 2", new Traceable<Book>() {
            public Book call(final TracerContext context) throws Exception {
                // Simulating a delay of 100ms required to call external system
                Thread.sleep(200);
                     
                return new Book("Developing Web Services with Apache CXF and Axis2", 
                    "Kent Ka Iok Tong");
            }
        })
    );
        
    return Arrays.asList(book1.get(), book2.get());
}

The actual invocation of the request by the client (with service name tracer-client) and consequent invocation of the service on the server side (process name tracer-server) is going to generate the following sample traces (taken from Jaeger UI):

Image Added

Example #6: Client and Server with asynchronous JAX-RS service (server-side)

In this example server-side implementation of the JAX-RS service is going to be executed asynchronously. It poses a challenge from the tracing prospective as request and response are processed in different threads (in general). At the moment, Apache CXF does not support the transparent tracing spans management (except for default use case) but provides the simple ways to do that (by letting to transfer spans from thread to thread). The client-side code stays unchanged.

Code Block
java
java
@Produces( { MediaType.APPLICATION_JSON } )
@GET
public void getBooks(@Suspended final AsyncResponse response, @Context final TracerContext tracer) throws Exception {
    tracer.continueSpan(new Traceable<Future<Void>>() {
        public Future<Void> call(final TracerContext context) throws Exception {
            return executor.submit(
                tracer.wrap("Getting Book", new Traceable<Void>() {
                    public Void call(final TracerContext context) throws Exception {
                        // Simulating a processing delay of 50ms
                        Thread.sleep(50);
                             
                        response.resume(
                            Arrays.asList(
                                new Book("Apache CXF Web Service Development", "Naveen Balani, Rajeev Hathi")
                            )
                        );
                             
                        return null;
                    }
                })
            );
        }
    });
}

The actual invocation of the request by the client (with service name tracer-client) and consequent invocation of the service on the server side (service name tracer-server) is going to generate the following sample traces (taken from Jaeger UI):

Image Added

Example #7: Client and Server with asynchronous invocation (client-side)

In this example server-side implementation of the JAX-RS service is going to be the default one:

Code Block
java
java
@Produces( { MediaType.APPLICATION_JSON } )
@GET
public Collection<Book> getBooks() {
    return Arrays.asList(
        new Book("Apache CXF Web Service Development", "Naveen Balani, Rajeev Hathi")
    );
}

While the JAX-RS client implementation is going to perform the asynchronous invocation:

Code Block
java
java
final Future<Response> future = client
    .target("http://localhost:8282/books")
    .request()
    .accept(MediaType.APPLICATION_JSON)
    .async()
    .get();

In this respect, there is no difference from the caller prospective however a bit more work is going under the hood to transfer the active tracing span from JAX-RS client request filter to client response filter as in general those are executed in different threads (similarly to server-side asynchronous JAX-RS resource invocation). The actual invocation of the request by the client (with service name tracer-client) and consequent invocation of the service on the server side (service name tracer-server) is going to generate the following sample traces (taken from Jaeger UI):

Image Added

Distributed Tracing with OpenTracing and JAX-WS support

Distributed tracing in the Apache CXF is build primarily around JAX-RS 2.x implementation. However, JAX-WS is also supported but it requires to write some boiler-plate code and use OpenTracing Java API directly (the JAX-WS integration is going to be enhanced in the future). Essentially, from the server-side prospective the in/out interceptors, OpenTracingStartInterceptor and OpenTracingStopInterceptor respectively, should be configured as part of interceptor chains, either manually or using OpenTracingFeature. For example:

Code Block
java
java
final Tracer tracer = new Configuration("tracer")
    .withSampler(
        new SamplerConfiguration() 
            .withType(ConstSampler.TYPE) /* or any other Sampler */
            .withParam(1)
        )
    .withReporter(
        new ReporterConfiguration() 
            .withSender(
                new SenderConfiguration() /* or any other Sender configuration */
                    .withEndpoint("http://localhost:14268/api/traces")
            )
    )
    .getTracer();;

final JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
...
sf.getFeatures().add(new OpenTracingFeature(trace));
...
sf.create();

Similarly to the server-side, client-side needs own set of out/in interceptors, OpenTracingClientStartInterceptor and OpenTracingClientStopInterceptor (or OpenTracingClientFeature). Please notice the difference from server-side:  OpenTracingClientStartInterceptor becomes out-interceptor while OpenTracingClientStopInterceptor becomes in-interceptor. For example:

Code Block
java
java
final Tracer tracer = new Configuration("tracer") 
    .withSampler(
        new SamplerConfiguration() 
            .withType(ConstSampler.TYPE) /* or any other Sampler */
            .withParam(1)
        )
    .withReporter(
        new ReporterConfiguration()
            .withSender(
                new SenderConfiguration() /* or any other Sender configuration */
                    .withEndpoint("http://localhost:14268/api/traces")
            )
    )
    .getTracer();;
              
final JaxWsProxyFactoryBean sf = new JaxWsProxyFactoryBean();
...
sf.getFeatures().add(new OpenTracingClientFeature(tracer));
...
sf.create();

As it was mentioned before, you may use GlobalTracer utility class to pass the tracer around so, for example, any JAX-WS service will be able to retrieve the current tracer by invoking GlobalTracer.get() method.