How to Setup Micrometer with Prometheus in Spring Boot 1.5


Sharing is caring!

Overview

In this article, I will revisit a little bit my old article, but still valid, regarding monitoring your services. As you can see in my previous article, I wrote about monitoring service in Go application. This time a little different. I will monitor Spring Boot application by using Micrometer and wiring it into Prometheus endpoint.

Micrometer

So, what is micrometer. To answer this, I just quote from their website. Micrometer provides a simple facade over the instrumentation clients for the most popular monitoring systems, allowing you to instrument your JVM based application code without vendor lock-in¹. In other words, Ideally I can embed micrometer to my spring-boot application without any problem.

Without too much non-sense and talking, let start the step by step.

Step by step

The demo will expose a REST endpoint and we will let micrometer to instrument the metrics. The metrics result then will be expose to prometheus endpoint. Therefore, for the start we need two spring boot dependencies, which are web and actuator modules. Whenever the project setup is ready, we can start to implement micrometer into our service.

Step 1: add micrometer dependencies into pom.xml

Note: in this article I am using micrometer version 1.0.2.

Step 2: Create a Configuration Bean to Register Your Metrics

By default, micrometer automatically configure three binders, which are JvmMemoryMetrics , UptimeMetrics and LogbackMetrics. If you are happy with this default setup, you can skip this step. However, sometimes we want to have another binder, such as HystrixMetricsBinder. Or  we want to have our own tags in the metrics by registering MeterRegistryCustomizer. All we need just register with @Bean annotation in our bean configuration.

Note: I register application tag in order to display the metrics into Grafana micrometer dashboard.

Step 3: Configure Timer in Controller

By default, spring boot will auto configure interceptors to record the metrics in our endpoints. It registered as Timer and produce a record with label http.server.requests. And again, maybe it is not enough. Sometimes instead of rely everything by default configuration, there are custom needs. Such as we want to generate the result as a histogram. We can leverage this need by adding @Timed annotation into our controller.

Note: @Timed annotation will produce get.counter.requests instead of http.server.requests (see value’s value). Then we enable histogram and percentiles in two values (0.95 and 0.99). Lastly, we add another tag for this endpoint which is version tag.

Step 4: Optional: Change Your Prometheus Endpoint

By default, the endpoint will be /prometheus. In case you want to change it, rename the property endpoints.prometheus.id.

Step 5: Run It

After you wait for a while, make some requests to your endpoint, then retrieve your metrics by accessing http://<your_ip>:<port>/myprometheus.

Conclusion

So simple, isn’t it? Just three steps and we have the metrics ready for our services. However, we need a better display for the metrics result. The metrics should be easy to see, read and understand. For that purpose, you need to export the result to Grafana and show them into your favorite dashboard. Also, you can set alerts based on your SLA or anything you like. You have the metrics. You have the data. They are all yours.

That’s all for now. Thank you for reading. You can download the sample on  github.

Author: ru rocker

I am a professional software developer with more than 10 years experiences. I am a certified Java Developer (SCJP and SCWCD). However, In the recent months, I have more interest in DevOps and start to become a polyglot developer. Python and Go-lang become my favorite programming languages besides Java.

Leave a Reply

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