22
33import com .xiaojukeji .kafka .manager .common .constant .Constant ;
44import com .xiaojukeji .kafka .manager .common .entity .ConsumerMetrics ;
5- import com .xiaojukeji .kafka .manager .common .entity .zookeeper .PartitionState ;
65import com .xiaojukeji .kafka .manager .common .entity .dto .consumer .ConsumerDTO ;
76import com .xiaojukeji .kafka .manager .common .entity .po .ClusterDO ;
87import com .xiaojukeji .kafka .manager .service .cache .ClusterMetadataManager ;
98import com .xiaojukeji .kafka .manager .service .cache .KafkaMetricsCache ;
109import com .xiaojukeji .kafka .manager .service .service .ConsumerService ;
10+ import org .apache .kafka .common .TopicPartition ;
1111import org .slf4j .Logger ;
1212import org .slf4j .LoggerFactory ;
1313
@@ -34,8 +34,8 @@ public void collect() {
3434 if (clusterDO == null ) {
3535 return ;
3636 }
37- Map <String , List < PartitionState >> topicNamePartitionStateListMap = new HashMap <>();
38- List <ConsumerDTO > consumerDTOList = consumerService .getMonitoredConsumerList (clusterDO , topicNamePartitionStateListMap );
37+ Map <TopicPartition , Long > allPartitionOffsetMap = new HashMap <>();
38+ List <ConsumerDTO > consumerDTOList = consumerService .getMonitoredConsumerList (clusterDO , allPartitionOffsetMap );
3939
4040 List <ConsumerMetrics > consumerMetricsList = convert2ConsumerMetrics (consumerDTOList );
4141 KafkaMetricsCache .putConsumerMetricsToCache (clusterId , consumerMetricsList );
@@ -47,23 +47,27 @@ public void collect() {
4747 private List <ConsumerMetrics > convert2ConsumerMetrics (List <ConsumerDTO > consumerDTOList ) {
4848 List <ConsumerMetrics > consumerMetricsList = new ArrayList <>();
4949 for (ConsumerDTO consumerDTO : consumerDTOList ) {
50- Map <String , List <PartitionState >> topicNamePartitionStateListMap = consumerDTO .getTopicPartitionMap ();
51- for (Map .Entry <String , List <PartitionState >> entry : topicNamePartitionStateListMap .entrySet ()){
52- String topicName = entry .getKey ();
53- List <PartitionState > partitionStateList = entry .getValue ();
54- ConsumerMetrics consumerMetrics = new ConsumerMetrics ();
55- consumerMetrics .setClusterId (clusterId );
56- consumerMetrics .setConsumerGroup (consumerDTO .getConsumerGroup ());
57- consumerMetrics .setLocation (consumerDTO .getLocation ());
58- consumerMetrics .setTopicName (topicName );
59- long sumLag = 0 ;
60- for (PartitionState partitionState : partitionStateList ) {
61- Map .Entry <Long , Long > offsetEntry = new AbstractMap .SimpleEntry <>(partitionState .getOffset (), partitionState .getConsumeOffset ());
62- sumLag += (offsetEntry .getKey () - offsetEntry .getValue () > 0 ? offsetEntry .getKey () - offsetEntry .getValue (): 0 );
50+ if (consumerDTO .getPartitionOffsetMap () == null || consumerDTO .getConsumerOffsetMap () == null ) {
51+ continue ;
52+ }
53+
54+ ConsumerMetrics consumerMetrics = new ConsumerMetrics ();
55+ consumerMetrics .setClusterId (consumerDTO .getClusterId ());
56+ consumerMetrics .setConsumerGroup (consumerDTO .getConsumerGroup ());
57+ consumerMetrics .setLocation (consumerDTO .getLocation ());
58+ consumerMetrics .setTopicName (consumerDTO .getTopicName ());
59+
60+ long sumLag = 0 ;
61+ for (Map .Entry <Integer , Long > entry : consumerDTO .getPartitionOffsetMap ().entrySet ()){
62+ Long partitionOffset = entry .getValue ();
63+ Long consumerOffset = consumerDTO .getConsumerOffsetMap ().get (entry .getKey ());
64+ if (partitionOffset == null || consumerOffset == null ) {
65+ continue ;
6366 }
64- consumerMetrics .setSumLag (sumLag );
65- consumerMetricsList .add (consumerMetrics );
67+ sumLag += Math .max (partitionOffset - consumerOffset , 0 );
6668 }
69+ consumerMetrics .setSumLag (sumLag );
70+ consumerMetricsList .add (consumerMetrics );
6771 }
6872 return consumerMetricsList ;
6973 }
0 commit comments