diff --git a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java index c6beb0013..161b6c59b 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java @@ -55,12 +55,12 @@ public abstract class JMXAttribute { String[] splitBeanName = this.beanName.split(":"); String domain = splitBeanName[0]; String beanParameters = splitBeanName[1]; - LinkedList defaultTags = getBeanTags(instanceName, domain, beanParameters, instanceTags); - HashMap beanParametersHash = getBeanParametersHash(defaultTags); + LinkedList beanParametersList = getBeanParametersList(instanceName, domain, beanParameters, instanceTags); + HashMap beanParametersHash = getBeanParametersHash(beanParametersList); this.domain = domain; this.beanParameters = beanParametersHash; - this.defaultTagsList = defaultTags; + this.defaultTagsList = renameConflictingParameters(beanParametersList); } private static HashMap getBeanParametersHash(LinkedList beanParameters) { @@ -74,7 +74,7 @@ private static HashMap getBeanParametersHash(LinkedList } - private static LinkedList getBeanTags(String instanceName, String domain, String beanParameters, HashMap instanceTags) { + private static LinkedList getBeanParametersList(String instanceName, String domain, String beanParameters, HashMap instanceTags) { LinkedList beanTags = new LinkedList(Arrays.asList(new String(beanParameters).replace("=", ":").split(","))); beanTags.add("instance:" + instanceName); beanTags.add("jmx_domain:" + domain); @@ -88,6 +88,20 @@ private static LinkedList getBeanTags(String instanceName, String domain return beanTags; } + private static LinkedList renameConflictingParameters(LinkedList beanParametersList) { + LinkedList defaultTagsList = new LinkedList(); + for (String beanParameter: beanParametersList) { + // the 'host' parameter is renamed to 'bean_host' + if (beanParameter.startsWith("host:")) { + defaultTagsList.add("bean_host:" + beanParameter.substring("host:".length())); + } else { + defaultTagsList.add(beanParameter); + } + } + + return defaultTagsList; + } + static String convertMetricName(String metricName) { metricName = metricName.replaceAll(FIRST_CAP_PATTERN, "$1_$2"); metricName = metricName.replaceAll(ALL_CAP_PATTERN, "$1_$2").toLowerCase(); diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index da477ed14..9e04c1569 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -48,7 +48,7 @@ public static App initApp(String yamlFileName, AppConfig appConfig){ public void testBeanTags() throws Exception { // We expose a few metrics through JMX MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope"); + ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope,host=localhost"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); mbs.registerMBean(testApp, objectName); @@ -66,19 +66,21 @@ public void testBeanTags() throws Exception { // Fetching our 'defined' metric tags for (HashMap m : metrics) { - String name = (String) (m.get("name")); - if(!name.equals("this.is.100")){ - continue; - } - String[] tags = (String[]) (m.get("tags")); - Set tagsSet = new HashSet(Arrays.asList(tags)); - - // We should find bean parameters as tags - assertEquals(4, tags.length); - assertEquals(true, tagsSet.contains("type:SimpleTestJavaApp")); - assertEquals(true, tagsSet.contains("scope:CoolScope")); - assertEquals(true, tagsSet.contains("instance:jmx_test_instance")); - assertEquals(true, tagsSet.contains("jmx_domain:org.datadog.jmxfetch.test")); + String name = (String) (m.get("name")); + if(!name.equals("this.is.100")){ + continue; + } + String[] tags = (String[]) (m.get("tags")); + Set tagsSet = new HashSet(Arrays.asList(tags)); + + // We should find bean parameters as tags + assertEquals(5, tags.length); + assertEquals(true, tagsSet.contains("type:SimpleTestJavaApp")); + assertEquals(true, tagsSet.contains("scope:CoolScope")); + assertEquals(true, tagsSet.contains("instance:jmx_test_instance")); + assertEquals(true, tagsSet.contains("jmx_domain:org.datadog.jmxfetch.test")); + // Special case of the 'host' parameter which tag is renamed to 'bean_host' + assertEquals(true, tagsSet.contains("bean_host:localhost")); } mbs.unregisterMBean(objectName); } @@ -126,8 +128,8 @@ public void testDomainExclude() throws Exception { // First filter 14 = 13 metrics from java.lang + 2 metrics explicitly define- 1 implicitly defined in the exclude section assertEquals(14, metrics.size()); - mbs.unregisterMBean(includeMe); - mbs.unregisterMBean(excludeMe); + mbs.unregisterMBean(includeMe); + mbs.unregisterMBean(excludeMe); } @Test diff --git a/src/test/resources/jmx_bean_tags.yaml b/src/test/resources/jmx_bean_tags.yaml index 967856ed9..a299dba61 100644 --- a/src/test/resources/jmx_bean_tags.yaml +++ b/src/test/resources/jmx_bean_tags.yaml @@ -5,13 +5,13 @@ instances: name: jmx_test_instance conf: - include: - bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope + bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope,host=localhost attribute: ShouldBe100: metric_type: gauge alias: this.is.100 - include: - bean: org.datadog.jmxfetch.test:type=WrongType,scope=WrongScope + bean: org.datadog.jmxfetch.test:type=WrongType,scope=WrongScope,host=localhost attribute: Hashmap.thisis0: metric_type: gauge