跳转至

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 指标。

Info

实际采集到的指标,以 DataDog 的文档 为准。

指标

参见这里

重点解释一下以下几个指标:gc_major_collection_count gc_minor_collection_count gc_major_collection_time gc_minor_collection_time:

指标类型是 counter 也就是计数器,在采集过程中每次采集到指标后会和上一次的结果相减,并除以时间,也就是说 这些指标就是每秒的变化速率,并不是实际 JVMMBean 中的值。

通过 Jolokia 采集 JVM 指标

JVM 采集器可以通过 JMX 来采取很多指标,并将指标采集到观测云,帮助分析 Java 运行情况。

配置

前置条件

安装或下载 Jolokia。DataKit 安装目录下的 data 目录中已经有下载好的 Jolokia jar 包。通过如下方式开启 Java 应用:

java -javaagent:/path/to/jolokia-jvm-agent.jar=port=8080,host=localhost -jar your_app.jar

已测试的版本:

  • 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] 指定其它标签:

 [inputs.jvm.tags]
  # some_tag = "some_value"
  # more_tag = "some_other_value"
  # ...

jvm

指标集包含 JVM runtime、memory、garbage collector、threading、class loading 和 memory pool 相关指标,v2 版本统一

Tags & Fields Description
host
(tag)
The hostname of the Jolokia agent/proxy running on.
jolokia_agent_url
(tag)
Jolokia agent url path.
name
(tag)
The name of memory pool.
CollectionCount The number of GC that have occurred.
Type: int | (count)
Unit: count
Tagged by: name
CollectionTime The approximate GC collection time elapsed.
Type: int | (count)
Unit: time,ms
Tagged by: name
CollectionUsagecommitted The amount of memory in bytes that is committed for the Java virtual machine to use.
Type: float | (gauge)
Unit: digital,B
Tagged by: name
CollectionUsageinit The amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management.
Type: float | (gauge)
Unit: digital,B
Tagged by: name
CollectionUsagemax The maximum amount of memory in bytes that can be used for memory management.
Type: float | (gauge)
Unit: digital,B
Tagged by: name
CollectionUsageused The amount of used memory in bytes.
Type: float | (gauge)
Unit: digital,B
Tagged by: name
DaemonThreadCount The count of daemon thread.
Type: int | (count)
Unit: count
HeapMemoryUsagecommitted The total Java heap memory committed to be used.
Type: int | (gauge)
Unit: digital,B
HeapMemoryUsageinit The initial Java heap memory allocated.
Type: int | (gauge)
Unit: digital,B
HeapMemoryUsagemax The maximum Java heap memory available.
Type: int | (gauge)
Unit: digital,B
HeapMemoryUsageused The total Java heap memory used.
Type: int | (gauge)
Unit: digital,B
LoadedClassCount The count of loaded class.
Type: int | (count)
Unit: count
NonHeapMemoryUsagecommitted The total Java non-heap memory committed to be used.
Type: int | (gauge)
Unit: digital,B
NonHeapMemoryUsageinit The initial Java non-heap memory allocated.
Type: int | (gauge)
Unit: digital,B
NonHeapMemoryUsagemax The maximum Java non-heap memory available.
Type: int | (gauge)
Unit: digital,B
NonHeapMemoryUsageused The total Java non-heap memory used.
Type: int | (gauge)
Unit: digital,B
ObjectPendingFinalizationCount The count of object pending finalization.
Type: int | (count)
Unit: count
PeakThreadCount The peak count of thread.
Type: int | (count)
Unit: count
PeakUsagecommitted The total peak Java memory pool committed to be used.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
PeakUsageinit The initial peak Java memory pool allocated.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
PeakUsagemax The maximum peak Java memory pool available.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
PeakUsageused The total peak Java memory pool used.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
ThreadCount The count of thread.
Type: int | (count)
Unit: count
TotalLoadedClassCount The total count of loaded class.
Type: int | (count)
Unit: count
TotalStartedThreadCount The total count of started thread.
Type: int | (count)
Unit: count
UnloadedClassCount The count of unloaded class.
Type: int | (count)
Unit: count
Uptime The total runtime.
Type: int | (count)
Unit: time,ms
Usagecommitted The total Java memory pool committed to be used.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
Usageinit The initial Java memory pool allocated.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
Usagemax The maximum Java memory pool available.
Type: int | (gauge)
Unit: digital,B
Tagged by: name
Usageused The total Java memory pool used.
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
Type: int | (gauge)
Unit: -

延伸阅读

文档评价

文档内容是否对您有帮助? ×