Try   HackMD

Spring Boot Application health monitor

Actuator module收集內部info, 可以透過http或jmx取得 health check, metric, httptrack etc, 輔助監控及管理spring boot application, 另外也能結合外部監控系統, 像是Prometheus,Grafana, 利用dashboard, analysis, alert等進階操作

Spring Boot Actuator

導入actuator的depenedency 及接合外部系統的micrometer(如: ptometheus)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

endpoint:

大致可以分為三類

  1. application配置
  2. 度量指標: 用於監控的metrics, 像是jvm, thread pool, http request等
  3. 操作控制: 對application的關閉等操作功能

spring boot 2.x 以上, default expose endpoints有2個
/actuator/health: 基礎狀態
/actuator/info: properties當中info開頭的屬性
/actuator/heapdump: 取得heap檔案, 內存的snapshot, 方便日常定位问题的时候查看线程的情况。 主要展示了线程名、线程ID、线程的状态、是否等待锁资源、线程堆栈等信息
/actuator/threaddump: thread pool的snapshot
/actuator/env: 環境變數
/actuator/httptrace: http的請求狀況
/actuator/loggers: 查看配置的logger level
/actuator/metrics(常用): 查看有哪些指標可以看(ex: jvm.memory.max、system.cpu.usage),要再使用/actuator/metrics/{metric.name}分別查看各指標的詳細資訊

序號 參數 參數說明 是否監控 監控手段 重要度
JVM
1 jvm.memory.max JVM 最大內存
2 jvm.memory.committed JVM 可用內存 展示並監控堆內存和 Metaspace 重要
3 jvm.memory.used JVM 已用內存 展示並監控堆內存和 Metaspace 重要
4 jvm.buffer.memory.used JVM 緩衝區已用內存
5 jvm.buffer.count 當前緩衝區數
6 jvm.threads.daemon JVM 守護線程數 顯示在監控頁面
7 jvm.threads.live JVM 當前活躍線程數 顯示在監控頁面;監控達到閾值時報警 重要
8 jvm.threads.peak JVM 峰值線程數 顯示在監控頁面
9 jvm.classes.loaded 載入 classes 數
10 jvm.classes.unloaded 未載入的 classes 數
11 jvm.gc.memory.allocated GC 時,年輕代分配的內存空間
12 jvm.gc.memory.promoted GC 時,老年代分配的內存空間
13 jvm.gc.max.data.size GC 時,老年代的最大內存空間
14 jvm.gc.live.data.size FullGC 時,老年代的內存空間
15 jvm.gc.pause GC 耗時 顯示在監控頁面
TOMCAT
16 tomcat.sessions.created tomcat 已創建 session 數
17 tomcat.sessions.expired tomcat 已過期 session 數
18 tomcat.sessions.active.current tomcat 活躍 session 數
19 tomcat.sessions.active.max tomcat 最多活躍 session 數 顯示在監控頁面,超過閾值可報警或者進行動態擴容 重要
20 tomcat.sessions.alive.max.second 最多活躍 session 數持續時間
21 tomcat.sessions.rejected 超過 session 最大配置後,拒絕的 session 數 顯示在監控頁面,方便分析問題
22 tomcat.global.error 錯誤總數 顯示在監控頁面,方便分析問題
23 tomcat.global.sent 發送的位元組數
24 tomcat.global.request.max request 最長時間
25 tomcat.global.request 全局 request 次數和時間
26 tomcat.global.received 全局 received 次數和時間
27 tomcat.servlet.request servlet 的請求次數和時間
28 tomcat.servlet.error servlet 發生錯誤總數
29 tomcat.servlet.request.max servlet 請求最長時間
30 tomcat.threads.busy tomcat 繁忙線程 顯示在監控頁面,據此檢查是否有線程夯住
31 tomcat.threads.current tomcat 當前線程數(包括守護線程) 顯示在監控頁面 重要
32 tomcat.threads.config.max tomcat 配置的線程最大數 顯示在監控頁面 重要
33 tomcat.cache.access tomcat 讀取快取次數
34 tomcat.cache.hit tomcat 快取命中次數
CPU
35 system.cpu.count CPU 數量
36 system.load.average.1m load average 超過閾值報警 重要
37 system.cpu.usage 系統 CPU 使用率
38 process.cpu.usage 當前進程 CPU 使用率
39 http.server.requests http 請求調用情況 顯示 10 個請求量最大,耗時最長的 URL;統計非 200 的請求量 重要
40 process.uptime 應用已運行時間 顯示在監控頁面
41 process.files.max 允許最大控制台 配合當前打開控制台數使用
42 process.start.time 應用啟動時間點 顯示在監控頁面
43 process.files.open 當前打開控制台數 監控控制台使用率,超過閾值後報警 重要

/actuator/prometheus: 提供prometheus格式的metrics

配置方式
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include info, healt

預設base為/actuator

management.endpoints.web.base-path=/manage

修改預設的路徑名稱

#同时可以将health修改成healthcheck
management.endpoints.web.path-mapping.health=healthcheck
/actuator: 查看expose的enpoints


JMX(Java Management Extensions)

啟用Spring Boot應用的JMX(Java Management Extensions)支援,將資訊使用MBean封裝及管理數據,將使 /actuator/metrics 端點多出一些與JMX相關的指標。具體而言,這些指標通常與應用程式的性能、執行時狀態和其他相關信息有關。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Spring Boot 2.2.x+ default DISABLE expose JMX
spring.jmx.enabled=true
server.tomcat.mbeanregistry.enabled=true

啟用程式後
可以使用jdk的jconsole查看Mbeans

cmd: jconsole

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Figure. 連線

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Figure. 查看tomcat的參數

一些可能出現的與JMX相關的指標包括:

JVM 相關指標:

jvm.memory.max: JVM 最大內存
jvm.memory.committed: JVM 可用內存
jvm.memory.used: JVM 已用內存
jvm.threads.current: 當前活躍線程數
jvm.gc.pause: GC 耗時等

tomcat 相關指標

tomcat.sessions.created: Tomcat 已創建 session 數
tomcat.sessions.expired: Tomcat 已過期 session 數
tomcat.sessions.active.current: Tomcat 活躍 session 數
tomcat.sessions.active.max: Tomcat 最多活躍 session 數
tomcat.threads.current: Tomcat 當前線程數
等等

應用程式性能指標:

http.server.requests: HTTP 請求調用情況,包括請求量、耗時等
process.cpu.usage: 當前進程 CPU 使用率
process.uptime: 應用已運行時間
process.files.open: 當前打開控制台數等
啟用JMX通常會增加許多系統和應用程式級別的度量,具體增加了哪些指標可能還取決於應用程式所使用的其他庫和組件。

請注意,具體的指標可以根據你的應用程式、Spring Boot 版本以及所使用的依賴庫而有所不同。你可以查閱相關的Spring Boot文檔以獲取更詳細的信息。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


REF