You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ij...@apache.org on 2017/09/08 12:30:31 UTC
kafka git commit: KAFKA-5656;
Support bulk attributes request on KafkaMbean where some attributes
do not exist
Repository: kafka
Updated Branches:
refs/heads/trunk ef17d5b89 -> ee4e4c8cb
KAFKA-5656; Support bulk attributes request on KafkaMbean where some attributes do not exist
Author: Erik Kringen <er...@icloud.com>
Reviewers: Manikumar Reddy <ma...@gmail.com>, Ismael Juma <is...@juma.me.uk>
Closes #3582 from ErikKringen/trunk
Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/ee4e4c8c
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/ee4e4c8c
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/ee4e4c8c
Branch: refs/heads/trunk
Commit: ee4e4c8cb35d1403c521518d6c269bbc03700325
Parents: ef17d5b
Author: Erik Kringen <er...@icloud.com>
Authored: Fri Sep 8 13:16:03 2017 +0100
Committer: Ismael Juma <is...@juma.me.uk>
Committed: Fri Sep 8 13:30:23 2017 +0100
----------------------------------------------------------------------
.../kafka/common/metrics/JmxReporter.java | 16 +-
.../kafka/common/metrics/KafkaMbeanTest.java | 165 +++++++++++++++++++
2 files changed, 173 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kafka/blob/ee4e4c8c/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java b/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java
index 67dfaa8..de02a23 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java
@@ -169,7 +169,7 @@ public class JmxReporter implements MetricsReporter {
private final Map<String, KafkaMetric> metrics;
public KafkaMbean(String mbeanName) throws MalformedObjectNameException {
- this.metrics = new HashMap<String, KafkaMetric>();
+ this.metrics = new HashMap<>();
this.objectName = new ObjectName(mbeanName);
}
@@ -191,15 +191,15 @@ public class JmxReporter implements MetricsReporter {
@Override
public AttributeList getAttributes(String[] names) {
- try {
- AttributeList list = new AttributeList();
- for (String name : names)
+ AttributeList list = new AttributeList();
+ for (String name : names) {
+ try {
list.add(new Attribute(name, getAttribute(name)));
- return list;
- } catch (Exception e) {
- log.error("Error getting JMX attribute: ", e);
- return new AttributeList();
+ } catch (Exception e) {
+ log.warn("Error getting JMX attribute '{}'", name, e);
+ }
}
+ return list;
}
public KafkaMetric removeAttribute(String name) {
http://git-wip-us.apache.org/repos/asf/kafka/blob/ee4e4c8c/clients/src/test/java/org/apache/kafka/common/metrics/KafkaMbeanTest.java
----------------------------------------------------------------------
diff --git a/clients/src/test/java/org/apache/kafka/common/metrics/KafkaMbeanTest.java b/clients/src/test/java/org/apache/kafka/common/metrics/KafkaMbeanTest.java
new file mode 100644
index 0000000..963a66a
--- /dev/null
+++ b/clients/src/test/java/org/apache/kafka/common/metrics/KafkaMbeanTest.java
@@ -0,0 +1,165 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.metrics.stats.Count;
+import org.apache.kafka.common.metrics.stats.Sum;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.management.Attribute;
+import javax.management.AttributeList;
+import javax.management.AttributeNotFoundException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.RuntimeMBeanException;
+import java.lang.management.ManagementFactory;
+import java.util.List;
+
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+public class KafkaMbeanTest {
+
+ private final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
+ private Sensor sensor;
+ private MetricName countMetricName;
+ private MetricName sumMetricName;
+ private Metrics metrics;
+
+ @Before
+ public void setup() throws Exception {
+ metrics = new Metrics();
+ metrics.addReporter(new JmxReporter());
+ sensor = metrics.sensor("kafka.requests");
+ countMetricName = metrics.metricName("pack.bean1.count", "grp1");
+ sensor.add(countMetricName, new Count());
+ sumMetricName = metrics.metricName("pack.bean1.sum", "grp1");
+ sensor.add(sumMetricName, new Sum());
+ }
+
+ @After
+ public void tearDown() {
+ metrics.close();
+ }
+
+ @Test
+ public void testGetAttribute() throws Exception {
+ sensor.record(2.5);
+ Object counterAttribute = getAttribute(countMetricName);
+ assertEquals(1.0, counterAttribute);
+ Object sumAttribute = getAttribute(sumMetricName);
+ assertEquals(2.5, sumAttribute);
+ }
+
+ @Test
+ public void testGetAttributeUnknown() throws Exception {
+ sensor.record(2.5);
+ try {
+ getAttribute(sumMetricName, "name");
+ fail("Should have gotten attribute not found");
+ } catch (AttributeNotFoundException e) {
+ // Expected
+ }
+ }
+
+ @Test
+ public void testGetAttributes() throws Exception {
+ sensor.record(3.5);
+ sensor.record(4.0);
+ AttributeList attributeList = getAttributes(countMetricName, countMetricName.name(), sumMetricName.name());
+ List<Attribute> attributes = attributeList.asList();
+ assertEquals(2, attributes.size());
+ for (Attribute attribute : attributes) {
+ if (countMetricName.name().equals(attribute.getName()))
+ assertEquals(2.0, attribute.getValue());
+ else if (sumMetricName.name().equals(attribute.getName()))
+ assertEquals(7.5, attribute.getValue());
+ else
+ fail("Unexpected attribute returned: " + attribute.getName());
+ }
+ }
+
+ @Test
+ public void testGetAttributesWithUnknown() throws Exception {
+ sensor.record(3.5);
+ sensor.record(4.0);
+ AttributeList attributeList = getAttributes(countMetricName, countMetricName.name(),
+ sumMetricName.name(), "name");
+ List<Attribute> attributes = attributeList.asList();
+ assertEquals(2, attributes.size());
+ for (Attribute attribute : attributes) {
+ if (countMetricName.name().equals(attribute.getName()))
+ assertEquals(2.0, attribute.getValue());
+ else if (sumMetricName.name().equals(attribute.getName()))
+ assertEquals(7.5, attribute.getValue());
+ else
+ fail("Unexpected attribute returned: " + attribute.getName());
+ }
+ }
+
+ @Test
+ public void testInvoke() throws Exception {
+ try {
+ mBeanServer.invoke(objectName(countMetricName), "something", null, null);
+ fail("invoke should have failed");
+ } catch (RuntimeMBeanException e) {
+ assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
+ }
+ }
+
+ @Test
+ public void testSetAttribute() throws Exception {
+ try {
+ mBeanServer.setAttribute(objectName(countMetricName), new Attribute("anything", 1));
+ fail("setAttribute should have failed");
+ } catch (RuntimeMBeanException e) {
+ assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
+ }
+ }
+
+ @Test
+ public void testSetAttributes() throws Exception {
+ try {
+ mBeanServer.setAttributes(objectName(countMetricName), new AttributeList(1));
+ fail("setAttributes should have failed");
+ } catch (Exception e) {
+ assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
+ }
+ }
+
+ private ObjectName objectName(MetricName metricName) throws Exception {
+ return new ObjectName(JmxReporter.getMBeanName("", metricName));
+ }
+
+ private Object getAttribute(MetricName metricName, String attribute) throws Exception {
+ return mBeanServer.getAttribute(objectName(metricName), attribute);
+ }
+
+ private Object getAttribute(MetricName metricName) throws Exception {
+ return getAttribute(metricName, metricName.name());
+ }
+
+ private AttributeList getAttributes(MetricName metricName, String... attributes) throws Exception {
+ return mBeanServer.getAttributes(objectName(metricName), attributes);
+ }
+
+}