KAFKA-10770: Remove duplicate defination of Metrics#getTags (#9659)

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
ArunParthiban-ST 2020-11-30 23:10:22 -06:00 committed by GitHub
parent 90bc7e9777
commit cc1aa3b83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 42 deletions

View File

@ -18,6 +18,7 @@ package org.apache.kafka.common.metrics;
import org.apache.kafka.common.MetricName; import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.MetricNameTemplate; import org.apache.kafka.common.MetricNameTemplate;
import org.apache.kafka.common.metrics.internals.MetricsUtils;
import org.apache.kafka.common.utils.KafkaThread; import org.apache.kafka.common.utils.KafkaThread;
import org.apache.kafka.common.utils.Time; import org.apache.kafka.common.utils.Time;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -227,7 +228,7 @@ public class Metrics implements Closeable {
* @param keyValue additional key/value attributes of the metric (must come in pairs) * @param keyValue additional key/value attributes of the metric (must come in pairs)
*/ */
public MetricName metricName(String name, String group, String description, String... keyValue) { public MetricName metricName(String name, String group, String description, String... keyValue) {
return metricName(name, group, description, getTags(keyValue)); return metricName(name, group, description, MetricsUtils.getTags(keyValue));
} }
/** /**
@ -242,16 +243,6 @@ public class Metrics implements Closeable {
return metricName(name, group, "", tags); return metricName(name, group, "", tags);
} }
private static Map<String, String> getTags(String... keyValue) {
if ((keyValue.length % 2) != 0)
throw new IllegalArgumentException("keyValue needs to be specified in pairs");
Map<String, String> tags = new LinkedHashMap<>();
for (int i = 0; i < keyValue.length; i += 2)
tags.put(keyValue[i], keyValue[i + 1]);
return tags;
}
/** /**
* Use the specified domain and metric name templates to generate an HTML table documenting the metrics. A separate table section * Use the specified domain and metric name templates to generate an HTML table documenting the metrics. A separate table section
* will be generated for each of the MBeans and the associated attributes. The MBean names are lexicographically sorted to * will be generated for each of the MBeans and the associated attributes. The MBean names are lexicographically sorted to
@ -633,7 +624,7 @@ public class Metrics implements Closeable {
} }
public MetricName metricInstance(MetricNameTemplate template, String... keyValue) { public MetricName metricInstance(MetricNameTemplate template, String... keyValue) {
return metricInstance(template, getTags(keyValue)); return metricInstance(template, MetricsUtils.getTags(keyValue));
} }
public MetricName metricInstance(MetricNameTemplate template, Map<String, String> tags) { public MetricName metricInstance(MetricNameTemplate template, Map<String, String> tags) {

View File

@ -16,6 +16,10 @@
*/ */
package org.apache.kafka.common.metrics.internals; package org.apache.kafka.common.metrics.internals;
import org.apache.kafka.common.metrics.Metrics;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class MetricsUtils { public class MetricsUtils {
@ -43,4 +47,20 @@ public class MetricsUtils {
throw new IllegalStateException("Unknown unit: " + unit); throw new IllegalStateException("Unknown unit: " + unit);
} }
} }
/**
* Create a set of tags using the supplied key and value pairs. The order of the tags will be kept.
*
* @param keyValue the key and value pairs for the tags; must be an even number
* @return the map of tags that can be supplied to the {@link Metrics} methods; never null
*/
public static Map<String, String> getTags(String... keyValue) {
if ((keyValue.length % 2) != 0)
throw new IllegalArgumentException("keyValue needs to be specified in pairs");
Map<String, String> tags = new LinkedHashMap<>(keyValue.length / 2);
for (int i = 0; i < keyValue.length; i += 2)
tags.put(keyValue[i], keyValue[i + 1]);
return tags;
}
} }

View File

@ -0,0 +1,40 @@
/*
* 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.common.metrics.internals;
import org.junit.Test;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
public class MetricsUtilsTest {
@Test
public void testCreatingTags() {
Map<String, String> tags = MetricsUtils.getTags("k1", "v1", "k2", "v2");
assertEquals("v1", tags.get("k1"));
assertEquals("v2", tags.get("k2"));
assertEquals(2, tags.size());
}
@Test
public void testCreatingTagsWithOddNumberOfTags() {
assertThrows(IllegalArgumentException.class, () -> MetricsUtils.getTags("k1", "v1", "k2", "v2", "extra"));
}
}

View File

@ -27,6 +27,7 @@ import org.apache.kafka.common.metrics.Metrics;
import org.apache.kafka.common.metrics.MetricsContext; import org.apache.kafka.common.metrics.MetricsContext;
import org.apache.kafka.common.metrics.MetricsReporter; import org.apache.kafka.common.metrics.MetricsReporter;
import org.apache.kafka.common.metrics.Sensor; import org.apache.kafka.common.metrics.Sensor;
import org.apache.kafka.common.metrics.internals.MetricsUtils;
import org.apache.kafka.common.utils.AppInfoParser; import org.apache.kafka.common.utils.AppInfoParser;
import org.apache.kafka.common.utils.Time; import org.apache.kafka.common.utils.Time;
import org.apache.kafka.connect.runtime.distributed.DistributedConfig; import org.apache.kafka.connect.runtime.distributed.DistributedConfig;
@ -148,7 +149,7 @@ public class ConnectMetrics {
} }
protected MetricGroupId groupId(String groupName, String... tagKeyValues) { protected MetricGroupId groupId(String groupName, String... tagKeyValues) {
Map<String, String> tags = tags(tagKeyValues); Map<String, String> tags = MetricsUtils.getTags(tagKeyValues);
return new MetricGroupId(groupName, tags); return new MetricGroupId(groupName, tags);
} }
@ -433,22 +434,6 @@ public class ConnectMetrics {
T metricValue(long now); T metricValue(long now);
} }
/**
* Create a set of tags using the supplied key and value pairs. The order of the tags will be kept.
*
* @param keyValue the key and value pairs for the tags; must be an even number
* @return the map of tags that can be supplied to the {@link Metrics} methods; never null
*/
static Map<String, String> tags(String... keyValue) {
if ((keyValue.length % 2) != 0)
throw new IllegalArgumentException("keyValue needs to be specified in pairs");
Map<String, String> tags = new LinkedHashMap<>();
for (int i = 0; i < keyValue.length; i += 2) {
tags.put(keyValue[i], keyValue[i + 1]);
}
return tags;
}
/** /**
* Utility to generate the documentation for the Connect metrics. * Utility to generate the documentation for the Connect metrics.
* *

View File

@ -67,19 +67,6 @@ public class ConnectMetricsTest {
assertNotNull(metrics.metrics()); assertNotNull(metrics.metrics());
} }
@Test
public void testCreatingTags() {
Map<String, String> tags = ConnectMetrics.tags("k1", "v1", "k2", "v2");
assertEquals("v1", tags.get("k1"));
assertEquals("v2", tags.get("k2"));
assertEquals(2, tags.size());
}
@Test(expected = IllegalArgumentException.class)
public void testCreatingTagsWithOddNumberOfTags() {
ConnectMetrics.tags("k1", "v1", "k2", "v2", "extra");
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGettingGroupWithOddNumberOfTags() { public void testGettingGroupWithOddNumberOfTags() {
metrics.group("name", "k1", "v1", "k2", "v2", "extra"); metrics.group("name", "k1", "v1", "k2", "v2", "extra");