Versions Compared

Key

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

...

Apache CXF uses HTTP headers to hand off tracing context from the client to the service and from the service to service. Those headers are used internally by HTraceProvider and HTraceClientProvider, but are configurable. The default header names are declared in the TracerHeaders class:

  • X-Trace-Id: contains a current trace IDX-Span-Id: contains a current span ID

...

Code Block
java
java
final Map<String, String> properties = new HashMap<String, String>();
final HTraceConfiguration conf = HTraceConfiguration.fromMap(propertiesproperties.put(Tracer.SPAN_RECEIVER_CLASSES_KEY, ...);
Traceproperties.addReceiver(new StandardOutSpanReceiver(conf));
        
final HTraceClientProvider provider = new HTraceClientProvider(new AlwaysSampler(conf));
final Response response = WebClientput(Tracer.SAMPLER_CLASSES_KEY, ...);
/**
 * For example:
 *
 * properties.put(Tracer.SPAN_RECEIVER_CLASSES_KEY, StandardOutSpanReceiver.class.getName());
 * properties.put(Tracer.SAMPLER_CLASSES_KEY, AlwaysSampler.class.getName());
 */
        
final Tracer tracer = new Tracer.Builder()
    .create("http://localhost:9000/catalog", Arrays.asList(provider))name("webclient")
    .acceptconf(MediaType.APPLICATION_JSONHTraceConfiguration.fromMap(properties))
    .getbuild();

The configuration based on using the standard JAX-RS Client is very similar:

Code Block
javajava
final Map<String, String> properties = new HashMap<String, String>();
final HTraceConfiguration conf = HTraceConfiguration.fromMap(properties);
Trace.addReceiver(new StandardOutSpanReceiver(conf));
                
                
final HTraceClientProvider provider = new HTraceClientProvider(new AlwaysSampler(conf));
final Client client = ClientBuilder.newClient().register(provider);

tracer);
final Response response = clientWebClient
    .targetcreate("http://localhost:8282/rest/api/people")
    .request()9000/catalog", Arrays.asList(provider))
    .accept(MediaType.APPLICATION_JSON)
    .get();

Configuring tracing header names

To change the default HTTP header names, used to transfer the tracing context from client to server, it is enough to define two properties only: TracerHeaders.HEADER_SPAN_ID and TracerHeaders.HEADER_TRACE_ID. For exampleThe configuration based on using the standard JAX-RS Client is very similar:

Code Block
java
java
final ClientConfiguration configMap<String, String> properties = WebClient.getConfig(clientnew HashMap<String, String>();
configproperties.getRequestContext().put(TracerHeadersTracer.HEADERSPAN_RECEIVER_SPANCLASSES_IDKEY, "CUSTOM_HEADER_SPAN_ID"...);
configproperties.getRequestContext().put(TracerHeadersTracer.HEADERSAMPLER_TRACECLASSES_IDKEY, "CUSTOM_HEADER_TRACE_ID");
Warning

It is very important to keep client and server HTTP headers configuration in sync, otherwise the server won't be able to establish the current tracing context properly.

...

...);
/**
 * For example:
 *
 * properties.put(Tracer.SPAN_RECEIVER_CLASSES_KEY, StandardOutSpanReceiver.class.getName());
 * properties.put(Tracer.SAMPLER_CLASSES_KEY, AlwaysSampler.class.getName());
 */
        
final Tracer tracer = new Tracer.Builder()
    .name("jaxrs-client")
    .conf(HTraceConfiguration.fromMap(properties))
    .build();
                
final HTraceClientProvider provider = new HTraceClientProvider(tracer);
final Client client = ClientBuilder.newClient().register(provider);

final Response response = client
    .target("http://localhost:8282/rest/api/people")
    .request()
    .accept(MediaType.APPLICATION_JSON)
    .get();

Configuring tracing header names

To change the default HTTP header names, used to transfer the tracing context from client to server, it is enough to define one property: TracerHeaders.HEADER_SPAN_ID. For example:

Code Block
java
java
final ClientConfiguration config = WebClient.getConfig(client);
config.getRequestContext().put(TracerHeaders.HEADER_SPAN_ID, "CUSTOM_HEADER_SPAN_ID");
Warning

It is very important to keep client and server HTTP headers configuration in sync, otherwise the server won't be able to establish the current tracing context properly.

Configuring Server
Anchor
configure.server
configure.server

Server configuration is a bit simpler than client one thanks to the feature class available, HTraceFeature. Depending on the way the Apache CXF is used to configure JAX-RS services, it could be part of JAX-RS application configuration, for example:

Code Block
java
java
@ApplicationPath("/")
public class CatalogApplication extends Application {
    @Override
    public Set<Object> getSingletons() {
        final HashMap<String, String> properties = new HashMap<String, String>();
        properties.put(Tracer.SPAN_RECEIVER_CLASSES_KEY, ...);
        properties.put(Tracer.SAMPLER_CLASSES_KEY, ...);
        /**
         * For example:
         *
  

...

Server configuration is a bit simpler than client one thanks to the feature class available, HTraceFeature. Depending on the way the Apache CXF is used to configure JAX-RS services, it could be part of JAX-RS application configuration, for example:

Code Block
javajava
@ApplicationPath("/")
public class CatalogApplication extends Application {
    @Override
    public Set<Object> getSingletons() {
        final Map<String, String> properties = new HashMap<String, String>();
       * properties.put("span.receiver"Tracer.SPAN_RECEIVER_CLASSES_KEY, StandardOutSpanReceiver.class.getName());
         * properties.put("sampler"Tracer.SAMPLER_CLASSES_KEY, AlwaysSampler.class.getName());
         */

        return return new HashSet<Object>(
            Arrays.asList(
                new HTraceFeature(HTraceConfiguration.fromMap(properties), "tracer")
            )
        );
    }
}

Or it could be configured using JAXRSServerFactoryBean as well, for example:

Code Block
java
java
final Map<String, String> properties = new HashMap<String, String>();
properties.put("span.receiver"Tracer.SPAN_RECEIVER_CLASSES_KEY, StandardOutSpanReceiver..class.getName());
properties.put("sampler"Tracer.SAMPLER_CLASSES_KEY, AlwaysSampler..class.getName());

final JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint(/* application instance */, JAXRSServerFactoryBean.class);
factory.setFeatures(Arrays.< Feature >asList(new HTraceFeature(HTraceConfiguration.fromMap(properties), "tracer")));
...
return factory.create();

...

As with the client, to change the default HTTP header names, used to establish the tracing context on the server, it is enough to define two properties onlysingle property: TracerHeaders.HEADER_SPAN_ID and TracerHeaders. HEADER_TRACE_ID. For example:

Code Block
java
java
final Map<String, Object> headers = new HashMap<String, Object>();
headers.put(TracerHeaders.HEADER_SPAN_ID, "CUSTOM_HEADER_SPAN_ID");
headers.put(TracerHeaders.HEADER_TRACE_ID, "CUSTOM_HEADER_TRACE_ID");
            
final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setProperties(headers);
...
sf.create();

...