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.xxxDDTrace 自身指标数据采集和发送设置- 如果要开启链路追踪(APM)可追加如下两个参数(DataKit HTTP 地址)
-Ddd.agent.host=localhost-Ddd.agent.port=9529
开启后,就能采集到 DDTrace 暴露出来的 jvm 指标。
Info
实际采集到的指标,以 DataDog 的文档 为准。
指标¶
参见这里
重点解释一下以下几个指标: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/samples 目录,复制 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"]
## v2+ override all measurement names to "jvm", default: v2
## If you want to use the old metric set, you can change it to "v1"
measurement_version = "v2"
## 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] 指定其它标签:
jvm¶
通过 Jolokia Java 平台 MXBean 采集的 JVM 运行时、内存、垃圾回收、线程、类加载和内存池指标。
| Tags & Fields | Description |
|---|---|
| host ( tag) |
Hostname reported by the Jolokia agent or proxy. |
| jolokia_agent_url ( tag) |
Jolokia agent URL used to collect the JVM metrics. |
| name ( tag) |
Garbage collector or memory pool name associated with fields tagged by name. |
| CollectionCount | Total number of garbage collections that have occurred for the collector. Type: int | (count) Unit: count Tagged by: name |
| CollectionTime | Approximate accumulated elapsed time spent in garbage collection. Type: int | (count) Unit: time,ms Tagged by: name |
| CollectionUsagecommitted | Committed memory in the post-GC collection usage snapshot for this memory pool. Type: float | (gauge) Unit: digital,B Tagged by: name |
| CollectionUsageinit | Initial memory size in the post-GC collection usage snapshot for this memory pool. Type: float | (gauge) Unit: digital,B Tagged by: name |
| CollectionUsagemax | Maximum memory in the post-GC collection usage snapshot for this memory pool, or -1 if undefined. Type: float | (gauge) Unit: digital,B Tagged by: name |
| CollectionUsageused | Used memory in the post-GC collection usage snapshot for this memory pool. Type: float | (gauge) Unit: digital,B Tagged by: name |
| DaemonThreadCount | Current number of live daemon threads. Type: int | (gauge) Unit: count |
| HeapMemoryUsagecommitted | Heap memory currently committed for JVM use. Type: int | (gauge) Unit: digital,B |
| HeapMemoryUsageinit | Initial heap memory size requested by the JVM. Type: int | (gauge) Unit: digital,B |
| HeapMemoryUsagemax | Maximum heap memory available to the JVM, or -1 if undefined. Type: int | (gauge) Unit: digital,B |
| HeapMemoryUsageused | Current heap memory used by the JVM. Type: int | (gauge) Unit: digital,B |
| LoadedClassCount | Current number of classes loaded in the JVM. Type: int | (gauge) Unit: count |
| NonHeapMemoryUsagecommitted | Non-heap memory currently committed for JVM use. Type: int | (gauge) Unit: digital,B |
| NonHeapMemoryUsageinit | Initial non-heap memory size requested by the JVM. Type: int | (gauge) Unit: digital,B |
| NonHeapMemoryUsagemax | Maximum non-heap memory available to the JVM, or -1 if undefined. Type: int | (gauge) Unit: digital,B |
| NonHeapMemoryUsageused | Current non-heap memory used by the JVM. Type: int | (gauge) Unit: digital,B |
| ObjectPendingFinalizationCount | Approximate current number of objects pending finalization. Type: int | (gauge) Unit: count |
| PeakThreadCount | Peak live thread count since the JVM started or the peak was reset. Type: int | (gauge) Unit: count |
| PeakUsagecommitted | Committed memory in the peak usage snapshot for this memory pool. Type: int | (gauge) Unit: digital,B Tagged by: name |
| PeakUsageinit | Initial memory size in the peak usage snapshot for this memory pool. Type: int | (gauge) Unit: digital,B Tagged by: name |
| PeakUsagemax | Maximum memory in the peak usage snapshot for this memory pool, or -1 if undefined. Type: int | (gauge) Unit: digital,B Tagged by: name |
| PeakUsageused | Used memory in the peak usage snapshot for this memory pool. Type: int | (gauge) Unit: digital,B Tagged by: name |
| ThreadCount | Current number of live threads. Type: int | (gauge) Unit: count |
| TotalLoadedClassCount | Total number of classes loaded since the JVM started. Type: int | (count) Unit: count |
| TotalStartedThreadCount | Total number of threads created and started since the JVM started. Type: int | (count) Unit: count |
| UnloadedClassCount | Total number of classes unloaded since the JVM started. Type: int | (count) Unit: count |
| Uptime | Elapsed time since the JVM started. Type: int | (gauge) Unit: time,ms |
| Usagecommitted | Memory currently committed for this memory pool. Type: int | (gauge) Unit: digital,B Tagged by: name |
| Usageinit | Initial memory size requested for this memory pool. Type: int | (gauge) Unit: digital,B Tagged by: name |
| Usagemax | Maximum memory available for this memory pool, or -1 if undefined. Type: int | (gauge) Unit: digital,B Tagged by: name |
| Usageused | Current memory used by this memory pool. Type: int | (gauge) Unit: digital,B Tagged by: name |
collector¶
多个采集器共用的采集可用性指标,用于上报目标在最近一次采集周期中是否成功采集。
| Tags & Fields | Description |
|---|---|
| instance ( tag) |
Server addr of the instance |
| job ( tag) |
Server name of the instance |
| up | Whether the collector successfully scraped the target during the last collection cycle: 1 means true and 0 means false. Type: int | (gauge) Unit: bool |