A developer spends a lot of time building features, but very little time asking an important question:
How healthy is our application when it is running? To be very specific we need to answer the below questions:
- How many requests are we serving?
- How much memory are we using?
- Is our database connection pool exhausted?
- Are users encountering errors?
Fortunately, Spring Boot and Prometheus make it surprisingly easy to answer these questions.
In this article, we'll:
- Enable Prometheus metrics in a Spring Boot application
- Configure Prometheus to scrape those metrics
- Explore the most useful metrics exposed by Spring Boot
Enable Prometheus metrics in a Spring Boot application
Enabling prometheus metrics is a 2-step process:
- Spring Boot uses Micrometer as its metrics facade. To expose metrics in a Prometheus-compatible format, add the following dependencies in pom.xml
The Actuator provides production-ready endpoints, while Micrometer translates metrics into a format Prometheus understands.
- Expose the Prometheus Endpoint. Add the following properties to application.properties:
When you start your application, you should be able to see the metrics in the /actuator/prometheus endpoint.
Now our application is exposing operational data that we can scrape using Prometheus.
Configure Prometheus to scrape those metrics
configure prometheus.yml with the "target" we want to scrape, metric path and a scrape interval.
Explore the most useful metrics exposed by Spring Boot
At this point, Prometheus is scraping metrics every 15 seconds. Let's take a look at some important ones and query using PromQL:
jvm_memory_used_bytes
This tells us how much heap memory the JVM is currently using.
Let's check how many '403's we have(unauthorized requests):
How many requests are made to each endpoint in the last couple of hours?
Without observability such metrics often go unnoticed. Usually when there's a firefighting situation these queries are like gold.
Traditionally, developers(and lot of Admins like me) focus on logs. Observability encourages us to ask different questions:
Is the application healthy?
Are requests succeeding?
Are users experiencing failures?
Is the database becoming a bottleneck?
Is the JVM under stress?
Metrics provide the answers.
In the next article, we'll connect Prometheus to Grafana and build dashboards that allow us to visualize these metrics in real time.








Top comments (0)