启用 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`
环境: `KC_CACHE_METRICS_HISTOGRAMS_ENABLED`

仅在启用指标时可用

true, false (默认)

http-metrics-histograms-enabled

为 HTTP 服务器请求的持续时间启用具有默认桶的直方图。

CLI: `--http-metrics-histograms-enabled`
环境: `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`
环境: `KC_HTTP_METRICS_SLOS`

仅在启用指标时可用

metrics-enabled

服务器是否应公开指标。

如果启用,指标将在 ` /metrics` 端点处可用。

CLI: `--metrics-enabled`
环境: `KC_METRICS_ENABLED`

true, false (默认)

在本页上