apache-camelprometheusmetrics

Apache Camel migration from 2.x to 3.x/4.x lost compatibility in Route Metrics


In our application we use Apache Camel 2.25.4, but we're upgrading to a new version of Java, spring-boot and migrating Apache camel to 3.x then 4.x.

In the Apache camel Prometheus route metrics we add plus information's from the exchange. It was simple, we create an own MicrometerRoutePolicyNamingStrategy class and Override the Tags getTags(Route route, Exchange exchange) method then add it with setNamingStrategy to the MicrometerRoutePolicyFactory.

The 2.25.4 code was:

public class MyOwnMicrometerRoutePolicyNamingStrategy implements MicrometerRoutePolicyNamingStrategy {

    public String getName(Route route) {
        return "CamelRoutePolicy";
    }

    @Override
    public Tags getTags(Route route, Exchange exchange) {
        CamelMessageMetaData input = CamelMessageMetaData.getInput(exchange);
        return MicrometerRoutePolicyNamingStrategy.DEFAULT.getTags(route, exchange)
                .and("messageName", Objects.nonNull(input) ? input.getCode() : "Unknown");
    }

}

The metric looks like this:

CamelRoutePolicy_seconds_max{camelContext="my.test.channel.test",failed="false",messageName="MyTestMessageType1",routeId="main-test-route",serviceName="MicrometerRoutePolicyService",} 0.0

After the Apache Camel version update to 3.x our application route metrics solution didn't work: the MicrometerRoutePolicyNamingStrategygetTags method doesn't get the Exchange parameter which we usually used in the Override method.

I debugged: this method called from MicrometerRoutePolicy's MetricsStatistics sub-class's onExchangeDone method. The onExchangeDone has the Exchange parameter, but in the future versions it doesn't give it to the getTags method.

This onExchangeDone method is a private inner class's method so I couldn't Override it.

So our 2.25.4 solution won't work in the future version's.

I need help with migration, because I still need those plus tags - for Grafana - so I need to modify our application's Route's metrics to add plus tags with Exchange data's after we update Apache Camel version3.x and above.

Please help find me a solution to make the same changes in the route metrics.


Solution

  • We found the solution: There is a new class: MicrometerExchangeEventNotifierNamingStrategyDefault

    So we use/extends: MicrometerExchangeEventNotifierNamingStrategyDefault instead of MicrometerRoutePolicyNamingStrategy.

    public class MyOwnMicrometerExchangeEventNotifierNamingStrategy
            extends MicrometerExchangeEventNotifierNamingStrategyDefault {
    
        @Override
        public Tags getTags(CamelEvent.ExchangeEvent event, Endpoint endpoint) {
            CamelMessageMetaData metaData = CamelMessageMetaData.getInput(event.getExchange());
    
            if (Objects.isNull(metaData)){
                metaData = CamelMessageMetaData.getOutput(event.getExchange());
            }
    
            return super.getTags(event, endpoint)
                    .and("messageName", Objects.nonNull(metaData) ? metaData.getCode() : "Unknown");
        }
    }