Grafana Prometheus 通过JMX监控kafka

第三方kafka exporter方案

目前网上关于使用Prometheus 监控kafka的大部分资料都是使用一个第三方的
kafka exporter,他的原理大概就是启动一个kafka客户端,获取kafka服务器的信息,然后提供一些metric接口供Prometheus使用,随意它能展示的监控信息比较有限,只有每个主题的分区数,每秒/分钟消息数,消费组的lag数。但是kafka本身的JMX有提供500+的监控信息可以进行监控,当然不是说这这么监控指标都很重要,相比kafka exporter直接使用JMX可监控的选项会更多。
Grafana Prometheus 通过JMX监控kafka_第1张图片

Prometheus官方方案

Prometheus官方提供的jmx_exporter可以将JMX转换为Prometheus Metrics格式。

Prometheus JMX exporter使用方式选择

jmx_exporter提供两种用法:

  • 一种是启动独立的进程。JVM 启动时指定参数,暴露 JMX 的 RMI 接口,JMX_Exporter 调用 RMI 获取 JVM 运行时状态数据,转换为 Prometheus metrics 格式,并暴露端口让 Prometheus 采集。
  • 一种是JVM进程内启动,通过java agent的形式运行,进程内读取 JVM 运行时状态数据,转换为 Prometheus metrics 格式,并暴露端口让 Prometheus 采集。官方比较推荐使用这种方式。

使用JMX exporter监控kafka

在kafka-server-start.sh最上面添加下面的代码:

export KAFKA_OPTS="-javaagent:/opt/kafka_2.11-1.1.0/bin/jmx_prometheus_javaagent-0.19.0.jar=9990:/opt/kafka_2.11-1.1.0/bin/kafka-jmx.yml"

jmx_exporter官网下载最新的jmx_prometheus_javaagent-0.19.0.jar包。

kafka-jmx.yml

lowercaseOutputName: true

rules:
# Special cases and very specific rules
- pattern : kafka.server<>Value
  name: kafka_server_$1_$2
  type: GAUGE
  labels:
    clientId: "$3"
    topic: "$4"
    partition: "$5"
- pattern : kafka.server<>Value
  name: kafka_server_$1_$2
  type: GAUGE
  labels:
    clientId: "$3"
    broker: "$4:$5"
- pattern : kafka.coordinator.(\w+)<>Value
  name: kafka_coordinator_$1_$2_$3
  type: GAUGE

# Generic per-second counters with 0-2 key/value pairs
- pattern: kafka.(\w+)<>Count
  name: kafka_$1_$2_$3_total
  type: COUNTER
  labels:
    "$4": "$5"
    "$6": "$7"
- pattern: kafka.(\w+)<>Count
  name: kafka_$1_$2_$3_total
  type: COUNTER
  labels:
    "$4": "$5"
- pattern: kafka.(\w+)<>Count
  name: kafka_$1_$2_$3_total
  type: COUNTER

- pattern: kafka.server<>([a-z-]+)
  name: kafka_server_quota_$3
  type: GAUGE
  labels:
    resource: "$1"
    clientId: "$2"

- pattern: kafka.server<>([a-z-]+)
  name: kafka_server_quota_$4
  type: GAUGE
  labels:
    resource: "$1"
    user: "$2"
    clientId: "$3"

# Generic gauges with 0-2 key/value pairs
- pattern: kafka.(\w+)<>Value
  name: kafka_$1_$2_$3
  type: GAUGE
  labels:
    "$4": "$5"
    "$6": "$7"
- pattern: kafka.(\w+)<>Value
  name: kafka_$1_$2_$3
  type: GAUGE
  labels:
    "$4": "$5"
- pattern: kafka.(\w+)<>Value
  name: kafka_$1_$2_$3
  type: GAUGE

# Emulate Prometheus 'Summary' metrics for the exported 'Histogram's.
#
# Note that these are missing the '_sum' metric!
- pattern: kafka.(\w+)<>Count
  name: kafka_$1_$2_$3_count
  type: COUNTER
  labels:
    "$4": "$5"
    "$6": "$7"
- pattern: kafka.(\w+)<>(\d+)thPercentile
  name: kafka_$1_$2_$3
  type: GAUGE
  labels:
    "$4": "$5"
    "$6": "$7"
    quantile: "0.$8"
- pattern: kafka.(\w+)<>Count
  name: kafka_$1_$2_$3_count
  type: COUNTER
  labels:
    "$4": "$5"
- pattern: kafka.(\w+)<>(\d+)thPercentile
  name: kafka_$1_$2_$3
  type: GAUGE
  labels:
    "$4": "$5"
    quantile: "0.$6"
- pattern: kafka.(\w+)<>Count
  name: kafka_$1_$2_$3_count
  type: COUNTER
- pattern: kafka.(\w+)<>(\d+)thPercentile
  name: kafka_$1_$2_$3
  type: GAUGE
  labels:
    quantile: "0.$4"	

配置好kafka-server-start.sh后还需要重启kafka。

Prometheus配置

在Prometheus的prometheus.yml添加如下内容。注意端口号为KAFKA_OPTS配置的端口。

- job_name: "kafka_jmx"
    metrics_path: /metrics
    static_configs:
      - targets: ['192.168.249.1:9990','192.168.249.2:9990','192.168.249.3:9990']

配置完成后重新加载Prometheus配置文件就可以了。

grafana 配置

通过上面配置后,可以在grafan中找到对应的面板直接来用。

https://grafana.com/grafana/dashboards/18276-kafka-dashboard/

效果

你可能感兴趣的:(grafana,prometheus,kafka)