becketqin commented on a change in pull request #16115: URL: https://github.com/apache/flink/pull/16115#discussion_r659446873
########## File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/metrics/KafkaSourceReaderMetrics.java ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.kafka.source.metrics; + +import org.apache.flink.connector.kafka.source.reader.KafkaSourceReader; +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.MetricGroup; + +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.Metric; +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.TopicPartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * A collection class for handling metrics in {@link KafkaSourceReader}. + * + * <p>Hierarchy + */ +public class KafkaSourceReaderMetrics { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaSourceReaderMetrics.class); + + // Constants + public static final String KAFKA_SOURCE_READER_METRIC_GROUP = "KafkaSourceReader"; + public static final String TOPIC_GROUP = "topic"; + public static final String PARTITION_GROUP = "partition"; + public static final String CURRENT_OFFSET_METRIC_GAUGE = "currentOffset"; + public static final String COMMITTED_OFFSET_METRIC_GAUGE = "committedOffset"; + public static final String COMMITS_SUCCEEDED_METRIC_COUNTER = "commitsSucceeded"; + public static final String COMMITS_FAILED_METRIC_COUNTER = "commitsFailed"; + public static final String KAFKA_CONSUMER_METRIC_GROUP = "KafkaConsumer"; + + public static final long INITIAL_OFFSET = 0; + + // Metric group for registering Kafka related metrics + private final MetricGroup kafkaSourceReaderMetricGroup; + + // Successful / Failed commits counters + private final Counter commitsSucceeded; + private final Counter commitsFailed; + + // Map for tracking current consuming / committing offsets + private final Map<TopicPartition, Offset> offsets = new HashMap<>(); + + public KafkaSourceReaderMetrics(MetricGroup parentMetricGroup) { + this.kafkaSourceReaderMetricGroup = + parentMetricGroup.addGroup(KAFKA_SOURCE_READER_METRIC_GROUP); + this.commitsSucceeded = + this.kafkaSourceReaderMetricGroup.counter(COMMITS_SUCCEEDED_METRIC_COUNTER); + this.commitsFailed = + this.kafkaSourceReaderMetricGroup.counter(COMMITS_FAILED_METRIC_COUNTER); + } + + /** + * Register metrics of KafkaConsumer in Kafka metric group. + * + * @param kafkaConsumer Kafka consumer used by partition split reader. + */ + @SuppressWarnings("Convert2MethodRef") + public void registerKafkaConsumerMetrics(KafkaConsumer<?, ?> kafkaConsumer) { + final Map<MetricName, ? extends Metric> kafkaConsumerMetrics = kafkaConsumer.metrics(); + if (kafkaConsumerMetrics == null) { + LOG.warn("Consumer implementation does not support metrics"); + return; + } + + final MetricGroup kafkaConsumerMetricGroup = + kafkaSourceReaderMetricGroup.addGroup(KAFKA_CONSUMER_METRIC_GROUP); + + kafkaConsumerMetrics.forEach( + (name, metric) -> + kafkaConsumerMetricGroup.gauge(name.name(), () -> metric.metricValue())); + } + + /** + * Register metric groups for the given {@link TopicPartition}. + * + * @param tp Registering topic partition + */ + public void registerTopicPartition(TopicPartition tp) { + offsets.put(tp, new Offset(INITIAL_OFFSET, INITIAL_OFFSET)); + registerOffsetMetricsForTopicPartition(tp); + } + + /** + * Update current consuming offset of the given {@link TopicPartition}. + * + * @param tp Updating topic partition + * @param offset Current consuming offset + */ + public void consuming(TopicPartition tp, long offset) { + checkTopicPartitionTracked(tp); + offsets.get(tp).currentOffset = offset; + } + + /** + * Update current committing offset of the given {@link TopicPartition}. + * + * @param tp Updating topic partition + * @param offset Committing offset + */ + public void committing(TopicPartition tp, long offset) { Review comment: Ditto above ########## File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/metrics/KafkaSourceReaderMetrics.java ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.kafka.source.metrics; + +import org.apache.flink.connector.kafka.source.reader.KafkaSourceReader; +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.MetricGroup; + +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.Metric; +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.TopicPartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * A collection class for handling metrics in {@link KafkaSourceReader}. + * + * <p>Hierarchy + */ +public class KafkaSourceReaderMetrics { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaSourceReaderMetrics.class); + + // Constants + public static final String KAFKA_SOURCE_READER_METRIC_GROUP = "KafkaSourceReader"; + public static final String TOPIC_GROUP = "topic"; + public static final String PARTITION_GROUP = "partition"; + public static final String CURRENT_OFFSET_METRIC_GAUGE = "currentOffset"; + public static final String COMMITTED_OFFSET_METRIC_GAUGE = "committedOffset"; + public static final String COMMITS_SUCCEEDED_METRIC_COUNTER = "commitsSucceeded"; + public static final String COMMITS_FAILED_METRIC_COUNTER = "commitsFailed"; + public static final String KAFKA_CONSUMER_METRIC_GROUP = "KafkaConsumer"; + + public static final long INITIAL_OFFSET = 0; Review comment: Should this be set to -1 instead of 0 to avoid confusion? ########## File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/metrics/KafkaSourceReaderMetrics.java ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.kafka.source.metrics; + +import org.apache.flink.connector.kafka.source.reader.KafkaSourceReader; +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.MetricGroup; + +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.Metric; +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.TopicPartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * A collection class for handling metrics in {@link KafkaSourceReader}. + * + * <p>Hierarchy + */ +public class KafkaSourceReaderMetrics { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaSourceReaderMetrics.class); + + // Constants + public static final String KAFKA_SOURCE_READER_METRIC_GROUP = "KafkaSourceReader"; + public static final String TOPIC_GROUP = "topic"; + public static final String PARTITION_GROUP = "partition"; + public static final String CURRENT_OFFSET_METRIC_GAUGE = "currentOffset"; + public static final String COMMITTED_OFFSET_METRIC_GAUGE = "committedOffset"; + public static final String COMMITS_SUCCEEDED_METRIC_COUNTER = "commitsSucceeded"; + public static final String COMMITS_FAILED_METRIC_COUNTER = "commitsFailed"; + public static final String KAFKA_CONSUMER_METRIC_GROUP = "KafkaConsumer"; + + public static final long INITIAL_OFFSET = 0; + + // Metric group for registering Kafka related metrics + private final MetricGroup kafkaSourceReaderMetricGroup; + + // Successful / Failed commits counters + private final Counter commitsSucceeded; + private final Counter commitsFailed; + + // Map for tracking current consuming / committing offsets + private final Map<TopicPartition, Offset> offsets = new HashMap<>(); + + public KafkaSourceReaderMetrics(MetricGroup parentMetricGroup) { + this.kafkaSourceReaderMetricGroup = + parentMetricGroup.addGroup(KAFKA_SOURCE_READER_METRIC_GROUP); + this.commitsSucceeded = + this.kafkaSourceReaderMetricGroup.counter(COMMITS_SUCCEEDED_METRIC_COUNTER); + this.commitsFailed = + this.kafkaSourceReaderMetricGroup.counter(COMMITS_FAILED_METRIC_COUNTER); + } + + /** + * Register metrics of KafkaConsumer in Kafka metric group. + * + * @param kafkaConsumer Kafka consumer used by partition split reader. + */ + @SuppressWarnings("Convert2MethodRef") + public void registerKafkaConsumerMetrics(KafkaConsumer<?, ?> kafkaConsumer) { + final Map<MetricName, ? extends Metric> kafkaConsumerMetrics = kafkaConsumer.metrics(); + if (kafkaConsumerMetrics == null) { + LOG.warn("Consumer implementation does not support metrics"); + return; + } + + final MetricGroup kafkaConsumerMetricGroup = + kafkaSourceReaderMetricGroup.addGroup(KAFKA_CONSUMER_METRIC_GROUP); + + kafkaConsumerMetrics.forEach( + (name, metric) -> + kafkaConsumerMetricGroup.gauge(name.name(), () -> metric.metricValue())); + } + + /** + * Register metric groups for the given {@link TopicPartition}. + * + * @param tp Registering topic partition + */ + public void registerTopicPartition(TopicPartition tp) { + offsets.put(tp, new Offset(INITIAL_OFFSET, INITIAL_OFFSET)); + registerOffsetMetricsForTopicPartition(tp); + } + + /** + * Update current consuming offset of the given {@link TopicPartition}. + * + * @param tp Updating topic partition + * @param offset Current consuming offset + */ + public void consuming(TopicPartition tp, long offset) { Review comment: Can we make the name more explicit, e.g. `recordConsumingOffset()` ########## File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/metrics/KafkaSourceReaderMetrics.java ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.kafka.source.metrics; + +import org.apache.flink.connector.kafka.source.reader.KafkaSourceReader; +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.MetricGroup; + +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.Metric; +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.TopicPartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * A collection class for handling metrics in {@link KafkaSourceReader}. + * + * <p>Hierarchy + */ +public class KafkaSourceReaderMetrics { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaSourceReaderMetrics.class); + + // Constants + public static final String KAFKA_SOURCE_READER_METRIC_GROUP = "KafkaSourceReader"; + public static final String TOPIC_GROUP = "topic"; + public static final String PARTITION_GROUP = "partition"; + public static final String CURRENT_OFFSET_METRIC_GAUGE = "currentOffset"; + public static final String COMMITTED_OFFSET_METRIC_GAUGE = "committedOffset"; + public static final String COMMITS_SUCCEEDED_METRIC_COUNTER = "commitsSucceeded"; + public static final String COMMITS_FAILED_METRIC_COUNTER = "commitsFailed"; + public static final String KAFKA_CONSUMER_METRIC_GROUP = "KafkaConsumer"; + + public static final long INITIAL_OFFSET = 0; + + // Metric group for registering Kafka related metrics + private final MetricGroup kafkaSourceReaderMetricGroup; + + // Successful / Failed commits counters + private final Counter commitsSucceeded; + private final Counter commitsFailed; + + // Map for tracking current consuming / committing offsets + private final Map<TopicPartition, Offset> offsets = new HashMap<>(); + + public KafkaSourceReaderMetrics(MetricGroup parentMetricGroup) { + this.kafkaSourceReaderMetricGroup = + parentMetricGroup.addGroup(KAFKA_SOURCE_READER_METRIC_GROUP); + this.commitsSucceeded = + this.kafkaSourceReaderMetricGroup.counter(COMMITS_SUCCEEDED_METRIC_COUNTER); + this.commitsFailed = + this.kafkaSourceReaderMetricGroup.counter(COMMITS_FAILED_METRIC_COUNTER); + } + + /** + * Register metrics of KafkaConsumer in Kafka metric group. + * + * @param kafkaConsumer Kafka consumer used by partition split reader. + */ + @SuppressWarnings("Convert2MethodRef") + public void registerKafkaConsumerMetrics(KafkaConsumer<?, ?> kafkaConsumer) { + final Map<MetricName, ? extends Metric> kafkaConsumerMetrics = kafkaConsumer.metrics(); + if (kafkaConsumerMetrics == null) { + LOG.warn("Consumer implementation does not support metrics"); + return; + } + + final MetricGroup kafkaConsumerMetricGroup = + kafkaSourceReaderMetricGroup.addGroup(KAFKA_CONSUMER_METRIC_GROUP); + + kafkaConsumerMetrics.forEach( + (name, metric) -> + kafkaConsumerMetricGroup.gauge(name.name(), () -> metric.metricValue())); + } + + /** + * Register metric groups for the given {@link TopicPartition}. + * + * @param tp Registering topic partition + */ + public void registerTopicPartition(TopicPartition tp) { + offsets.put(tp, new Offset(INITIAL_OFFSET, INITIAL_OFFSET)); + registerOffsetMetricsForTopicPartition(tp); + } + + /** + * Update current consuming offset of the given {@link TopicPartition}. + * + * @param tp Updating topic partition + * @param offset Current consuming offset + */ + public void consuming(TopicPartition tp, long offset) { + checkTopicPartitionTracked(tp); + offsets.get(tp).currentOffset = offset; + } + + /** + * Update current committing offset of the given {@link TopicPartition}. + * + * @param tp Updating topic partition + * @param offset Committing offset + */ + public void committing(TopicPartition tp, long offset) { + checkTopicPartitionTracked(tp); + commitsSucceeded.inc(); + offsets.get(tp).committedOffset = offset; + } + + /** Mark a failure commit. */ + public void failedCommitting() { Review comment: Ditto above -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
