diff --git a/.travis.yml b/.travis.yml index fc5611fc1..817f20ee1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ jdk: - openjdk7 install: mvn install -Dhttps.protocols=TLSv1.2 -DskipTests=true -Dmaven.javadoc.skip=true -B -V -script: mvn test -B -Dhttps.protocols=TLSv1.2 +script: mvn test -B -Dhttps.protocols=TLSv1.2 -Dlog4j.configuration=log4j.travis.properties addons: hostname: dd-jmxfetch-testhost diff --git a/log4j.travis.properties b/log4j.travis.properties new file mode 100644 index 000000000..0641702ac --- /dev/null +++ b/log4j.travis.properties @@ -0,0 +1,8 @@ +log4j.rootLogger=INFO,stdout +log4j.logger.com.endeca=INFO +# Logger for crawl metrics +log4j.logger.com.endeca.itl.web.metrics=INFO + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 5fb6f0a50..ce12c67d6 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -57,6 +57,7 @@ public class Instance { private Connection connection; private AppConfig appConfig; private Boolean cassandraAliasing; + private boolean emptyDefaultHostname; public Instance(Instance instance, AppConfig appConfig) { @@ -97,6 +98,13 @@ public Instance(LinkedHashMap instanceMap, LinkedHashMap> getMetrics() throws IOException { } return metrics; } - + public boolean timeToCollect() { if (this.minCollectionPeriod == null) { return true; @@ -365,13 +373,13 @@ private void getMatchingAttributes() { String attributeType = attributeInfo.getType(); if (SIMPLE_TYPES.contains(attributeType)) { LOGGER.debug(ATTRIBUTE + beanName + " : " + attributeInfo + " has attributeInfo simple type"); - jmxAttribute = new JMXSimpleAttribute(attributeInfo, beanName, instanceName, connection, tags, cassandraAliasing); + jmxAttribute = new JMXSimpleAttribute(attributeInfo, beanName, instanceName, connection, tags, cassandraAliasing, emptyDefaultHostname); } else if (COMPOSED_TYPES.contains(attributeType)) { LOGGER.debug(ATTRIBUTE + beanName + " : " + attributeInfo + " has attributeInfo composite type"); - jmxAttribute = new JMXComplexAttribute(attributeInfo, beanName, instanceName, connection, tags); + jmxAttribute = new JMXComplexAttribute(attributeInfo, beanName, instanceName, connection, tags, emptyDefaultHostname); } else if (MULTI_TYPES.contains(attributeType)) { LOGGER.debug(ATTRIBUTE + beanName + " : " + attributeInfo + " has attributeInfo tabular type"); - jmxAttribute = new JMXTabularAttribute(attributeInfo, beanName, instanceName, connection, tags); + jmxAttribute = new JMXTabularAttribute(attributeInfo, beanName, instanceName, connection, tags, emptyDefaultHostname); } else { try { LOGGER.debug(ATTRIBUTE + beanName + " : " + attributeInfo + " has an unsupported type: " + attributeType); @@ -462,6 +470,10 @@ public String[] getServiceCheckTags() { } } tags.add("instance:" + this.instanceName); + + if (this.emptyDefaultHostname) { + tags.add("host:"); + } return tags.toArray(new String[tags.size()]); } diff --git a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java index d6ae20afa..ca3ea0dc8 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java @@ -51,7 +51,8 @@ public abstract class JMXAttribute { private Boolean cassandraAliasing; JMXAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, - Connection connection, HashMap instanceTags, Boolean cassandraAliasing) { + Connection connection, HashMap instanceTags, Boolean cassandraAliasing, + boolean emptyDefaultHostname) { this.attribute = attribute; this.beanName = beanName; this.matchingConf = null; @@ -73,6 +74,9 @@ public abstract class JMXAttribute { this.beanParameters = beanParametersHash; this.defaultTagsList = sanitizeParameters(beanParametersList); + if (emptyDefaultHostname) { + this.defaultTagsList.add("host:"); + } } /** @@ -303,7 +307,7 @@ private boolean matchBeanRegex(Filter filter, boolean matchIfNoRegex) { Matcher m = beanRegex.matcher(beanStringName); if(m.matches()) { - for (int i = 0; i<= m.groupCount(); i++) { + for (int i = 0; i<= m.groupCount(); i++) { this.beanParameters.put(Integer.toString(i), m.group(i)); } return true; diff --git a/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java index d38bf6218..a67042474 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java @@ -22,8 +22,8 @@ public class JMXComplexAttribute extends JMXAttribute { private HashMap> subAttributeList; public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, - Connection connection, HashMap instanceTags) { - super(attribute, beanName, instanceName, connection, instanceTags, false); + Connection connection, HashMap instanceTags, boolean emptyDefaultHostname) { + super(attribute, beanName, instanceName, connection, instanceTags, false, emptyDefaultHostname); this.subAttributeList = new HashMap>(); } diff --git a/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java index 195c53850..10932b36c 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java @@ -19,8 +19,9 @@ public class JMXSimpleAttribute extends JMXAttribute { private String metricType; public JMXSimpleAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, - Connection connection, HashMap instanceTags, Boolean cassandraAliasing) { - super(attribute, beanName, instanceName, connection, instanceTags, cassandraAliasing); + Connection connection, HashMap instanceTags, boolean cassandraAliasing, + Boolean emptyDefaultHostname) { + super(attribute, beanName, instanceName, connection, instanceTags, cassandraAliasing, emptyDefaultHostname); } @Override diff --git a/src/main/java/org/datadog/jmxfetch/JMXTabularAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXTabularAttribute.java index e4e121ac1..441b063a3 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXTabularAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXTabularAttribute.java @@ -27,8 +27,8 @@ public class JMXTabularAttribute extends JMXAttribute { private HashMap>> subAttributeList; public JMXTabularAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, - Connection connection, HashMap instanceTags) { - super(attribute, beanName, instanceName, connection, instanceTags, false); + Connection connection, HashMap instanceTags, boolean emptyDefaultHostname) { + super(attribute, beanName, instanceName, connection, instanceTags, false, emptyDefaultHostname); subAttributeList = new HashMap>>(); } diff --git a/src/test/java/org/datadog/jmxfetch/TestInstance.java b/src/test/java/org/datadog/jmxfetch/TestInstance.java index 45a3852d4..0fe3dd244 100644 --- a/src/test/java/org/datadog/jmxfetch/TestInstance.java +++ b/src/test/java/org/datadog/jmxfetch/TestInstance.java @@ -1,21 +1,26 @@ package org.datadog.jmxfetch; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; import java.util.HashMap; import java.util.LinkedList; +import java.util.Arrays; +import java.util.List; import org.apache.log4j.Logger; import org.junit.Test; public class TestInstance extends TestCommon { private final static Logger LOGGER = Logger.getLogger("Test Instance"); - + @Test public void testMinCollectionInterval() throws Exception { registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); initApplication("jmx_min_collection_period.yml"); - + run(); LinkedList> metrics = getMetrics(); assertEquals(15, metrics.size()); @@ -23,7 +28,7 @@ public void testMinCollectionInterval() throws Exception { run(); metrics = getMetrics(); assertEquals(0, metrics.size()); - + LOGGER.info("sleeping before the next collection"); Thread.sleep(5000); run(); @@ -31,4 +36,40 @@ public void testMinCollectionInterval() throws Exception { assertEquals(15, metrics.size()); } + // assertHostnameTags is used by testEmptyDefaultHostname + private void assertHostnameTags(List tagList) throws Exception { + // Fixed instance tag + assertTrue(tagList.contains(new String("jmx:fetch"))); + + if (tagList.contains(new String("instance:jmx_test_default_hostname"))) { + // Nominal case + assertFalse(tagList.contains(new String("host:"))); + } else if (tagList.contains(new String("instance:jmx_test_no_hostname"))) { + // empty_default_hostname case + assertTrue(tagList.contains(new String("host:"))); + } else { + fail("unexpected instance tag"); + } + } + + @Test + public void testEmptyDefaultHostname() throws Exception { + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); + initApplication("jmx_empty_default_hostname.yaml"); + run(); + + LinkedList> metrics = getMetrics(); + assertEquals(28, metrics.size()); + for (HashMap metric : metrics) { + String[] tags = (String[]) metric.get("tags"); + this.assertHostnameTags(Arrays.asList(tags)); + } + + LinkedList> serviceChecks = getServiceChecks(); + assertEquals(2, serviceChecks.size()); + for (HashMap sc : serviceChecks) { + String[] tags = (String[]) sc.get("tags"); + this.assertHostnameTags(Arrays.asList(tags)); + } + } } diff --git a/src/test/resources/jmx_empty_default_hostname.yaml b/src/test/resources/jmx_empty_default_hostname.yaml new file mode 100644 index 000000000..464160882 --- /dev/null +++ b/src/test/resources/jmx_empty_default_hostname.yaml @@ -0,0 +1,27 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_default_hostname + tags: + - jmx:fetch + conf: + - include: + domain: org.datadog.jmxfetch.test + attribute: + ShouldBe100: + metric_type: gauge + alias: this.is.100.$foo.$qux + + - process_name_regex: .*surefire.* + empty_default_hostname: true + name: jmx_test_no_hostname + tags: + - jmx:fetch + conf: + - include: + domain: org.datadog.jmxfetch.test + attribute: + ShouldBe100: + metric_type: gauge + alias: this.is.100.$foo.$qux