Enabling Apache Camel Metrics in Spring Boot Actuator Prometheus

Table of contents

Introduction

On one of the projects, I was faced with the task of turning on metrics-counters on routes during normal passage and when requests are rejected.

Given:

Popular approach

The most common approach I’ve come across on the Internet (in all articles from 2016 and newer, as well as in many videos) is as follows:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-micrometer-starter</artifactId>
</dependency>

camel.component.metrics.metric-registry=prometheusMeterRegistry

    @Bean
    public CamelContextConfiguration camelContextConfiguration() {

    return new CamelContextConfiguration() {
        @Override
        public void beforeApplicationStart(CamelContext camelContext) {
            camelContext.addRoutePolicyFactory(new MicrometerRoutePolicyFactory());
            camelContext.setMessageHistoryFactory(new MicrometerMessageHistoryFactory());
        }

        @Override
        public void afterApplicationStart(CamelContext camelContext) {

        }
    };
}

Reportedly and per video, this should result in the standard Apache Camel metrics on the / actuator / prometheus page such as CamelMessageHistory_seconds_count, and, in the future, the ability to add your own metrics using the syntax micrometer: counter: simple.counter

Problem

In my case, the problem is that I have this is does not work

Attempts (move) solutions

  1. I tried to take into account the oldness of the method given above and went through a bunch of combinations of dependency versions (in this case, I had some flexibility):

  2. I checked the code for typos many times. Along the way, I found out that they are present in many places on the Internet on this topic, including the Apache Camel documentation (we are talking about the similarity of the syntax of metrics through metrics: and micrometer: )

  3. A lot of debugging in different places from my service, Apache Camel, Spring Boot, Micrometer.

  4. In the process of debugging, the channel listener was revealed micrometer: does not “see” the link to prometheusMeterRegistry.

  5. I tried to explicitly add PrometheusMeterRegistry

@Bean(name = { MicrometerConstants.METRICS_REGISTRY_NAME })
public PrometheusMeterRegistry prometheusMeterRegistry() {
    return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
}
  1. This did not completely solve the problem. The link / actuator / metrics nevertheless appeared the required metrics (both the default from Apache Camel and new ones added in the code). But on page / actuator / prometheus Apache Camel metrics didn’t show up and also using micrometer: counter: simple.counter does not add simple.counter to the list of metrics.

  2. Metrics can be added to the list in approximately the following way:

@Bean
public void initCounter(MeterRegistry meterRegistry) {
    Counter.builder("simple.counter")
            .register(meterRegistry);
}
  1. But in this case they are not updated in the / actuator / prometheus page and have a constant value of 0.

  2. After some debugging, the following picture became clear – in the CamelContext instance there are 3 MeterRegistry objects (one JmxMeterRegistry and two PrometheusMeterRegistry). Which means that another PrometheusMeterRegistry is being initialized somewhere in addition to the one described above.

  3. Indeed, spring-boot-actuator-autoconfigure has its own Bean with this type.

As a result, it remains to initialize the same bean for both / actuator and Apache Camel.

Final (my) decision

The final solution looks like this:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-micrometer-starter</artifactId>
    <version>${org.apache.camel.springboot.version}</version>
</dependency>
@Bean(name = { MicrometerConstants.METRICS_REGISTRY_NAME, "prometheusMeterRegistry" })
public PrometheusMeterRegistry prometheusMeterRegistry(
        PrometheusConfig prometheusConfig, CollectorRegistry collectorRegistry, Clock clock) {
    return new PrometheusMeterRegistry(prometheusConfig, collectorRegistry, clock);
}

In this case, we have among the MeterRegistry only one JmxMeterRegistry and one PrometheusMeterRegistry which is available for both ApacheCamel and Spring Boot Actuator.

Also, the result of this solution is immediately noticeable, namely, both Apache Camel and custom metrics are available at / actuator / prometheus:

Some content at / actuator / prometheus
Some content at / actuator / prometheus

Conclusion

As a result of the work done, a working solution was found for integrating Apache Camel metrics into Spring Boot Actuator and throwing them into Prometheus.

Thank you for the attention.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *