Configure Prometheus and Grafana in Dockers
Prometheus is an open-source software application used for event monitoring and alerting. It records real-time metrics in a time series database with flexible queries and real-time alerting. A simple user interface where you can visualize, query, and monitor all the metrics.
Grafana is an open-source platform for data visualization, monitoring, and analysis. In Grafana, users can create dashboards with panels, each representing specific metrics over a set time-frame. Grafana supports graph, table, heatmap, and free text panels as well as integration with official and community-built plugins and apps that could be visualized too.
Why Prometheus and Grafana?
Prometheus dashboard also has simple graphs. But Grafana’s graphs are way better it can be customized according to the user requirements. That’s why, in this post, we’ll integrate Grafana with Prometheus to import and visualize our metrics data.
Spring Boot Actuator’s Prometheus endpoint
As you know Spring Boot has different actuator endpoints. Prometheus also one of the endpoint.
- /prometheus — returns metrics like the previous one, but formatted to work with a Prometheus server
The Prometheus endpoint exposes metrics data in a format that can be retrieved by a Prometheus server. You can see the exposed metrics data by navigating to the Prometheus endpoint (http://localhost:8080/actuator/prometheus). Here are the details you can see in your browser.
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{id="direct",} 81920.0
jvm_buffer_memory_used_bytes{id="mapped",} 0.0
# HELP jvm_threads_live The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live gauge
jvm_threads_live 23.0
# HELP tomcat_global_received_bytes_total
# TYPE tomcat_global_received_bytes_total counter
tomcat_global_received_bytes_total{name="http-nio-8080",} 0.0
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Allocation Failure",} 7.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Allocation Failure",} 0.232
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Metadata GC Threshold",} 0.01
jvm_gc_pause_seconds_count{action="end of major GC",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of major GC",cause="Metadata GC Threshold",} 0.302
# HELP jvm_gc_pause_seconds_max Time spent in GC pause
# TYPE jvm_gc_pause_seconds_max gauge
jvm_gc_pause_seconds_max{action="end of minor GC",cause="Allocation Failure",} 0.0
So how we monitor these metrics in a perfect manner? For that, we are going to use Prometheus docker image and visualize the data in Prometheus graph and Grafana. Let’s begin.
Configure Prometheus in Docker
- Download Prometheus Docker image
First, download the Prometheus docker image using docker pull
command.
$ docker pull prom/prometheus
Once it downloads you can see the prom/prometheus
image using $ docker image ls
2. Configure Prometheus endpoint
Next, we need to configure the Prometheus to retrieve metrics data from Spring Boot Actuator /prometheus
endpoint. For that, we need to create a prometheus.yml file and add the following configurations to the .yml file.
spring-actuator
job insidescrape_configs
section.The
metrics_path
is the path of the Actuator’sprometheus
endpoint. Thetargets
section and make sure to replace theHOST_IP
with your machine Ip.localhost
won’t work here because we’ll be connecting to the HOST machine from the docker container.
3. Run the Prometheus docker container in the background
$ docker run -d --name prometheus -p 9090:9090 -v <PATH_TO_prometheus.yml_FILE>:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
Here make sure to replace the
<PATH_TO_prometheus.yml_FILE>
with the PATH where you have stored the Prometheus configuration file.
you can see prom/prometheus
has been deployed in the container using docker container ls
command.
prom/prometheus Image
4. Visualizing Spring Boot Metrics from Prometheus dashboard
That’s it! You can now navigate to http://localhost:9090
explore the Prometheus dashboard. Here I have selected go_memstats_gc_cpu_fraction metric for demo purpose. You can define any metric here
You can check out the official Prometheus documentation to learn more about Prometheus Query Expressions
.
Setup Grafana in Docker
Now, we are going to integrate Prometheus metrics to Grafana dashboard. To do that, first, pull the Grafana docker image.
$ docker run -d --name grafana -p 3000:3000 grafana/grafana
You can type docker container ls
to see the list of Docker containers -
Integrate Grafana with Prometheus metrics
You can now navigate to http://localhost:3000
and log in to Grafana with the default username admin
and password admin
. You can see the home page as follows
After that click on Add Data Source and select Prometheus
Add HTTP URL as you defined in prometheus.yml
After configuring the data source, Its time to create a dashboard to visualize Prometheus metrics. For that click on New Dashboard in home page and click on Choose Visualization. Here you can select visualization (Graph, Singlestat, Guage, Table, Text, etc.). I used Graph mode to visualize metrics data.
After that, you have to provide Prometheus Query expression in Grafana’s query editor. For demo purpose, I have given go_memstats_gc_cpu_fraction visualize metrics in Grafana and finally, you can see the graph as follows.
Conclusion
Grafana is a powerful tool for visualization and that can be integrated with many data sources like Elasticsearch, AWS Cloudwatch, Azure Monitor, MySQL server, etc. Prometheus endpoint is widely used in Spring Boot to gather application details. This combination would help the developer to monitor application in real-time.
References