forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-14740: Add source tag to MirrorSourceMetrics - KIP-911 (apache#…
…13420) New add.source.alias.to.metrics setting to add the source cluster alias to the MirrorSourceConnector metrics Reviewers: Chris Egerton <[email protected]>
- Loading branch information
Showing
3 changed files
with
193 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorSourceMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* 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.KafkaMetric; | ||
import org.apache.kafka.common.metrics.MetricsReporter; | ||
import org.apache.kafka.connect.runtime.ConnectorConfig; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
public class MirrorSourceMetricsTest { | ||
|
||
private final static String SOURCE = "source"; | ||
private final static String TARGET = "target"; | ||
private final static TopicPartition TP = new TopicPartition("topic", 0); | ||
private final static TopicPartition SOURCE_TP = new TopicPartition(SOURCE + "." + TP.topic(), TP.partition()); | ||
|
||
private final Map<String, String> configs = new HashMap<>(); | ||
private TestReporter reporter; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
configs.put(ConnectorConfig.NAME_CONFIG, "name"); | ||
configs.put(ConnectorConfig.CONNECTOR_CLASS_CONFIG, MirrorSourceConnector.class.getName()); | ||
configs.put(MirrorConnectorConfig.SOURCE_CLUSTER_ALIAS, SOURCE); | ||
configs.put(MirrorConnectorConfig.TARGET_CLUSTER_ALIAS, TARGET); | ||
configs.put(MirrorSourceTaskConfig.TASK_TOPIC_PARTITIONS, TP.toString()); | ||
reporter = new TestReporter(); | ||
} | ||
|
||
@Test | ||
public void testTags() { | ||
MirrorSourceTaskConfig taskConfig = new MirrorSourceTaskConfig(configs); | ||
MirrorSourceMetrics metrics = new MirrorSourceMetrics(taskConfig); | ||
metrics.addReporter(reporter); | ||
|
||
metrics.countRecord(SOURCE_TP); | ||
assertEquals(13, reporter.metrics.size()); | ||
Map<String, String> tags = reporter.metrics.get(0).metricName().tags(); | ||
assertEquals(TARGET, tags.get("target")); | ||
assertEquals(SOURCE_TP.topic(), tags.get("topic")); | ||
assertEquals(String.valueOf(SOURCE_TP.partition()), tags.get("partition")); | ||
assertNull(tags.get("source")); | ||
} | ||
|
||
@Test | ||
public void testTagsWithSourceAlias() { | ||
configs.put(MirrorSourceConfig.ADD_SOURCE_ALIAS_TO_METRICS, "true"); | ||
MirrorSourceTaskConfig taskConfig = new MirrorSourceTaskConfig(configs); | ||
MirrorSourceMetrics metrics = new MirrorSourceMetrics(taskConfig); | ||
metrics.addReporter(reporter); | ||
|
||
metrics.countRecord(SOURCE_TP); | ||
assertEquals(13, reporter.metrics.size()); | ||
Map<String, String> tags = reporter.metrics.get(0).metricName().tags(); | ||
assertEquals(SOURCE, tags.get("source")); | ||
assertEquals(TARGET, tags.get("target")); | ||
assertEquals(SOURCE_TP.topic(), tags.get("topic")); | ||
assertEquals(String.valueOf(SOURCE_TP.partition()), tags.get("partition")); | ||
} | ||
|
||
static class TestReporter implements MetricsReporter { | ||
|
||
List<KafkaMetric> metrics = new ArrayList<>(); | ||
|
||
@Override | ||
public void init(List<KafkaMetric> metrics) { | ||
for (KafkaMetric metric : metrics) { | ||
metricChange(metric); | ||
} | ||
} | ||
|
||
@Override | ||
public void metricChange(KafkaMetric metric) { | ||
metrics.add(metric); | ||
} | ||
|
||
@Override | ||
public void metricRemoval(KafkaMetric metric) {} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) {} | ||
} | ||
} |