JVM
这里我们提供俩类 JVM 指标采集方式,一种是 DDTrace,另一种方案是 Jolokia(已弃用),选择方式的建议如下:
- 推荐使用 DDTrace 进行采集 JVM 指标,Jolokia 也是可以的,用起来比较麻烦所以不推荐使用。
- 如果采集自己开发的 Java 应用 JVM 指标,推荐 DDTrace 方案,除了能采集 JVM 指标外,还能实现链路追踪(APM)数据采集。
配置¶
通过 DDTrace 采集 JVM 指标¶
DataKit 内置了 StatsD 采集器,用于接收网络上发送过来的 StatsD 协议的数据。此处我们利用 DDTrace 来采集 JVM 的指标数据,并通过 StatsD 协议发送给 Datakit。
这里推荐使用如下的 StatsD 配置来采集 DDTrace JVM 指标。将其拷贝到 conf.d/statsd
目录下,并命名为 ddtrace-jvm-statsd.conf
:
[[inputs.statsd]]
protocol = "udp"
## Address and port to host UDP listener on
service_address = ":8125"
## separator to use between elements of a statsd metric
metric_separator = "_"
drop_tags = ["runtime-id"]
metric_mapping = [
"jvm_:jvm",
"datadog_tracer_:ddtrace",
]
# 以下配置无需关注
delete_gauges = true
delete_counters = true
delete_sets = true
delete_timings = true
## Percentiles to calculate for timing & histogram stats
percentiles = [50.0, 90.0, 99.0, 99.9, 99.95, 100.0]
## Parses tags in the datadog statsd format
## http://docs.datadoghq.com/guides/dogstatsd/
parse_data_dog_tags = true
## Parses datadog extensions to the statsd format
datadog_extensions = true
## Parses distributions metric as specified in the datadog statsd format
## https://docs.datadoghq.com/developers/metrics/types/?tab=distribution#definition
datadog_distributions = true
## Number of UDP messages allowed to queue up, once filled,
## the statsd server will start dropping packets
allowed_pending_messages = 10000
## Number of timing/histogram values to track per-measurement in the
## calculation of percentiles. Raising this limit increases the accuracy
## of percentiles but also increases the memory usage and cpu time.
percentile_limit = 1000
## Max duration (TTL) for each metric to stay cached/reported without being updated.
#max_ttl = "1000h"
[inputs.statsd.tags]
# some_tag = "your-tag-value"
# some_other_tag = "your-other-tag-value"
目前可以通过 ConfigMap 方式注入采集器配置来开启采集器。
关于这里的配置说明:
service_address
此处设置成:8125
,指 DDTrace 将 jvm 指标发送出来的目标地址drop_tags
此处我们将runtime-id
丢弃,因为其可能导致时间线爆炸。如确实需要该字段,将其从drop_tags
中移除即可metric_mapping
在 ddtrace 发送出来的原始数据中,有俩类指标,它们的指标名称分别以jvm_
和datadog_tracer_
开头,故我们将它们统一规约到俩类指标集中,一个是jvm
,一个是ddtrace
自身运行指标
启动 Java 应用¶
一种可行的 JVM 部署方式如下:
java -javaagent:dd-java-agent.jar \
-Ddd.profiling.enabled=true \
-Ddd.logs.injection=true \
-Ddd.trace.sample.rate=1 \
-Ddd.service.name=my-app \
-Ddd.env=staging \
-Ddd.agent.host=localhost \
-Ddd.agent.port=9529 \
-Ddd.jmxfetch.enabled=true \
-Ddd.jmxfetch.check-period=1000 \
-Ddd.jmxfetch.statsd.host=127.0.0.1 \
-Ddd.jmxfetch.statsd.port=8125 \
-Ddd.version=1.0 \
-jar your-app.jar
注意:
- 关于
dd-java-agent.jar
包的下载,参见 这里 -
建议给如下几个字段命名:
service.name
用于表示该 JVM 数据来自哪个应用env
用于表示该 JVM 数据来自某个应用的哪个环境(如prod/testing/preprod
等)
-
此处几个选项的意义:
-Ddd.jmxfetch.check-period
表示采集频率,单位为毫秒-Ddd.jmxfetch.statsd.host=127.0.0.1
表示 Datakit 上 StatsD 采集器的连接地址-Ddd.jmxfetch.statsd.port=8125
表示 DataKit 上 StatsD 采集器的 UDP 连接端口,默认为 8125-Ddd.trace.health.xxx
DDTrace 自身指标数据采集和发送设置- 如果要开启链路追踪(APM)可追加如下两个参数(DataKit HTTP 地址)
-Ddd.agent.host=localhost
-Ddd.agent.port=9529
开启后,就能采集到 DDTrace 暴露出来的 jvm 指标。
Attention
实际采集到的指标,以 DataDog 的文档 为准。
指标¶
- 标签
其中每个指标有如下 tags (实际 tags 受 Java 启动参数以及 StatsD 配置影响)
标签名 | 描述 |
---|---|
env |
对应 DD_ENV |
host |
主机名 |
instance |
实例 |
jmx_domain |
|
metric_type |
|
name |
|
service |
|
type |
|
version |
- 指标列表
指标 | 描述 | 数据类型 | 单位 |
---|---|---|---|
heap_memory |
The total Java heap memory used | int | B |
heap_memory_committed |
The total Java heap memory committed to be used | int | B |
heap_memory_init |
The initial Java heap memory allocated | int | B |
heap_memory_max |
The maximum Java heap memory available | int | B |
non_heap_memory |
The total Java non-heap memory used. Non-heap memory is calculated as follows: Metaspace + CompressedClassSpace + CodeCache |
int | B |
non_heap_memory_committed |
The total Java non-heap memory committed to be used | int | B |
non_heap_memory_init |
The initial Java non-heap memory allocated | int | B |
non_heap_memory_max |
The maximum Java non-heap memory available | int | B |
thread_count |
The number of live threads | int | count |
gc_cms_count |
The total number of garbage collections that have occurred | int | count |
gc_major_collection_count |
The number of major garbage collections that have occurred. Set new_gc_metrics: true to receive this metric |
int | count |
gc_minor_collection_count |
The number of minor garbage collections that have occurred. Set new_gc_metrics: true to receive this metric |
int | count |
gc_parnew_time |
The approximate accumulated garbage collection time elapsed | int | ms |
gc_major_collection_time |
The approximate major garbage collection time elapsed. Set new_gc_metrics: true to receive this metric |
int | ms |
gc_minor_collection_time |
The approximate minor garbage collection time elapsed. Set new_gc_metrics: true to receive this metric |
int | ms |
重点解释一下以下几个指标:gc_major_collection_count
gc_minor_collection_count
gc_major_collection_time
gc_minor_collection_time
:
指标类型是 counter
也就是计数器,在采集过程中每次采集到指标后会和上一次的结果相减,并除以时间,也就是说 这些指标就是每秒的变化速率,并不是实际 JVM
中 MBean
中的值。
通过 Jolokia 采集 JVM 指标¶
JVM 采集器可以通过 JMX 来采取很多指标,并将指标采集到观测云,帮助分析 Java 运行情况。
配置¶
前置条件¶
安装或下载 Jolokia。DataKit 安装目录下的 data
目录中已经有下载好的 Jolokia jar 包。通过如下方式开启 Java 应用:
已测试的版本:
- JDK 20
- JDK 17
- JDK 11
- JDK 8
进入 DataKit 安装目录下的 conf.d/jvm
目录,复制 jvm.conf.sample
并命名为 jvm.conf
。示例如下:
[[inputs.jvm]]
# default_tag_prefix = ""
# default_field_prefix = ""
# default_field_separator = "."
# username = ""
# password = ""
# response_timeout = "5s"
## Optional TLS config
# tls_ca = "/var/private/ca.pem"
# tls_cert = "/var/private/client.pem"
# tls_key = "/var/private/client-key.pem"
# insecure_skip_verify = false
## Monitor Intreval
# interval = "60s"
# Add agents URLs to query
urls = ["http://localhost:8080/jolokia"]
## Add metrics to read
[[inputs.jvm.metric]]
name = "java_runtime"
mbean = "java.lang:type=Runtime"
paths = ["Uptime"]
[[inputs.jvm.metric]]
name = "java_memory"
mbean = "java.lang:type=Memory"
paths = ["HeapMemoryUsage", "NonHeapMemoryUsage", "ObjectPendingFinalizationCount"]
[[inputs.jvm.metric]]
name = "java_garbage_collector"
mbean = "java.lang:name=*,type=GarbageCollector"
paths = ["CollectionTime", "CollectionCount"]
tag_keys = ["name"]
[[inputs.jvm.metric]]
name = "java_threading"
mbean = "java.lang:type=Threading"
paths = ["TotalStartedThreadCount", "ThreadCount", "DaemonThreadCount", "PeakThreadCount"]
[[inputs.jvm.metric]]
name = "java_class_loading"
mbean = "java.lang:type=ClassLoading"
paths = ["LoadedClassCount", "UnloadedClassCount", "TotalLoadedClassCount"]
[[inputs.jvm.metric]]
name = "java_memory_pool"
mbean = "java.lang:name=*,type=MemoryPool"
paths = ["Usage", "PeakUsage", "CollectionUsage"]
tag_keys = ["name"]
[inputs.jvm.tags]
# some_tag = "some_value"
# more_tag = "some_other_value"
# ...
配置好后,重启 DataKit 即可。
Jolokia 指标¶
以下所有数据采集,默认会追加名为 host
的全局 tag(tag 值为 DataKit 所在主机名),也可以在配置中通过 [inputs.jvm.tags]
指定其它标签:
java_runtime
¶
- 标签
Tag | Description |
---|---|
host |
The hostname of the Jolokia agent/proxy running on. |
jolokia_agent_url |
Jolokia agent url path. |
- 指标列表
Metric | Description | Type | Unit |
---|---|---|---|
CollectionUsagecommitted |
The amount of memory in bytes that is committed for the Java virtual machine to use. | float | B |
CollectionUsageinit |
The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management. | float | B |
CollectionUsagemax |
The maximum amount of memory in bytes that can be used for memory management. | float | B |
CollectionUsageused |
The amount of used memory in bytes. | float | B |
Uptime |
The total runtime. | int | ms |
java_memory
¶
- 标签
Tag | Description |
---|---|
host |
The hostname of the Jolokia agent/proxy running on. |
jolokia_agent_url |
Jolokia agent url path. |
- 指标列表
Metric | Description | Type | Unit |
---|---|---|---|
CollectionUsagecommitted |
The amount of memory in bytes that is committed for the Java virtual machine to use. | float | B |
CollectionUsageinit |
The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management. | float | B |
CollectionUsagemax |
The maximum amount of memory in bytes that can be used for memory management. | float | B |
CollectionUsageused |
The amount of used memory in bytes. | float | B |
HeapMemoryUsagecommitted |
The total Java heap memory committed to be used. | int | B |
HeapMemoryUsageinit |
The initial Java heap memory allocated. | int | B |
HeapMemoryUsagemax |
The maximum Java heap memory available. | int | B |
HeapMemoryUsageused |
The total Java heap memory used. | int | B |
NonHeapMemoryUsagecommitted |
The total Java non-heap memory committed to be used. | int | B |
NonHeapMemoryUsageinit |
The initial Java non-heap memory allocated. | int | B |
NonHeapMemoryUsagemax |
The maximum Java non-heap memory available. | int | B |
NonHeapMemoryUsageused |
The total Java non-heap memory used. | int | B |
ObjectPendingFinalizationCount |
The count of object pending finalization. | int | count |
java_garbage_collector
¶
- 标签
Tag | Description |
---|---|
host |
The hostname of the Jolokia agent/proxy running on. |
jolokia_agent_url |
Jolokia agent url path. |
name |
The name of GC generation. |
- 指标列表
Metric | Description | Type | Unit |
---|---|---|---|
CollectionCount |
The number of GC that have occurred. | int | count |
CollectionTime |
The approximate GC collection time elapsed. | int | B |
CollectionUsagecommitted |
The amount of memory in bytes that is committed for the Java virtual machine to use. | float | B |
CollectionUsageinit |
The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management. | float | B |
CollectionUsagemax |
The maximum amount of memory in bytes that can be used for memory management. | float | B |
CollectionUsageused |
The amount of used memory in bytes. | float | B |
java_threading
¶
- 标签
Tag | Description |
---|---|
host |
The hostname of the Jolokia agent/proxy running on. |
jolokia_agent_url |
Jolokia agent url path. |
- 指标列表
Metric | Description | Type | Unit |
---|---|---|---|
CollectionUsagecommitted |
The amount of memory in bytes that is committed for the Java virtual machine to use. | float | B |
CollectionUsageinit |
The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management. | float | B |
CollectionUsagemax |
The maximum amount of memory in bytes that can be used for memory management. | float | B |
CollectionUsageused |
The amount of used memory in bytes. | float | B |
DaemonThreadCount |
The count of daemon thread. | int | count |
PeakThreadCount |
The peak count of thread. | int | count |
ThreadCount |
The count of thread. | int | count |
TotalStartedThreadCount |
The total count of started thread. | int | count |
java_class_loading
¶
- 标签
Tag | Description |
---|---|
host |
The hostname of the Jolokia agent/proxy running on. |
jolokia_agent_url |
Jolokia agent url path. |
- 指标列表
Metric | Description | Type | Unit |
---|---|---|---|
CollectionUsagecommitted |
The amount of memory in bytes that is committed for the Java virtual machine to use. | float | B |
CollectionUsageinit |
The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management. | float | B |
CollectionUsagemax |
The maximum amount of memory in bytes that can be used for memory management. | float | B |
CollectionUsageused |
The amount of used memory in bytes. | float | B |
LoadedClassCount |
The count of loaded class. | int | count |
TotalLoadedClassCount |
The total count of loaded class. | int | count |
UnloadedClassCount |
The count of unloaded class. | int | count |
java_memory_pool
¶
- 标签
Tag | Description |
---|---|
host |
The hostname of the Jolokia agent/proxy running on. |
jolokia_agent_url |
Jolokia agent url path. |
name |
The name of space. |
- 指标列表
Metric | Description | Type | Unit |
---|---|---|---|
CollectionUsagecommitted |
The amount of memory in bytes that is committed for the Java virtual machine to use. | float | B |
CollectionUsageinit |
The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management. | float | B |
CollectionUsagemax |
The maximum amount of memory in bytes that can be used for memory management. | float | B |
CollectionUsageused |
The amount of used memory in bytes. | float | B |
PeakUsagecommitted |
The total peak Java memory pool committed to be used. | int | B |
PeakUsageinit |
The initial peak Java memory pool allocated. | int | B |
PeakUsagemax |
The maximum peak Java memory pool available. | int | B |
PeakUsageused |
The total peak Java memory pool used. | int | B |
Usagecommitted |
The total Java memory pool committed to be used. | int | B |
Usageinit |
The initial Java memory pool allocated. | int | B |
Usagemax |
The maximum Java memory pool available. | int | B |
Usageused |
The total Java memory pool used. | int | B |