flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [flink] PatrickRen commented on a change in pull request #16115: [FLINK-22766][connector/kafka] Report offsets and Kafka consumer metrics in Flink metric group
Date Wed, 09 Jun 2021 07:37:32 GMT

PatrickRen commented on a change in pull request #16115:
URL: https://github.com/apache/flink/pull/16115#discussion_r648045623



##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/source/metrics/KafkaSourceReaderMetricsListener.java
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.metrics.CharacterFilter;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormat;
+import org.apache.flink.runtime.metrics.util.TestingMetricRegistry;
+
+import org.apache.kafka.common.TopicPartition;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/** A utilization class for mocking {@link OperatorMetricGroup} and collecting reported metrics.
*/
+public class KafkaSourceReaderMetricsListener {
+    public static final String OPERATOR_NAME = "operator_metric_mocker";
+    public static final char DELIMITER = '.';
+
+    Map<String, Metric> kafkaSourceReaderMetrics = new HashMap<>();
+    Map<String, Metric> kafkaConsumerMetrics = new HashMap<>();
+
+    private final OperatorMetricGroup metricGroup;
+
+    public KafkaSourceReaderMetricsListener() {
+        TestingMetricRegistry registry =
+                TestingMetricRegistry.builder()
+                        .setDelimiter(DELIMITER)
+                        .setRegisterConsumer(
+                                (metric, metricName, group) -> {
+                                    String parentGroupScope =
+                                            group.getLogicalScope(CharacterFilter.NO_OP_FILTER);
+
+                                    // We only care about Kafka source reader metrics
+                                    if (isKafkaSourceReaderMetric(parentGroupScope)) {
+
+                                        // KafkaConsumer metric
+                                        if (isKafkaConsumerMetric(parentGroupScope)) {
+                                            kafkaConsumerMetrics.put(metricName, metric);
+                                            return;
+                                        }
+
+                                        final String topic =
+                                                group.getAllVariables()
+                                                        .get(
+                                                                ScopeFormat.asVariable(
+                                                                        KafkaSourceReaderMetrics
+                                                                                .TOPIC_GROUP));
+                                        final String partition =
+                                                group.getAllVariables()
+                                                        .get(
+                                                                ScopeFormat.asVariable(
+                                                                        KafkaSourceReaderMetrics
+                                                                                .PARTITION_GROUP));
+
+                                        if (topic == null && partition == null) {
+                                            // General metric
+                                            kafkaSourceReaderMetrics.put(metricName, metric);
+                                        } else {
+                                            // topic partition specific metric
+                                            kafkaSourceReaderMetrics.put(
+                                                    String.join(
+                                                            String.valueOf(DELIMITER),
+                                                            topic,
+                                                            partition,
+                                                            metricName),
+                                                    metric);
+                                        }
+                                    }
+                                })

Review comment:
       Thanks for your review Myoungdo! Fixed in 4c65357.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message