diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index e3263abc8..0c74d3f17 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -133,7 +133,7 @@ public Instance( // Make sure to refresh the beans list every 10 minutes // Useful because sometimes if the application restarts, jmxfetch might read // a jmxtree that is not completely initialized and would be missing some attributes - this.refreshBeansPeriod = DEFAULT_REFRESH_BEANS_PERIOD; + this.refreshBeansPeriod = DEFAULT_REFRESH_BEANS_PERIOD; } } else { // Allow global overrides @@ -146,7 +146,7 @@ public Instance( // by refresh_beans // Useful for Java applications that are lazy loaded and may take some time after // application startup before actually being exposed - this.initialRefreshBeansPeriod = this.refreshBeansPeriod; + this.initialRefreshBeansPeriod = this.refreshBeansPeriod; } } else { // Allow global overrides @@ -575,6 +575,7 @@ private void getMatchingAttributes() throws IOException { beanName, className, instanceName, + service, checkName, connection, tags, @@ -593,6 +594,7 @@ private void getMatchingAttributes() throws IOException { beanName, className, instanceName, + service, checkName, connection, tags, @@ -610,6 +612,7 @@ private void getMatchingAttributes() throws IOException { beanName, className, instanceName, + service, checkName, connection, tags, @@ -725,10 +728,6 @@ public String[] getServiceCheckTags() { } tags.add("instance:" + this.instanceName); - if (this.service != null && !this.service.isEmpty()) { - tags.add("service:" + this.service); - } - if (this.emptyDefaultHostname) { tags.add("host:"); } diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index 638fcaec8..d3920b276 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -50,6 +50,7 @@ public abstract class JmxAttribute { private ObjectName beanName; private String domain; private String className; + private String serviceName; private String beanStringName; private Map beanParameters; private String attributeName; @@ -66,6 +67,7 @@ public abstract class JmxAttribute { ObjectName beanName, String className, String instanceName, + String serviceName, String checkName, Connection connection, Map instanceTags, @@ -80,6 +82,7 @@ public abstract class JmxAttribute { this.beanStringName = beanName.toString(); this.cassandraAliasing = cassandraAliasing; this.checkName = checkName; + this.serviceName = serviceName; // A bean name is formatted like that: // org.apache.cassandra.db:type=Caches,keyspace=system,cache=HintsColumnFamilyKeyCache @@ -109,7 +112,8 @@ private void applyTagsBlackList() { for (String excludedTagName : include.getExcludeTags()) { for (Iterator it = this.defaultTagsList.iterator(); it.hasNext(); ) { - if (it.next().startsWith(excludedTagName + ":")) { + String tag = it.next(); + if (tag.startsWith(excludedTagName + ":")) { it.remove(); } } @@ -132,6 +136,12 @@ private void addAdditionalTags() { } } + private void addServiceTag() { + if (serviceName != null && !serviceName.isEmpty()) { + this.defaultTagsList.add("service:" + serviceName); + } + } + /** Gets bean parameter hash map. */ public static Map getBeanParametersHash(String beanParametersString) { String[] beanParameters = beanParametersString.split(","); @@ -467,6 +477,9 @@ public void setMatchingConf(Configuration matchingConf) { this.addAdditionalTags(); // - filter out excluded tags this.applyTagsBlackList(); + // Add the service tag - comes last because if the service tag is blacklisted as + // a JmxAttribute tag, we still want to include the service specified in the config. + this.addServiceTag(); } MBeanAttributeInfo getAttribute() { diff --git a/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java index 4a76ede7c..5214231ee 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java @@ -24,6 +24,7 @@ public JmxComplexAttribute( ObjectName beanName, String className, String instanceName, + String serviceName, String checkName, Connection connection, Map instanceTags, @@ -33,6 +34,7 @@ public JmxComplexAttribute( beanName, className, instanceName, + serviceName, checkName, connection, instanceTags, diff --git a/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java index d5107a1f4..13b0f8abf 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java @@ -21,6 +21,7 @@ public JmxSimpleAttribute( ObjectName beanName, String className, String instanceName, + String serviceName, String checkName, Connection connection, Map instanceTags, @@ -31,6 +32,7 @@ public JmxSimpleAttribute( beanName, className, instanceName, + serviceName, checkName, connection, instanceTags, diff --git a/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java index cfeb35f4b..4ebafb4f0 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java @@ -9,11 +9,11 @@ abstract class JmxSubAttribute extends JmxAttribute { private Map cachedMetrics = new HashMap(); public JmxSubAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String className, - String instanceName, String checkName, Connection connection, + String instanceName, String serviceName, String checkName, Connection connection, Map instanceTags, boolean cassandraAliasing, boolean emptyDefaultHostname) { - super(attribute, beanName, className, instanceName, checkName, connection, instanceTags, - cassandraAliasing, emptyDefaultHostname); + super(attribute, beanName, className, instanceName, serviceName, checkName, connection, + instanceTags, cassandraAliasing, emptyDefaultHostname); } public Metric getCachedMetric(String name) { diff --git a/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java index e3246a5b5..d6f122634 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java @@ -33,6 +33,7 @@ public JmxTabularAttribute( ObjectName beanName, String className, String instanceName, + String serviceName, String checkName, Connection connection, Map instanceTags, @@ -42,6 +43,7 @@ public JmxTabularAttribute( beanName, className, instanceName, + serviceName, checkName, connection, instanceTags, diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 4411628a5..797c25b9e 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -509,6 +509,31 @@ public void testExcludeTags() throws Exception { assertMetric("test1.histogram", 424242, commonTags, 2, "histogram"); } + @Test + public void testExcludeServiceTagsAndOverride() throws Exception { + // We expose a few metrics through JMX + SimpleTestJavaApp testApp = new SimpleTestJavaApp(); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp,service=foo"); + + // We do a first collection + initApplication("jmx_exclude_tags_override_service.yml"); + run(); + List> metrics = getMetrics(); + + // We test for the presence and the value of the metrics we want to collect. + // Tags "type", "newTag" and "env" should be excluded + List commonTags = + Arrays.asList("instance:jmx_test_service_override_instance", + "jmx_domain:org.datadog.jmxfetch.test","service:test"); + + // 15 = 13 metrics from java.lang + the 2 collected (gauge and histogram) + assertEquals(15, metrics.size()); + + // There should only left 2 tags per metric + assertMetric("test1.gauge", 1000.0, commonTags, 3, "gauge"); + assertMetric("test1.histogram", 424242, commonTags, 3, "histogram"); + } + @Test public void testAdditionalTags() throws Exception { // We expose a few metrics through JMX @@ -844,7 +869,7 @@ public void testAppCounterRate() throws Exception { assertCoverage(); } - /** + /** * Test JMX Service Discovery. * */ @Test diff --git a/src/test/resources/jmx_exclude_tags_override_service.yml b/src/test/resources/jmx_exclude_tags_override_service.yml new file mode 100644 index 000000000..274fb76a9 --- /dev/null +++ b/src/test/resources/jmx_exclude_tags_override_service.yml @@ -0,0 +1,24 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_service_override_instance + service: test + tags: + env: stage + newTag: test + conf: + - include: + domain: org.datadog.jmxfetch.test + exclude_tags: + - env + - type + - newTag + - service + attribute: + ShouldBe1000: + metric_type: gauge + alias: test1.gauge + Int424242: + metric_type: histogram + alias: test1.histogram