啟用 Keycloak 指標

了解如何啟用並公開伺服器的指標

Keycloak 內建支援指標。本指南將說明如何啟用及設定伺服器指標。

啟用指標

可以使用建置時選項 metrics-enabled 來啟用指標

bin/kc.[sh|bat] start --metrics-enabled=true

查詢指標

Keycloak 在管理介面的以下端點公開指標:

  • /metrics

有關管理介面的更多資訊,請參閱設定管理介面。端點的回應使用 application/openmetrics-text 內容類型,且基於 Prometheus (OpenMetrics) 文字格式。以下程式碼片段是回應的範例

# HELP base_gc_total Displays the total number of collections that have occurred. This attribute lists -1 if the collection count is undefined for this collector.
# TYPE base_gc_total counter
base_gc_total{name="G1 Young Generation",} 14.0
# HELP jvm_memory_usage_after_gc_percent The percentage of long-lived heap pool used after the last GC event, in the range [0..1]
# TYPE jvm_memory_usage_after_gc_percent gauge
jvm_memory_usage_after_gc_percent{area="heap",pool="long-lived",} 0.0
# HELP jvm_threads_peak_threads The peak live thread count since the Java virtual machine started or peak was reset
# TYPE jvm_threads_peak_threads gauge
jvm_threads_peak_threads 113.0
# HELP agroal_active_count Number of active connections. These connections are in use and not available to be acquired.
# TYPE agroal_active_count gauge
agroal_active_count{datasource="default",} 0.0
# HELP base_memory_maxHeap_bytes Displays the maximum amount of memory, in bytes, that can be used for memory management.
# TYPE base_memory_maxHeap_bytes gauge
base_memory_maxHeap_bytes 1.6781410304E10
# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.675188449054E9
# HELP system_load_average_1m The sum of the number of runnable entities queued to available processors and the number of runnable entities running on the available processors averaged over a period of time
# TYPE system_load_average_1m gauge
system_load_average_1m 4.005859375

...

可用的指標

下表總結了可用的指標群組

指標 描述

系統

一組與 CPU 和記憶體使用率相關的系統層級指標。

JVM

一組來自 Java 虛擬機 (JVM) 的指標,與 GC 和堆積相關。

資料庫

一組來自資料庫連線池的指標(如果使用資料庫)。

HTTP

一組來自 HTTP 端點的全域和個別指標

快取

一組來自 Infinispan 快取的指標。有關更多詳細資訊,請參閱設定分散式快取

相關選項

cache-metrics-histograms-enabled

啟用嵌入式快取指標的直方圖。

CLI: --cache-metrics-histograms-enabled
Env: KC_CACHE_METRICS_HISTOGRAMS_ENABLED

僅在啟用指標時可用

true, false (預設)

http-metrics-histograms-enabled

為 HTTP 伺服器請求的持續時間啟用帶有預設儲存桶的直方圖。

CLI: --http-metrics-histograms-enabled
Env: KC_HTTP_METRICS_HISTOGRAMS_ENABLED

僅在啟用指標時可用

true, false (預設)

http-metrics-slos

HTTP 伺服器請求的服務等級目標。

使用此選項來取代預設直方圖,或與其結合以新增額外的儲存桶。指定以逗號分隔的值列表,單位為毫秒。例如,儲存桶從 5 毫秒到 10 秒:5,10,25,50,250,500,1000,2500,5000,10000

CLI: --http-metrics-slos
Env: KC_HTTP_METRICS_SLOS

僅在啟用指標時可用

metrics-enabled

是否伺服器應公開指標。

如果啟用,則可在 /metrics 端點取得指標。

CLI: --metrics-enabled
Env: KC_METRICS_ENABLED

true, false (預設)

在此頁面上