Apache Camel: Create your own metric with Micrometer
By: Daniel S. Blanco
Tags: Micrometer, Prometheus, Spring Boot, Apache Camel, Grafana

Today we are going to see a very complete post about Apache Camel and Micrometer, and how we can present different metrics in Prometheus and Grafana. But in which we will not be able to do anything if not for the use of Spring Actuator. Before starting to indicate how we can achieve this, we are going to give a small introduction to each of the components to get an idea of what they are and what they are for.

Spring Actuator is another module of Spring Boot that enables the creation of endpoints associated with the application that will give us information, status, and metrics about it.

Micrometer is software that allows us to send information and metrics from one application to another specialized metrics software. The main idea is to use Micrometer as we would use a neutral API for the generation of metrics. And through which we can easily communicate with much other software dedicated exclusively to metrics, such as Prometheus, Elastic, Graphite, and many more. To do that, Micrometer counts with a specific module for each software.

Prometheus is an important software specialized in the monitoring and generation of alerts. It is an excellent tool for the registration of a numerical time series. Specialized in the supervision of dynamic services, it is designed to give confidence about the state of the application and predictive maintenance.

And finally, Grafana is another free software that allows the visualization of metrics. It allows the creation of dashboards and graphics from multiple sources such as Prometheus, Graphite, InfluxDB, etc.

Now that we have clearer concepts, let’s see how we can carry them out. The first thing will be to add the necessary dependencies. We will need the library to enable Spring Actuator, the library to enable the Micrometer component in Apache Camel, and finally, the library to allow the export to Prometheus through Micrometer.

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.apache.camel.springboot</groupId>

<artifactId>camel-micrometer-starter</artifactId>

</dependency>

<dependency>

<groupId>io.micrometer</groupId>

<artifactId>micrometer-registry-prometheus</artifactId>

</dependency>

We have already talked about the fact that Spring Actuator enables us to use different endpoints. By default, it enables the ‘/actuator’ itself, but also ‘/info’ and ‘/health’. Using the Spring configuration file, we must indicate that it also enables the endpoint for Prometheus, as follows.

#SPRING CONFIGURATION

management.endpoints.web.exposure.include=info, health, prometheus

If we start the application now and access the URL ‘http://localhost:8080/actuator/prometheus’ we will see different metrics available for Prometheus. And this is important because Prometheus will read all this information that we are giving it through that URL.

And although we will not go into detail in the configuration of Prometheus and Grafana, which we can easily start both through a docker-compose. To see the importance of that URL of Spring Actuator, we will see an example of how we can configure Prometheus so that it can read all the metrics associated with a specific application. We will do this through the configuration file prometheus.yml.

scrape_configs:

- job_name: 'prometheus'

scrape_interval: 1m

static_configs:

- targets: ['localhost:9090']

- job_name: 'camel-app'

scrape_interval: 1m

metrics_path: '/actuator/prometheus'

static_configs:

- targets: ['localhost:8080']

- job_name: 'grafana'

scrape_interval: 1m

metrics_path: '/metrics'

static_configs:

- targets: ['localhost:3000']

Once Prometheus has been started up, if we access its main screen we will see an indicator of whether the systems are up and connected.

Now comes the main section of this post, the creation of our own metrics thanks to Micrometer. This one offers us three options to create metrics, these are

Counter: It will allow us to count the number of invocations to a service.

Timer: It will allow us to measure the execution time of certain operations.

Summary: Allows us to add the values we indicate.

Therefore, to test its effectiveness we will create a mock, which will allow us to count the number of times it will be invoked:

In the code above, we have created a counter through the Micrometer component called simpleCounterMock. And if we access the Prometheus URL provided by Spring Actuator, we can see information about it:

# HELP simpleCounterMock_total

# TYPE simpleCounterMock_total counter

simpleCounterMock_total{camelContext="camel-1",} 3.0

Now that we are generating our own metrics, the next step will be to be able to visualize them, and for that, we will use Grafana. Within Grafana the first step will be to create a Data Source that has as its origin Prometheus.

The next step will be to create a dashboard through which we can visualize the data. In the dashboard, we will have to indicate the source of the data and the metrics we want to visualize. For our example, we will make two types of graphs. The first one will allow us to see the counter increment and the second one will be simply the counter. If you look, at the bottom of each graph there is the formula used. The generation of the graphs according to the use of the formulas is out of the scope of this post, but it’s also very interesting.

Now we will see how we can perform, for example, a metric that measures the time of a certain operation. In this case, we will have to invoke Micrometer twice, the first time before starting to measure the time and the second time to indicate that it stops counting.

The next step will be to generate other dashboards in Grafana that will allow us to visualize the generated metrics.

Finally, you can also use these formulas and visualize the metrics from Prometheus itself.

I hope it was a useful post that has allowed us to introduce important tools in the handling of metrics. And at the same time, to learn how to generate our own metrics that will help us in the maintenance and control of our applications.

As always, you can see all the code here.