使用jmx exporter采集kafka指标

2022-11-14 19:15:07 浏览数 (1)

预置条件

安装kafka、prometheus

使用JMX exporter暴露指标

下载jmx exporter以及配置文件。Jmx exporter中包含了kafka各个组件的指标,如server metrics、producer metrics、consumer metrics等,但这些指标并不是prometheus格式的,因此需要通过重命名方式转变为prometheus格式,重命名规则配置在kafka-2_0_0.yml中。

代码语言:javascript复制
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.12.0/jmx_prometheus_javaagent-0.12.0.jar
wget https://raw.githubusercontent.com/prometheus/jmx_exporter/master/example_configs/kafka-2_0_0.yml 

kafka集群启用监控

启用的方式比较简单,只需要在kafka-server-start.sh中添加如下两行即可:

代码语言:javascript复制
...
#添加如下两行
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=$1
case $COMMAND in
  -daemon)
    EXTRA_ARGS="-daemon "$EXTRA_ARGS
    shift
    ;;
  *)
    ;;
esac

exec $base_dir/kafka-run-class.sh $EXTRA_ARGS kafka.Kafka "$@"

prometheus的配置文件如下,启动之后即可看到kafka的指标:

代码语言:javascript复制
global:
  scrape_interval: 15s

scrape_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文件中:

代码语言:javascript复制
# MBean: kafka.producer:type=producer-metrics,client-id=([-.w] )
- pattern: kafka.(.*)<type=(.*)-metrics, client-id=(.*)><>(. ):w*
  name: kafka_$2-metrics
  labels:
    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-metrics
  labels:
    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-metrics
  labels:
    client_id: "$3"
    topic: "$4"
    attr_name: "$5"
  help: "Kafka client JMX metric $1 $2-metrics"
  type: GAUGE

使用如下方式启动consumer,

代码语言:javascript复制
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类似。

代码语言:javascript复制
global:
  scrape_interval: 15s

scrape_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,最好还是自己绘制。

0 人点赞