Spring Boot迁移:如何为Camel XML路由添加执行时长日志
Hey Damien, great question—tracking Camel route execution times is key for building meaningful observability dashboards in Datadog, especially as you’re migrating to the latest Spring Boot. Since your routes are all XML-configured and already running, here are a few practical approaches tailored to your setup:
Camel’s interceptor system lets you wrap route logic to capture timing data directly. This is perfect if you only want to monitor specific routes and include the duration in your existing logs.
First, create a simple interceptor class:
import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.spi.Interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RouteTimerInterceptor implements Interceptor { private static final Logger LOG = LoggerFactory.getLogger(RouteTimerInterceptor.class); @Override public Processor wrap(Processor processor) { return exchange -> { long startTime = System.currentTimeMillis(); try { // Execute the original route logic processor.process(exchange); } finally { long durationMs = System.currentTimeMillis() - startTime; String routeId = exchange.getFromRouteId(); // Log in a Datadog-friendly format (easily parseable) LOG.info("Route execution | route_id={} | duration_ms={}", routeId, durationMs); } }; } }
Then attach it to your target route in your XML config:
<!-- Define the interceptor as a Spring bean --> <bean id="routeTimerInterceptor" class="com.your.package.RouteTimerInterceptor"/> <camelContext xmlns="http://camel.apache.org/schema/spring"> <!-- Apply interceptor only to this specific route --> <route id="your-monitored-route"> <intercept> <bean ref="routeTimerInterceptor"/> </intercept> <!-- Your existing route logic here --> <from uri="your-source-uri"/> <to uri="your-destination-uri"/> </route> </camelContext>
If you’re already using Spring Boot’s observability tools, this is the most robust approach. Camel integrates natively with Micrometer, which sends metrics directly to Datadog—no manual log parsing needed. This makes building dashboards with averages, percentiles, and error rates trivial.
- Add these dependencies to your build file:
<!-- Spring Boot Actuator for metrics endpoints --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Micrometer Datadog registry --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-datadog</artifactId> </dependency> <!-- Camel Micrometer integration --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-micrometer-starter</artifactId> </dependency>
- Configure Datadog and Camel metrics in
application.properties:
# Datadog connection settings management.metrics.export.datadog.api-key=your-datadog-api-key management.metrics.export.datadog.application-name=your-app-name management.metrics.export.datadog.enabled=true # Enable Camel's built-in metrics collection camel.metrics.enabled=true camel.metrics.route.enabled=true
Once set up, Camel will automatically collect metrics like camel.route.duration (execution time in milliseconds) for every route. You can directly use these metrics in Datadog to build dashboards showing average duration, p95/p99 percentiles, throughput, and more.
Camel publishes events when exchanges complete, which you can listen for to calculate execution times. This approach doesn’t require modifying your route XML at all.
Create a Spring component to listen for completion events:
import org.apache.camel.management.event.ExchangeCompletedEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class RouteCompletionListener { private static final Logger LOG = LoggerFactory.getLogger(RouteCompletionListener.class); @EventListener public void onExchangeCompleted(ExchangeCompletedEvent event) { // Get the timestamp when the exchange was created long startTime = event.getExchange().getProperty(Exchange.CREATED_TIMESTAMP, Long.class); long durationMs = System.currentTimeMillis() - startTime; String routeId = event.getExchange().getFromRouteId(); // Optional: Filter to only log specific routes if ("your-target-route".equals(routeId)) { LOG.info("Route {} finished in {} ms", routeId, durationMs); } } }
Just register this class as a Spring bean, and it will log execution times for (filtered) routes automatically.
Quick Recommendation
- Go with Option 2 if you’re building Datadog dashboards—metrics are far more reliable for statistical analysis than parsed logs.
- Use Option 1 or 3 if you just need to add duration to existing logs for quick debugging or ad-hoc analysis.
内容的提问来源于stack exchange,提问作者Damien

