参考地址: https://www.callicoder.com/spring-boot-actuator-metrics-monitoring-dashboard-prometheus-grafana/
Spring Boot Actuator metrics monitoring with Prometheus and Grafana
Welcome to the second part of the Spring Boot Actuator tutorial series. In the first part, you learned what spring-boot-actuator
module does, how to configure it in a spring boot application, and how to interact with various actuator endpoints.
In this article, you’ll learn how to integrate spring boot actuator with a monitoring system called Prometheusand a graphing solution called Grafana.
At the end of this article, you’ll have a Prometheus as well as a Grafana dashboard setup in your local machine where you’ll be able to visualize and monitor all the metrics generated from the Spring Boot application.
Prometheus
Prometheus is an open-source monitoring system that was originally built by SoundCloud. It consists of the following core components -
-
A data scraper that pulls metrics data over HTTP periodically at a configured interval.
-
A time-series database to store all the metrics data.
-
A simple user interface where you can visualize, query, and monitor all the metrics.
Grafana
Grafana allows you to bring data from various data sources like Elasticsearch, Prometheus, Graphite, InfluxDB etc, and visualize them with beautiful graphs.
It also lets you set alert rules based on your metrics data. When an alert changes state, it can notify you over email, slack, or various other channels.
Note that, Prometheus dashboard also has simple graphs. But Grafana’s graphs are way better. That’s why, in this post, we’ll integrate Grafana with Prometheus to import and visualize our metrics data.
Adding Micrometer Prometheus Registry to your Spring Boot application
Spring Boot uses Micrometer, an application metrics facade to integrate actuator metrics with external monitoring systems.
It supports several monitoring systems like Netflix Atlas, AWS Cloudwatch, Datadog, InfluxData, SignalFx, Graphite, Wavefront, Prometheus etc.
To integrate actuator with Prometheus, you need to add the micrometer-registry-prometheus
dependency -
<!-- Micrometer Prometheus registry -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Once you add the above dependency, Spring Boot will automatically configure a PrometheusMeterRegistry
and a CollectorRegistry
to collect and export metrics data in a format that can be scraped by a Prometheus server.
All the application metrics data are made available at an actuator endpoint called /prometheus
. The Prometheus server can scrape this endpoint to get metrics data periodically.
Exploring Spring Boot Actuator’s /prometheus Endpoint
Let’s explore the prometheus
endpoint that is exposed by Spring Boot when micrometer-registry-prometheus
dependency is available on the classpath.
First of all, you’ll start seeing the prometheus
endpoint on the actuator endpoint-discovery page (http://localhost:8080/actuator
) -
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-prometheus-endpoint.jpg" style="box-sizing: border-box;"></source>
The prometheus
endpoint exposes metrics data in a format that can be scraped by a Prometheus server. You can see the exposed metrics data by navigating to the prometheus
endpoint (http://localhost:8080/actuator/prometheus
) -
# 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
jvm_gc_pause_seconds_max{action="end of minor GC",cause="Metadata GC Threshold",} 0.0
jvm_gc_pause_seconds_max{action="end of major GC",cause="Metadata GC Threshold",} 0.0
# HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes 5.0657472E7
## More data ...... (Omitted for brevity)
Downloading and Running Prometheus using Docker
1. Downloading Prometheus
You can download the Prometheus docker image using docker pull
command like so -
$ docker pull prom/prometheus
Once the image is downloaded, you can type docker image ls
command to view the list of images present locally -
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest b82ef1f3aa07 5 days ago 119MB
2. Prometheus Configuration (prometheus.yml)
Next, We need to configure Prometheus to scrape metrics data from Spring Boot Actuator’s /prometheus
endpoint.
Create a new file called prometheus.yml
with the following configurations -
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'spring-actuator'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['HOST_IP:8080']
The above configuration file is an extension of the basic configuration file available in the Prometheus documentation.
The most important stuff to note in the above configuration file is the spring-actuator
job inside scrape_configs
section.
The metrics_path
is the path of the Actuator’s prometheus
endpoint. The targets
section contains the HOST and PORT of your Spring Boot application.
Please make sure to replace the HOST_IP
with the IP address of the machine where your Spring Boot application is running. Note that, localhost
won’t work here because we’ll be connecting to the HOST machine from the docker container. You must specify the network IP address.
3. Running Prometheus using Docker
Finally, Let’s run Prometheus using Docker. Type the following command to start a Prometheus server 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
Please make sure to replace the <PATH_TO_prometheus.yml_FILE>
with the PATH where you have stored the Prometheus configuration file.
After running the above command, docker will start the Prometheus server inside a container. You can see the list of all the containers with the following command -
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e036eb20b8ad prom/prometheus "/bin/prometheus --c…" 4 minutes ago Up 4 minutes 0.0.0.0:9090->9090/tcp prometheus
4. Visualizing Spring Boot Metrics from Prometheus dashboard
That’s it! You can now navigate to http://localhost:9090
to explore the Prometheus dashboard.
You can enter a Prometheus query expression inside the Expression
text field and visualize all the metrics for that query.
Following are some Prometheus graphs for our Spring Boot application’s metrics -
- System’s CPU usage -
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-metrics-dashboard-prometheus-graph.jpg" style="box-sizing: border-box;"></source>
- Response latency of a slow API -
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-prometheus-dashboard-graph-example.jpg" style="box-sizing: border-box;"></source>
You can check out the official Prometheus documentation to learn more about Prometheus Query Expressions
.
Downloading and running Grafana using Docker
Type the following command to download and run Grafana using Docker -
$ docker run -d --name=grafana -p 3000:3000 grafana/grafana
The above command will start Grafana inside a Docker container and make it available on port 3000
on the Host machine.
You can type docker container ls
to see the list of Docker containers -
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf9196b30d0d grafana/grafana "/run.sh" Less than a second ago Up 5 seconds 0.0.0.0:3000->3000/tcp grafana
e036eb20b8ad prom/prometheus "/bin/prometheus --c…" 16 minutes ago Up 16 minutes 0.0.0.0:9090->9090/tcp prometheus
That’s it! You can now navigate to http://localhost:3000
and log in to Grafana with the default username admin
and password admin
.
Configuring Grafana to import metrics data from Prometheus
Follow the steps below to import metrics from Prometheus and visualize them on Grafana:
1. Add the Prometheus data source in Grafana
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-prometheus-grafana-dashboard.jpg" style="box-sizing: border-box;"></source>
2. Create a new Dashboard with a Graph
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-grafana-dashboard-create-graph.jpg" style="box-sizing: border-box;"></source>
3. Add a Prometheus Query expression in Grafana’s query editor
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-grafana-dashboard-prometheus-metrics-graph.jpg" style="box-sizing: border-box;"></source>
4. Visualize metrics from Grafana’s dashboard
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-actuator-dashboard-visualize-prometheus-metrics-graph.jpg" style="box-sizing: border-box;"></source>
You can find the complete source code for the Actuator Demo application on Github.
Read the First Part: Spring Boot Actuator: Health check, Auditing, Metrics gathering and Monitoring
相关推荐
Spring Boot Actuator 执行器运行原理详解 Spring Boot Actuator 执行器是 Spring Boot 框架中的一种功能强大且实用的模块,用于监视和管理 Spring Boot 应用程序。在本文中,我们将详细介绍 Spring Boot Actuator ...
zabbix5.0模板-spring boot actuator 1.x 监控模板
赠送jar包:spring-boot-actuator-2.2.0.RELEASE.jar; 赠送原API文档:spring-boot-actuator-2.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-2.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
It will also cover what's been added to the new Spring Boot 2 release, including Spring Framework 5 features like WebFlux, Security, Actuator and the new way to expose Metrics through Micrometer ...
总结来说,Spring的Actuator、Prometheus的度量收集和Grafana的可视化能力共同构建了一个强大而灵活的监控解决方案。这对于开发者来说,无论是开发阶段还是生产环境,都能提供有力的支持,帮助他们及时发现并解决...
赠送jar包:spring-boot-actuator-2.6.3.jar; 赠送原API文档:spring-boot-actuator-2.6.3-javadoc.jar; 赠送源代码:spring-boot-actuator-2.6.3-sources.jar; 赠送Maven依赖信息文件:spring-boot-actuator-...
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
### 使用 Spring Boot Actuator 进行应用监控 随着微服务架构的广泛应用,系统变得越来越分散且复杂。在这样的背景下,微服务之间的通信和协作成为常态,这也为问题定位带来了挑战。在这种情况下,如何有效地监控...
赠送jar包:spring-boot-actuator-autoconfigure-2.6.3.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.6.3-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.6.3-sources.jar; 赠送...
然后,我们需要添加依赖项,包括 `spring-boot-starter-actuator` 和 `micrometer-registry-prometheus`。 在 `application.yml` 文件中,我们需要增加以下配置项: ``` management: metrics: export: ...
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
赠送jar包:spring-boot-actuator-autoconfigure-2.5.5.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.5.5-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.5.5-sources.jar; 赠送...
Spring Boot Actuator 和 Spring Boot Admin 是两个非常重要的组件,它们为Spring Boot应用提供了监控和管理的能力。本篇文章将深入探讨这两个组件的整合过程,并通过源码分析来理解其内部工作原理。 Spring Boot ...
赠送jar包:spring-boot-actuator-2.5.5.jar; 赠送原API文档:spring-boot-actuator-2.5.5-javadoc.jar; 赠送源代码:spring-boot-actuator-2.5.5-sources.jar; 赠送Maven依赖信息文件:spring-boot-actuator-...
### Spring Boot 2.0 Actuator 监控参数说明 #### 一、Spring Boot 2.0 ...通过以上步骤,Spring Boot 2.0项目不仅能够充分利用Actuator模块的强大功能,还能够借助于Prometheus实现更加灵活和细致的监控需求。
赠送jar包:spring-boot-actuator-autoconfigure-2.2.0.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.2.0....
赠送jar包:spring-boot-actuator-autoconfigure-2.0.4.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.0.4....
在 Prometheus 的配置文件 `prometheus.yml` 中,我们需要添加针对 Spring Boot 应用的抓取配置,指定 `metrics_path` 为 `/actuator/prometheus`,并设置目标应用的 `targets`,例如 `['localhost:8080']`。...
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 ...spring-boot-actuator:Spring Boot Actuator 使用示例 spring-boot-admin-simple:Spring Boot Admin 的使用示例
Spring Boot Actuator从未授权访问到getshell