欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 【Kafka】对 kafka 消费程序客户端进行监控采集

【Kafka】对 kafka 消费程序客户端进行监控采集

2024/10/24 8:31:34 来源:https://blog.csdn.net/Mrerlou/article/details/140791475  浏览:    关键词:【Kafka】对 kafka 消费程序客户端进行监控采集

前言

对于 Kafka 组件而言,我们通常会对 kafka 服务端添加一些监控,来确保服务的稳定性,虽然有 kafka-exporter 来对消费者进行监控,但是指标很少,对于生产者和消费者更细粒度的监控就无法做到了。只能将监控部署在客户端上,这样我们就能拿到更加详细的监控数据。

例如:对于消费者来说,我们可以获取到消费者重平衡次数,每次poll 数据的条数等等。

这里梳理下如何对消费者添加 JMX 监控。

一、所需组件

这里我们只针对于 JAVA 语言的 KAFKA 客户端为例,需要以下东西:

  • kafka-client:3.4.1
  • JMX-exporter

需要提前准备好 kafka 服务和创建好topic。

二、编写测试代码

1. pom 文件

 <properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><kafka.version>3.4.1</kafka.version></properties><dependencies><dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>${kafka.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.3.0</version><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>

2. 生产者代码

public class ProducerDemo {public static void main(String[] args) {//主题(当主题不存在,自动创建主题)String topic = "test";//配置Properties properties = new Properties();//kafka服务器地址properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.195.132:9092");//反序列化器properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class);//生产者KafkaProducer<String,String> kafkaProducer = new KafkaProducer(properties);//生产信息for (int i = 0; i < 100; i++) {String msg = String.format("hello,第%d条信息", i);//消息(key可以为null,key值影响消息发往哪个分区)ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(topic, String.valueOf(i), msg);//发送kafkaProducer.send(producerRecord);System.out.println("发送第"+i+"条信息");}//关闭kafkaProducer.close();}
}

3. 消费者代码

public class ConsumerDemo {public static void main(String[] args) throws Exception{//主题String topic = "test";//配置Properties properties = new Properties();//kafka服务器地址properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.195.132:9092");//k,v的序列化器properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);//消费者分组properties.put(ConsumerConfig.GROUP_ID_CONFIG,"Consumer-Group-1");//offset重置模式properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");//消费者KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer(properties);//订阅(可以订阅多个主题)kafkaConsumer.subscribe(Collections.singletonList(topic));//消费while (true){//获取信息ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofMillis(1000));//遍历records.forEach(o->{System.out.println(String.format("topic==%s,offset==%s,key==%s,value==%s",o.topic(),o.offset(),o.key(),o.value()));});//睡眠Thread.sleep(500);}}
}

4. 打包

mvn clean package

5. 测试生产和消费

生产数据测试:

java -cp kafka-1.0-jar-with-dependencies.jar      ProducerDemo

在这里插入图片描述
消费数据测试:

java -cp kafka-1.0-jar-with-dependencies.jar      ConsumerDemo

在这里插入图片描述

三、使用jmx-agent

1. 下载

https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.18.0/jmx_prometheus_javaagent-0.18.0.jar

2. 编写kafka-client.yaml

---
lowercaseOutputName: true
lowercaseOutputLabelNames: true
whitelistObjectNames:- "kafka.consumer:*"- "kafka.producer:*"
blacklistObjectNames:- "kafka.admin.client:*"- "kafka.consumer:type=*,id=*"- "kafka.producer:type=*,id=*"- "kafka.*:type=kafka-metrics-count,*"
rules:# "kafka.consumer:type=app-info,client-id=*"# "kafka.producer:type=app-info,client-id=*"- pattern: "kafka.(.+)<type=app-info, client-id=(.+)><>(.+): (.+)"value: 1name: kafka_$1_app_infocache: truelabels:client_type: $1client_id: $2$3: $4type: UNTYPED# "kafka.consumer:type=consumer-metrics,client-id=*, protocol=*, cipher=*"# "kafka.consumer:type=type=consumer-fetch-manager-metrics,client-id=*, topic=*, partition=*"# "kafka.producer:type=producer-metrics,client-id=*, protocol=*, cipher=*"- pattern: "kafka.(.+)<type=(.+), (.+)=(.+), (.+)=(.+), (.+)=(.+)><>(.+):"name: kafka_$1_$2_$9type: GAUGEcache: truelabels:client_type: $1$3: "$4"$5: "$6"$7: "$8"# "kafka.consumer:type=consumer-node-metrics,client-id=*, node-id=*"# "kafka.consumer:type=consumer-fetch-manager-metrics,client-id=*, topic=*"# "kafka.producer:type=producer-node-metrics,client-id=*, node-id=*"# "kafka.producer:type=producer-topic-metrics,client-id=*, topic=*"- pattern: "kafka.(.+)<type=(.+), (.+)=(.+), (.+)=(.+)><>(.+):"name: kafka_$1_$2_$7type: GAUGEcache: truelabels:client_type: $1$3: "$4"$5: "$6"# "kafka.consumer:type=consumer-fetch-manager-metrics,client-id=*"# "kafka.consumer:type=consumer-metrics,client-id=*"# "kafka.producer:type=producer-metrics,client-id=*"- pattern: "kafka.(.+)<type=(.+), (.+)=(.+)><>(.+):"name: kafka_$1_$2_$5type: GAUGEcache: truelabels:client_type: $1$3: "$4"- pattern: "kafka.(.+)<type=(.+)><>(.+):"name: kafka_$1_$2_$3cache: truelabels:client_type: $1

yaml 参考:https://github.com/confluentinc/jmx-monitoring-stacks/blob/main/shared-assets/jmx-exporter/kafka_client.yml

3. 重新启动消费程序

java -cp kafka-1.0-jar-with-dependencies.jar -javaagent:/opt/javaProject/jmx_prometheus_javaagent-0.18.0.jar=1234:/opt/javaProject/kafka_client.yml ConsumerDemo
4. 打开web ui

访问http://xxx.xxx.xxx.xxx:1234
在这里插入图片描述
这里就能看到我们采集到了kafka 消费者实例的详细指标。我们就可以和Prometheus进行集成,这里就不详细介绍了。

四、具体指标

想要获取更多的指标和指标具体的用途可以参考官网的文档
https://kafka.apache.org/documentation/#consumer_monitoring

具体指标如下:
以下是添加了中文翻译后的Kafka Consumer Metrics表格:

Consumer Coordinator Metrics

METRIC NAMEDESCRIPTIONMBEAN NAME中文描述
commit-latency-avgThe average time taken for a commit requestkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)提交请求的平均时间
commit-latency-maxThe max time taken for a commit requestkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)提交请求的最大时间
commit-rateThe number of commit calls per secondkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)每秒提交调用次数
commit-totalThe total number of commit callskafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)提交调用的总次数
assigned-partitionsThe number of partitions currently assigned to this consumerkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)当前分配给该消费者的分区数量
heartbeat-response-time-maxThe max time taken to receive a response to a heartbeat requestkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)接收到心跳请求响应的最大时间
heartbeat-rateThe average number of heartbeats per secondkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)每秒心跳次数的平均值
heartbeat-totalThe total number of heartbeatskafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)心跳的总次数
join-time-avgThe average time taken for a group rejoinkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)加入组的平均时间
join-time-maxThe max time taken for a group rejoinkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)加入组的最大时间
join-rateThe number of group joins per secondkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)每秒加入组的次数
join-totalThe total number of group joinskafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)加入组的总次数
sync-time-avgThe average time taken for a group synckafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)同步组的平均时间
sync-time-maxThe max time taken for a group synckafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)同步组的最大时间
sync-rateThe number of group syncs per secondkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)每秒同步组的次数
sync-totalThe total number of group syncskafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)同步组的总次数
rebalance-latency-avgThe average time taken for a group rebalancekafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)组重新平衡的平均时间
rebalance-latency-maxThe max time taken for a group rebalancekafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)组重新平衡的最大时间
rebalance-latency-totalThe total time taken for group rebalances so farkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)到目前为止组重新平衡的总时间
rebalance-totalThe total number of group rebalances participatedkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)参与的组重新平衡总次数
rebalance-rate-per-hourThe number of group rebalance participated per hourkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)每小时参与的组重新平衡次数
failed-rebalance-totalThe total number of failed group rebalanceskafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)失败的组重新平衡总次数
failed-rebalance-rate-per-hourThe number of failed group rebalance events per hourkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)每小时失败的组重新平衡事件次数
last-rebalance-seconds-agoThe number of seconds since the last rebalance eventkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)自上次重新平衡事件以来的秒数
last-heartbeat-seconds-agoThe number of seconds since the last controller heartbeatkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)自上次控制器心跳以来的秒数
partitions-revoked-latency-avgThe average time taken by the on-partitions-revoked rebalance listener callbackkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)分区撤销重新平衡监听器回调的平均时间
partitions-revoked-latency-maxThe max time taken by the on-partitions-revoked rebalance listener callbackkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)分区撤销重新平衡监听器回调的最大时间
partitions-assigned-latency-avgThe average time taken by the on-partitions-assigned rebalance listener callbackkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)分配分区重新平衡监听器回调的平均时间
partitions-assigned-latency-maxThe max time taken by the on-partitions-assigned rebalance listener callbackkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)分配分区重新平衡监听器回调的最大时间
partitions-lost-latency-avgThe average time taken by the on-partitions-lost rebalance listener callbackkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)丢失分区重新平衡监听器回调的平均时间
partitions-lost-latency-maxThe max time taken by the on-partitions-lost rebalance listener callbackkafka.consumer:type=consumer-coordinator-metrics,client-id=([-.\w]+)丢失分区重新平衡监听器回调的最大时间

Consumer Fetch Metrics

METRIC NAMEDESCRIPTIONMBEAN NAME中文描述
bytes-consumed-rateThe average number of bytes consumed per secondkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”每秒消费的字节平均数量
bytes-consumed-totalThe total number of bytes consumedkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”消费的字节总数
fetch-latency-avgThe average time taken for a fetch requestkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”提取请求的平均时间
fetch-latency-maxThe max time taken for any fetch requestkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”提取请求的最大时间
fetch-rateThe number of fetch requests per secondkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”每秒提取请求的次数
fetch-size-avgThe average number of bytes fetched per requestkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”每次请求提取的字节平均数量
fetch-size-maxThe maximum number of bytes fetched per requestkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”每次请求提取的字节最大数量
fetch-throttle-time-avgThe average throttle time in mskafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”平均限速时间(毫秒)
fetch-throttle-time-maxThe maximum throttle time in mskafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”最大限速时间(毫秒)
fetch-totalThe total number of fetch requestskafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”提取请求的总次数
records-consumed-rateThe average number of records consumed per secondkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”每秒消费的记录平均数量
records-consumed-totalThe total number of records consumedkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”消费的记录总数
records-lag-maxThe maximum lag in terms of number of records for any partition in this windowkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”任一分区的记录最大滞后数量
records-lead-minThe minimum lead in terms of number of records for any partition in this windowkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”任一分区的记录最小领先数量
records-per-request-avgThe average number of records in each requestkafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”每次请求的记录平均数量
bytes-consumed-rateThe average number of bytes consumed per second for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”每秒消费的字节平均数量(按主题)
bytes-consumed-totalThe total number of bytes consumed for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”消费的字节总数(按主题)
fetch-size-avgThe average number of bytes fetched per request for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”每次请求提取的字节平均数量(按主题)
fetch-size-maxThe maximum number of bytes fetched per request for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”每次请求提取的字节最大数量(按主题)
records-consumed-rateThe average number of records consumed per second for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”每秒消费的记录平均数量(按主题)
records-consumed-totalThe total number of records consumed for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”消费的记录总数(按主题)
records-per-request-avgThe average number of records in each request for a topickafka.consumer:type=consumer-fetch-manager-metrics,client-id=“{client-id}”,topic=“{topic}”每次请求的记录平均数量(按主题)
preferred-read-replicaThe current read replica for the partition, or -1 if reading from leaderkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”当前分区的读取副本,若从领导者读取则为-1
records-lagThe latest lag of the partitionkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”分区的最新滞后数量
records-lag-avgThe average lag of the partitionkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”分区的平均滞后数量
records-lag-maxThe max lag of the partitionkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”分区的最大滞后数量
records-leadThe latest lead of the partitionkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”分区的最新领先数量
records-lead-avgThe average lead of the partitionkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”分区的平均领先数量
records-lead-minThe min lead of the partitionkafka.consumer:type=consumer-fetch-manager-metrics,partition=“{partition}”,topic=“{topic}”,client-id=“{client-id}”分区的最小领先数量

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com