Chapter 108. OpenTelemetry
Chapter 108. OpenTelemetry Since Camel 3.5 The OpenTelemetry component is used for tracing and timing the incoming and outgoing Camel messages using OpenTelemetry . Events (spans) are captured for incoming and outgoing messages that are sent to/from Camel. 108.1. Dependencies Add the following dependency to your pom.xml for this component: <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-opentelemetry-starter</artifactId> </dependency> 108.2. Configuration properties The configuration properties for the OpenTelemetry tracer are: Option Default Description excludePatterns Sets exclude pattern(s) that will disable tracing for Camel messages that matches the pattern. The content is a Set<String> where the key is a pattern. The pattern uses the rules from Intercept. encoding false Sets whether the header keys need to be encoded (connector specific) or not. The value is a boolean. Dashes need for instances to be encoded for JMS property keys. 108.2.1. Using Camel OpenTelemetry Add the camel-opentelemetry component in your POM, in addition to any specific dependencies associated with the chosen OpenTelemetry compliant Tracer. To explicitly configure OpenTelemetry support, instantiate the OpenTelemetryTracer and initialize the camel context. You can optionally specify a Tracer , or alternatively it can be implicitly discovered using the Registry import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Tracer; OpenTelemetryTracer otelTracer = new OpenTelemetryTracer(); // By default it uses the DefaultTracer, but you can override it with a specific OpenTelemetry Tracer implementation. otelTracer.setTracer(...); // And then initialize the context otelTracer.init(camelContext); 108.3. Spring Boot Add the camel-opentelemetry-starter dependency, and then turn on the OpenTracing by annotating the main class with @CamelOpenTelemetry . The OpenTelemetryTracer is implicitly obtained from the camel context's Registry , unless a OpenTelemetryTracer bean has been defined by the application. 108.4. Using tracing strategy The camel-opentelemetry component starter allows you to trace not only the from/to Camel endpoints but also the Java Beans invoked from Camel processor/bean too. By default, Java beans invoked from Camel Processor or bean are categorized as another "span", that is, if user writes .to("bean:beanName?method=methodName") , it will be categorized under the same "span" with the from/to Camel endpoints. To categorize the Java beans invoked from Camel processor/bean under the same "span", you can use the org.apache.camel.opentelemetry.OpenTelemetryTracingStrategy class with the setTracingStrategy() option. import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Tracer; @Configuration public class CamelOtelConfiguration { public CamelOtelConfiguration(CamelContext camelContext, OpenTelemetryTracer tracer) { var strategy = new OpenTelemetryTracingStrategy(tracer); tracer.setTracingStrategy(strategy); camelContext.getCamelContextExtension().addInterceptStrategy(strategy); } } In case of too much data, you can filter the data by camel.opentelemetry.exclude-patterns property. camel: opentelemetry: exclude-patterns: - ## Set some ID here to filter ## 108.5. Java Agent Download the Java agent . This package includes the instrumentation agent as well as instrumentations for all supported libraries and all available data exporters. The package provides a completely automatic, out-of-the-box experience. Enable the instrumentation agent using the -javaagent flag to the JVM. java -javaagent:path/to/opentelemetry-javaagent.jar \ -jar myapp.jar By default, the OpenTelemetry Java agent uses OTLP exporter configured to send data to OpenTelemetry collector at http://localhost:4317 . Configuration parameters are passed as Java system properties ( -D flags) or as environment variables. See Configuring the agent and OpenTelemetry auto-configuration for the full list of configuration items. For example: java -javaagent:path/to/opentelemetry-javaagent.jar \ -Dotel.service.name=your-service-name \ -Dotel.traces.exporter=jaeger \ -jar myapp.jar 108.6. Adding the RouteID attribute to the span To identify the origin of the Route, it is necessary to add the RouteID attribute to the span. The workaround is to use the Span decorator from Camel. For this, add a custom decorator for each component you want to enrich with the route id. For example, if you want to print the route id in the direct component, the implementation is as follows: import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.tracing.SpanAdapter; import org.apache.camel.tracing.decorators.DirectSpanDecorator; public class RouteIDSpanDecorator extends DirectSpanDecorator { @Override public void pre(SpanAdapter span, Exchange exchange, Endpoint endpoint) { super.pre(span, exchange, endpoint); span.setTag("camel.route.id", exchange.getFromRouteId()); } } Add the file named org.apache.camel.tracing.SpanDecorator to src/main/resources/META-INF/services folder which contains the reference of the class . (for example, com.redhat.xxxx.RouteIDSpanDecorator). This allows the Camel tracing feature to load the custom Span decorator with the tag applied in the trace. 108.7. Spring Boot Auto-Configuration The component supports 2 options, which are listed below. Name Description Default Type camel.opentelemetry.encoding Activate or deactivate the dash encoding in headers (required by JMS) for messaging. Boolean camel.opentelemetry.exclude-patterns Sets exclude pattern(s) that will disable the tracing for the Camel messages that matches the pattern. Set 108.8. MDC Logging When MDC Logging is enabled for the active Camel context, the Trace ID and Span ID are added and removed from the MDC for each route, where the keys are trace_id and span_id , respectively.
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-opentelemetry-starter</artifactId> </dependency> import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Tracer; OpenTelemetryTracer otelTracer = new OpenTelemetryTracer(); // By default it uses the DefaultTracer, but you can override it with a specific OpenTelemetry Tracer implementation. otelTracer.setTracer(...); // And then initialize the context otelTracer.init(camelContext); import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Tracer; @Configuration public class CamelOtelConfiguration { public CamelOtelConfiguration(CamelContext camelContext, OpenTelemetryTracer tracer) { var strategy = new OpenTelemetryTracingStrategy(tracer); tracer.setTracingStrategy(strategy); camelContext.getCamelContextExtension().addInterceptStrategy(strategy); } } camel: opentelemetry: exclude-patterns: - ## Set some ID here to filter ## java -javaagent:path/to/opentelemetry-javaagent.jar -jar myapp.jar java -javaagent:path/to/opentelemetry-javaagent.jar -Dotel.service.name=your-service-name -Dotel.traces.exporter=jaeger -jar myapp.jar import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.tracing.SpanAdapter; import org.apache.camel.tracing.decorators.DirectSpanDecorator; public class RouteIDSpanDecorator extends DirectSpanDecorator { @Override public void pre(SpanAdapter span, Exchange exchange, Endpoint endpoint) { super.pre(span, exchange, endpoint); span.setTag("camel.route.id", exchange.getFromRouteId()); } }