When you deploy springboot apps to production, you would like to know how your apps are doing. You would like to know how many requests are coming in, how much memory is used, how many errors are there, etc. In this article I would like to share my experience on how to monitor your springboot app with prometheus and grafana.
Assuming you already know what is prometheus and grafana. If not, you can check out their official websites. Basically prometheus is a time series database, and grafana is a visualization tool.
The data flow between springboot, prometheus and grafana is like this:
springboot <–(scripting) – prometheus <–(query) – grafana
so the task is to set up the data flow between springboot and prometheus, and then set up the data flow between prometheus and grafana.
Springboot and prometheus
What we can use is a library called micrometer. It is a metrics instrumentation library for JVM-based applications. It 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. It’s easy to configure , you only need to add the following dependency to your pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency>
Restart your springboot app, and you can see the metrics at http://localhost:8080/actuator/prometheus
We then need to configure prometheus to scrape the metrics from springboot.
Add the following to prometheus.yml:
scrape_configs: - job_name: 'springboot' metrics_path: '/actuator/prometheus' scrape_interval: 5s static_configs: - targets: ['localhost:8080']
Prometheus and grafana
Now we have the metrics in prometheus, we can use grafana to visualize them.
First we need to add prometheus as a datasource in grafana.
Here is the instruction) on how you can operate on GUI
alternatively you can provision grafana with promethous datasource
Then we can create a dashboard and add a graph panel. In the query editor, we can use the metrics from prometheus. e.g.:
good news you don’t have to start from scratch, you can import a dashboard from grafana dashboard like this one https://grafana.com/grafana/dashboards/12900-springboot-apm-dashboard/
That’s it. Now you have a dashboard to monitor your springboot app. You can also use the same approach to monitor other apps like nodejs, python, etc. The only thing you need to do is to find the right metrics for your app.