kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maniku...@apache.org
Subject [kafka] branch trunk updated: MINOR: fix backwards incompatibility in JmxReporter introduced by KIP-606
Date Fri, 05 Jun 2020 20:01:59 GMT
This is an automated email from the ASF dual-hosted git repository.

manikumar pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 7a876ec  MINOR: fix backwards incompatibility in JmxReporter introduced by KIP-606
7a876ec is described below

commit 7a876ec9dea931af0f17d6a856a5932091ef5056
Author: Xavier Léauté <xvrl@apache.org>
AuthorDate: Sat Jun 6 01:30:48 2020 +0530

    MINOR: fix backwards incompatibility in JmxReporter introduced by KIP-606
    
    cc omkreddy this should also get backported to 2.6.x
    
    Author: Xavier Léauté <xvrl@apache.org>
    
    Reviewers: Manikumar Reddy <manikumar.reddy@gmail.com>
    
    Closes #8813 from xvrl/fix-jmx-reset
---
 .../org/apache/kafka/common/metrics/JmxReporter.java  |  8 ++++++++
 .../apache/kafka/common/metrics/JmxReporterTest.java  | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)

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 090f93b..73522a9 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
@@ -332,6 +332,14 @@ public class JmxReporter implements MetricsReporter {
             if (!mbeans.isEmpty()) {
                 throw new IllegalStateException("JMX MetricsContext can only be updated before
JMX metrics are created");
             }
+
+            // prevent prefix from getting reset back to empty for backwards compatibility
+            // with the deprecated JmxReporter(String prefix) constructor, in case contextChange
gets called
+            // via one of the Metrics() constructor with a default empty MetricsContext()
+            if (namespace.isEmpty()) {
+                return;
+            }
+
             prefix = namespace;
         }
     }
diff --git a/clients/src/test/java/org/apache/kafka/common/metrics/JmxReporterTest.java b/clients/src/test/java/org/apache/kafka/common/metrics/JmxReporterTest.java
index 5bc831c..3285889 100644
--- a/clients/src/test/java/org/apache/kafka/common/metrics/JmxReporterTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/metrics/JmxReporterTest.java
@@ -175,4 +175,23 @@ public class JmxReporterTest {
             metrics.close();
         }
     }
+
+    @Test
+    public void testDeprecatedJmxPrefixWithDefaultMetrics() throws Exception {
+        @SuppressWarnings("deprecation")
+        JmxReporter reporter = new JmxReporter("my-prefix");
+
+        // for backwards compatibility, ensure prefix does not get overridden by the default
empty namespace in metricscontext
+        MetricConfig metricConfig = new MetricConfig();
+        Metrics metrics = new Metrics(metricConfig, new ArrayList<>(Arrays.asList(reporter)),
Time.SYSTEM);
+
+        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        try {
+            Sensor sensor = metrics.sensor("my-sensor");
+            sensor.add(metrics.metricName("pack.bean1.avg", "grp1"), new Avg());
+            assertEquals("my-prefix", server.getObjectInstance(new ObjectName("my-prefix:type=grp1")).getObjectName().getDomain());
+        } finally {
+            metrics.close();
+        }
+    }
 }


Mime
View raw message