首頁 > 軟體

使用jmx exporter採集kafka指標範例詳解

2022-11-05 14:00:45

預置條件

安裝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.jar
wget https://raw.githubusercontent.com/prometheus/jmx_exporter/master/example_configs/kafka-2_0_0.yml 

kafka叢集啟用監控

啟用的方式比較簡單,只需要在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=$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的指標:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: "kafka-cluster"
    static_configs:
    - targets: ["localhost:7072"]

採集producer/consumer的指標

kafka的producer metricsconsumer 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-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,

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: 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,最好還是自己繪製。

以上就是使用jmx exporter採集kafka指標範例詳解的詳細內容,更多關於jmx exporter採集kafka的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com