徹底搞懂監(jiān)控系統(tǒng),使用Prometheus監(jiān)控Spring Boot應用,自定義應用監(jiān)控指標!
前面我們介紹了使用Prometheus + Grafana 構建了監(jiān)控系統(tǒng),那么我們的應用平臺怎么監(jiān)控呢?應用平臺中的核心業(yè)務的執(zhí)行情況能否監(jiān)控呢?那么接下來我們使用Actuator,Micrometer,Prometheus和Grafana監(jiān)控Spring Boot應用程序,自定義應用監(jiān)控指標。
應用程序在生產(chǎn)環(huán)境中運行時,監(jiān)控其運行狀況是非常必要的。通過實時了解應用程序的運行狀況,才能在問題出現(xiàn)之前得到警告,也可以通監(jiān)控應用系統(tǒng)的運行狀況,優(yōu)化性能,提高運行效率。
一、監(jiān)控Spring Boot應用
下面我們以Spring Boot 為例,演示Prometheus如何監(jiān)控應用系統(tǒng)。
1.1 項目環(huán)境:
Spring Boot 2.3.7.release
micrometer-registry-prometheus 1.5.9
需要注意Spring Boot 和 micrometer的版本號。不同的micrometer版本支持的Spring Boot 版本也不相同。
1.2 Spring Boot集成 Micrometer
step1:首先創(chuàng)建Spring Boot項目,首先添加依賴如下:
這里引入了 io.micrometer 的 micrometer-registry-prometheus 依賴以及 spring-boot-starter-actuator 依賴,因為該包對 Prometheus 進行了封裝,可以很方便的集成到 Spring Boot 工程中。
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.5.9</version></dependency></dependencies>
需要注意Spring Boot 和 micrometer的版本號。不同的micrometer版本支持的Spring Boot 版本也不相同。
step2:修改配置文件,打開Actuator監(jiān)控端點
在 application.yml 中配置如下:
spring:application:name: PrometheusApp#Prometheus springboot監(jiān)控配置management:endpoints:web:exposure:include: '*'metrics:export:prometheus:enabled: truetags:application: ${spring.application.name} # 暴露的數(shù)據(jù)中添加application label
上面的配置中, include=* 配置為開啟 Actuator 服務,Spring Boot Actuator 自帶了一個/actuator/Prometheus 的監(jiān)控端點供給Prometheus 抓取數(shù)據(jù)。不過默認該服務是關閉的,所以,使用該配置將打開所有的 Actuator 服務。
step3:最后,啟動服務,然后在瀏覽器訪問 http://10.2.1.159:8080/actuator/prometheus ,就可以看到服務的一系列不同類型 metrics 信息。例如 http_server_requests_seconds、jvm_memory_used_bytes gauge、jvm_gc_memory_promoted_bytes_total counter 等。

到此,Spring Boot 工程集成 Micrometer 就已經(jīng)完成,接下里就要與 Prometheus 進行集成了。
1.3 將應用添加到Prometheus
前面Spring Boot應用已經(jīng)啟動成功,并暴露了/actuator/Prometheus的監(jiān)控端點。接下來我們將此應用添加到Prometheus。
step1:首先,修改 Prometheus 的配置文件 prometheus.yml ,添加上邊啟動的服務地址來執(zhí)行監(jiān)控。vim /usr/local/etc/prometheus.yml 。具體配置如下:
global:scrape_interval: 15sscrape_configs:- job_name: "prometheus"static_configs:- targets: ["localhost:9090"]- job_name: 'node'static_configs:- targets: ['10.2.1.231:9527']- job_name: 'prometheusapp'metrics_path: '/actuator/prometheus'static_configs:- targets: ['10.2.1.159:8080']
上面的prometheusapp 就是前面創(chuàng)建的Spring Boot 應用程序,也就是 Prometheus 需要監(jiān)控的服務地址。
step2:然后,重啟 Prometheus 服務,查看 Prometheus UI 界面確認 Target 是否添加成功。

我們也可以在 Graph 頁面執(zhí)行一個簡單的查詢,也是獲取 PrometheusApp服務的相關性能指標值。

二、使用 Grafana Dashboard 展示應用數(shù)據(jù)
前面我們已經(jīng)在Prometheus正常監(jiān)控Spring Boot應用的JVM性能指標數(shù)據(jù),接下來,我們配置 Grafana Dashboard 來優(yōu)雅直觀的展示出來這些監(jiān)控指標。
2.1 下載Grafana模板
之前介紹過Grafana 使用Dashboard 模板展示Prometheus的數(shù)據(jù),這里就不再重復了,直接在https://grafana.com/dashboards 下載Spring Boot的模板(這里使用的是編號4701)。

2.2 導入模板
下載成功后直接在Dashboards | Import 將json模板導入到Grafana 即可。

2.3 查看應用信息
導入完畢后,就可以看到 JVM的各項監(jiān)控指標,如果有多個應用,可以通過Application選擇我們想要查看的應用即可。

三、自定義監(jiān)控指標
前面我們在Spring Boot項目中集成Actuator和Micrometer實現(xiàn)了Spring Boot應用監(jiān)控,基本上覆蓋 JVM 各個層間的參數(shù)指標,并且配合 Grafana Dashboard 模板基本可以滿足我們日常對Spring Boot應用的監(jiān)控。
但是,對于核心業(yè)務是否也能夠監(jiān)控它們的執(zhí)行情況呢?答案是肯定的,Micrometer支持自定義監(jiān)控指標,實現(xiàn)業(yè)務方面的數(shù)據(jù)監(jiān)控。例如統(tǒng)計訪問某一個 API 接口的請求數(shù),統(tǒng)計實時在線人數(shù)、統(tǒng)計實時接口響應時間等。
接下來,我們以監(jiān)控所有API請求次數(shù)為例,演示如何自定義監(jiān)控指標并展示到Grafana 。
3.1 添加指標統(tǒng)計
step1:首先,在之前的Spring Boot項目中,創(chuàng)建CustomMetricsController 控制器,具體示例代碼如下:
public class CustomMetricsController {private MeterRegistry meterRegistry;/*** 訂單請求測試*/public String orderTest( String appId) {Counter.builder("metrics.request.count").tags("apiCode", "order").register(meterRegistry).increment();return "order請求成功:" +appId ;}/*** 產(chǎn)品請求測試*/public String productTest( String appId) {Counter.builder("metrics.request.count").tags("apiCode", "product").register(meterRegistry).increment();return "product請求成功:" +appId ;}}
如上所示,使用Counter 計數(shù)器定義了自定義指標參數(shù):metrics_request_count,來統(tǒng)計相關接口的請求次數(shù)。這里只是測試,所以直接在Controller類中進行統(tǒng)計。實際項目項目中,應該是使用AOP,或是攔截器的方式統(tǒng)計所有接口的請求信息,減少這種非關鍵代碼的侵入性。
step2:驗證測試,重新啟動Spring Boot 應用。分別訪問:http://10.2.1.159:8080/custom/metrics/order/{appId}和http://10.2.1.159:8080/custom/metrics/product/{appId} 接口,然后在 Promtheus 中查看自定義的指標數(shù)據(jù):metrics_request_count_total。

如上圖所示,我們自定義的監(jiān)控指標已經(jīng)在Prometheus中顯示了,說明我們在應用中配置的自定義監(jiān)控指標已經(jīng)成功。
3.2 創(chuàng)建Grafana數(shù)據(jù)面板
接下來,我們在 Grafana Dashboard展示我們自定義的監(jiān)控指標。其實也非常簡單,創(chuàng)建一個新的數(shù)據(jù)面板Panel 并添加 Query 查詢,相關的監(jiān)控指標就圖形化展示出來了。接下來演示在Grafana上創(chuàng)建數(shù)據(jù)面板。
step1:首先,頁面的右上角的Add panel | Add a new Panel,添加一個 Panel,并命名為:統(tǒng)計接口請求次數(shù)。可以選擇選擇想要展示的圖形,如:連線圖、柱狀圖等。

step2:然后,在panel的下方增加 Query 查詢,選擇數(shù)據(jù)源為之前定義的Prometheus-1,指標選擇之前自定義的指標數(shù)據(jù):metrics_request_count_total,點擊applay 保存之后,返回首頁就可以看到剛添加的 panel。具體如下圖所示:

如上圖所示,上面我們新增加的panel中成功顯示了我們自定義的監(jiān)控數(shù)據(jù)。繼續(xù)請求之前的應用接口,數(shù)據(jù)會正常刷新。說明Grafana上的指標數(shù)據(jù)展示配置成功。
最后
以上,我們就把Prometheus如何監(jiān)控Spring Boot應用,自定義應用監(jiān)控指標介紹完了。

