showuon commented on code in PR #21576:
URL: https://github.com/apache/kafka/pull/21576#discussion_r2929820123
##########
connect/mirror/src/test/java/org/apache/kafka/connect/mirror/integration/MirrorConnectorsIntegrationBaseTest.java:
##########
@@ -1001,6 +1007,76 @@ public void testReplicateFromLatest() throws Exception {
});
}
+ @Test
+ public void testConnectorMetrics() throws InterruptedException,
ExecutionException {
Review Comment:
I would expect we have a test for both enabled case `legacy,new`. Maybe add
in the integration test?
##########
connect/mirror/src/test/java/org/apache/kafka/connect/mirror/integration/MirrorConnectorsIntegrationBaseTest.java:
##########
@@ -1001,6 +1007,76 @@ public void testReplicateFromLatest() throws Exception {
});
}
+ @Test
+ public void testConnectorMetrics() throws InterruptedException,
ExecutionException {
+ // one way replication from primary to backup
+ mm2Props.put(BACKUP_CLUSTER_ALIAS + "->" + PRIMARY_CLUSTER_ALIAS +
".enabled", "false");
+ mm2Props.put(PRIMARY_CLUSTER_ALIAS + "->" + BACKUP_CLUSTER_ALIAS + "."
+ MirrorConnectorConfig.METRIC_NAMES_FORMAT, METRIC_NAMES_NEW);
+ mm2Config = new MirrorMakerConfig(mm2Props);
+
+ String topic = "test-topic-metrics";
+ primary.kafka().createTopic(topic);
+ try (KafkaProducer<byte[], byte[]> producer =
primary.kafka().createProducer(Map.of())) {
+ for (int i = 0; i < NUM_RECORDS_PRODUCED; i++) {
+ producer.send(new ProducerRecord<>(topic, ("value" +
i).getBytes())).get();
+ }
+ }
+
+ waitUntilMirrorMakerIsRunning(backup,
+ List.of(MirrorSourceConnector.class,
MirrorCheckpointConnector.class),
+ mm2Config,
+ PRIMARY_CLUSTER_ALIAS,
+ BACKUP_CLUSTER_ALIAS);
+
+ try (KafkaConsumer<byte[], byte[]> consumer =
primary.kafka().createConsumer(
+ Map.of("group.id", "consumer-group-metrics",
+ ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest",
+ ConsumerConfig.FETCH_MAX_BYTES_CONFIG, "1"))) {
+ int consumed = 0;
+ consumer.subscribe(List.of(topic));
+ while (consumed < NUM_RECORDS_PRODUCED) {
+ ConsumerRecords<byte[], byte[]> records =
consumer.poll(Duration.ofMillis(100));
+ consumer.commitSync();
+ consumed += records.count();
+ }
+ }
+
+ Set<String> expectedSourceTags = Set.of("connector", "task", "source",
"target", "topic", "partition");
+ Set<String> expectedCheckpointTags = Set.of("connector", "task",
"source", "target", "group", "topic", "partition");
+ // We have 4 topics totalling 13 partitions
+ // primary.test-topic - 10 partitions
+ // primary.test-topic-no-checkpoints - 1 partition
+ // primary.test-topic-metrics - 1 partition
+ // primary.heartbeats - 1 partition
+ // There are 12 metrics per partition, 12 x 13 = 156
+ int expectedSourceCount = 156;
+ // We have 1 consumer group (consumer-group-metrics)
+ // There are 4 metrics per group
+ int expectedCheckpointCount = 4;
+ waitForCondition(() -> {
+ Set<ConnectMetrics> allConnectMetrics = backup.allConnectMetrics();
+ int sourceMetrics = 0;
+ int checkpointMetrics = 0;
+ for (ConnectMetrics connectMetrics : allConnectMetrics) {
+ Set<MetricName> metrics =
connectMetrics.metrics().metrics().keySet();
+ for (MetricName metricName : metrics) {
+ if (metricName.group().equals("plugins")) {
+ Map<String, String> tags = metricName.tags();
+ if
(MirrorSourceConnector.class.getSimpleName().equals(tags.get("connector"))) {
+ assertEquals(expectedSourceTags, tags.keySet());
+ sourceMetrics++;
+ }
+ if
(MirrorCheckpointConnector.class.getSimpleName().equals(tags.get("connector")))
{
+ assertEquals(expectedCheckpointTags,
tags.keySet());
+ checkpointMetrics++;
+ }
+ }
+ }
+ }
+ return sourceMetrics == expectedSourceCount && checkpointMetrics
== expectedCheckpointCount;
+ }, 10_000L, "Unable to find the MirrorMaker metrics");
Review Comment:
nit: I think increasing to 30 seconds is better to reduce flaky tests.
##########
connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorMetricsTest.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.kafka.connect.mirror;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.metrics.PluginMetrics;
+import org.apache.kafka.common.metrics.internals.PluginMetricsImpl;
+import org.apache.kafka.connect.runtime.ConnectorConfig;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class MirrorMetricsTest {
+
+ private static final String SOURCE = "source";
+ private static final String TARGET = "target";
+ private static final TopicPartition TP = new TopicPartition("topic", 0);
+ private static final TopicPartition SOURCE_TP = new TopicPartition(SOURCE
+ "." + TP.topic(), TP.partition());
+ private static final String GROUP = "my-group";
+
+ private final Map<String, String> configs = new HashMap<>();
+ private final Metrics metrics = new Metrics();
+ private final PluginMetrics pluginMetrics = new PluginMetricsImpl(metrics,
Map.of());
+
+ @BeforeEach
+ public void setUp() {
+ configs.put(ConnectorConfig.NAME_CONFIG, "name");
+ configs.put(MirrorConnectorConfig.SOURCE_CLUSTER_ALIAS, SOURCE);
+ configs.put(MirrorConnectorConfig.TARGET_CLUSTER_ALIAS, TARGET);
+ configs.put(MirrorConnectorConfig.TASK_INDEX, "0");
+ }
+
+ @Test
+ public void testSourceTags() {
+ configs.put(ConnectorConfig.CONNECTOR_CLASS_CONFIG,
MirrorSourceConnector.class.getName());
+ configs.put(MirrorSourceTaskConfig.TASK_TOPIC_PARTITIONS,
TP.toString());
+ MirrorSourceTaskConfig taskConfig = new
MirrorSourceTaskConfig(configs);
+ MirrorSourceMetrics sourceMetrics = new
MirrorSourceMetrics(pluginMetrics, taskConfig);
+
+ sourceMetrics.countRecord(SOURCE_TP);
+ // 12 MirrorSourceConnector metrics + the metrics count metric
+ assertEquals(13, metrics.metrics().size());
+ Map<String, String> tags = metrics.metrics().keySet().stream()
+ .filter(m -> m.group().equals("plugins"))
+ .findFirst()
+ .get().tags();
+ assertEquals(SOURCE, tags.get("source"));
Review Comment:
Should we also verify the `connector` tag? Same for `testCheckpointTags`.
--
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]