使用jmx exporter采集kafka指标

预置条件安装kafka、prometheus
使用JMX exporter暴露指标下载jmx exporter以及配置文件 。Jmx exporter中包含了kafka各个组件的指标,如server metrics、producer metrics、consumer metrics等,但这些指标并不是prometheus格式的,因此需要通过重命名方式转变为prometheus格式,重命名规则配置在kafka-2_0_0.yml中 。
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.12.0/jmx_prometheus_javaagent-0.12.0.jarwget https://raw.githubusercontent.com/prometheus/jmx_exporter/master/example_configs/kafka-2_0_0.ymlkafka集群启用监控启用的方式比较简单,只需要在kafka-server-start.sh中添加如下两行即可:
...#添加如下两行export JMX_PORT="7081"export KAFKA_OPTS="-javaagent:/Users/charlie.liu/kafka_2.13-3.2.3/monitor/jmx_prometheus_javaagent-0.12.0.jar=7072:/Users/charlie.liu/kafka_2.13-3.2.3/monitor/kafka-2_0_0.yml"COMMAND=$1case $COMMAND in-daemon)EXTRA_ARGS="-daemon "$EXTRA_ARGSshift;;*);;esacexec $base_dir/kafka-run-class.sh $EXTRA_ARGS kafka.Kafka "$@"【使用jmx exporter采集kafka指标】prometheus的配置文件如下,启动之后即可看到kafka的指标:
global:scrape_interval: 15sscrape_configs:- job_name: "kafka-cluster"static_configs:- targets: ["localhost:7072"]采集producer/consumer的指标kafka的producer metrics和consumer metrics的格式与kafka server metrics的格式不同,因此需要特定的重命名配置,可以将其追加到kafka-2_0_0.yml文件中:
# MBean: kafka.producer:type=producer-metrics,client-id=([-.w]+)- pattern: kafka.(.*)<type=(.*)-metrics, client-id=(.*)><>(.+):\w*name: kafka_$2-metricslabels:client_id: "$3"attr_name: "$4"help: "Kafka global client JMX metric $1 $2-metrics"type: GAUGE# MBean: kafka.producer:type=producer-node-metrics,client-id=([-.w]+),node-id=([0-9]+)# MBean: kafka.consumer:type=consumer-node-metrics,client-id=([-.w]+),node-id=([0-9]+)- pattern: kafka.(.*)<type=(.*)-metrics, client-id=(.*), node-id=(.*)><>(.+):\w*name: kafka_$2_$5-metricslabels:client_id: "$3"node_id: "$4"attr_name: "$5"help: "Kafka client JMX metric $1 $2-metrics"type: GAUGE- pattern: kafka.(.*)<type=(.*)-metrics, client-id=(.*), topic=(.*)><>(.+):\w*name: kafka_$2_$5-metricslabels:client_id: "$3"topic: "$4"attr_name: "$5"help: "Kafka client JMX metric $1 $2-metrics"type: GAUGE使用如下方式启动consumer,
KAFKA_OPTS="-javaagent:/kafka_2.13-3.2.3/monitor/jmx_prometheus_javaagent-0.12.0.jar=7073:/kafka_2.13-3.2.3/monitor/kafka-2_0_0.yml" kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic charlie-test1 --from-beginning并在prometheus的配置文件中添加consumer target,reload prometheus 配置即可发现consumer的指标,producer的指标采集与consumer类似 。
global:scrape_interval: 15sscrape_configs:- job_name: "kafka-cluster"static_configs:- targets: ["localhost:7072"]- job_name: "kafka-consumer"static_configs:- targets: ["localhost:7073"]官方给出了Jmx exporter的其他例子 。
注:由于不同的配置Jmx exporter生成的kafka指标名称是不一样的,因此并没有统一的kafka grafana dashboard,最好还是自己绘制 。

    经验总结扩展阅读