Enabling step-by-step tracing in code

Camel includes a Tracer interceptor that makes it very easy to enable step-by-step logging of the message. This interceptor will log the current state of the message, and information about the processing step within your route in which the message is located.

This recipe will show you how to enable and configure Camel's Tracer.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.monitoring.trace package. The Spring XML files are located under src/main/resources/META-INF/spring and prefixed with trace.

How to do it...

In the XML DSL, set the trace attribute of the camelContext element to true:

<camelContext trace="true"
              xmlns="http://camel.apache.org/schema/spring">

In the Java DSL, fetch the associated Camel context, and set its tracing property to true:

public class TraceRouteBuilder extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    getContext().setTracing(true);

    // Route here
  }
}

The resulting log entries should look something like the following:

INFO  Tracer  - ID-grelber-local-52891-1374579218343-0-2 >>> (route1) from(direct://start) --> setBody[Simple: Tracing ${body}] <<< Pattern:InOnly, Headers:{breadcrumbId=ID-grelber-local-52891-1374579218343-0-1}, BodyType:String, Body:Hello Camel
INFO  Tracer  - ID-grelber-local-52891-1374579218343-0-2 >>> (route1) setBody[Simple: Tracing ${body}] --> mock://result <<< Pattern:InOnly, Headers:{breadcrumbId=ID-grelber-local-52891-1374579218343-0-1}, BodyType:String, Body:Tracing Hello Camel

Notice how, by default, the log output shows the breadcrumbId header associated with this message, the step within the route the message is at, and details about the message.

Note

The breadcrumb is a unique ID that allows you to trace the progress of a message within your log files.

How it works...

The Tracer is implemented as an interceptor strategy, meaning that it gets injected between each step of every route within the Camel context. The Tracer will log the contents of the message at each step.

There's more...

You can enable Tracing at runtime by setting the JMX attribute tracing on the Camel context's MBean. See the Configuring JMX recipe for how to enable JMX in your Camel integrations.

There's more...

You can change the default configuration used by the Tracer, including the log name and level, as well as message formatting details.

In the XML DSL, define a bean of type org.apache.camel.processor.interceptor.Tracer (the id does not matter), and set the properties that you would like to change. You can also define a bean of type org.apache.camel.processor.interceptor.DefaultTraceFormatter (again, the id does not matter), and set its formatting properties as you like. Camel uses Spring's registry to find these beans based on their type, which is why you do not see any code explicitly linking the tracer and formatter with the Camel context.

You still need to set the trace attribute of the camelContext element to true:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="…">
  <bean id="camelTracer"
        class="org.apache.camel.processor.interceptor
               .Tracer">
    <property name="logName"
              value="MyTracerLog"/>
  </bean>

  <bean id="traceFormatter"
        class="org.apache.camel.processor.interceptor
               .DefaultTraceFormatter">
    <property name="showHeaders"
              value="false"/>
    <property name="showProperties"
              value="true"/>
  </bean>

  <camelContext trace="true"
                xmlns="http://camel.apache.org/schema/spring">
    <!-- ... -->
  </camelContext>
</beans>

In the Java DSL, create an org.apache.camel.processor.interceptor.Tracer instance, customize its properties as desired, and add it as an intercept strategy to the associated Camel context:

public void configure() throws Exception {
  getContext().setTracing(true);

  Tracer tracer = new Tracer();
  tracer.setLogName("MyTracerLog");

  tracer.getDefaultTraceFormatter().setShowProperties(true);
  tracer.getDefaultTraceFormatter().setShowHeaders(false);

  getContext().addInterceptStrategy(tracer);
  // Define route(s) after tracer
}

Here is a sample of the type of output that you would see from the preceding configuration:

INFO  MyTracerLog  - ID-grelber-local-52917-1374580245804-0-2 >>> (route1) from(direct://start) --> setBody[Simple: Tracing ${body}] <<< Pattern:InOnly, Properties:{CamelToEndpoint=direct://start, CamelCreatedTimestamp=Tue Jul 23 07:50:46 EDT 2013}, BodyType:String, Body:Hello Camel
INFO  MyTracerLog  - ID-grelber-local-52917-1374580245804-0-2 >>> (route1) setBody[Simple: Tracing ${body}] --> mock://result <<< Pattern:InOnly, Properties:{CamelToEndpoint=direct://start, CamelCreatedTimestamp=Tue Jul 23 07:50:46 EDT 2013}, BodyType:String, Body:Tracing Hello Camel
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.42