From fb9582b5cc8fcd597bed10f09fd7d7fdf89bba82 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Mon, 24 Feb 2020 23:47:41 +0100 Subject: [PATCH 1/8] Add class name --- .../java/org/datadog/jmxfetch/Connection.java | 7 ++++++ .../java/org/datadog/jmxfetch/Filter.java | 18 ++++++++++++++ .../java/org/datadog/jmxfetch/Instance.java | 17 ++++++++++--- .../org/datadog/jmxfetch/JmxAttribute.java | 24 ++++++++++++++++++- .../datadog/jmxfetch/JmxComplexAttribute.java | 4 ++++ .../datadog/jmxfetch/JmxSimpleAttribute.java | 4 ++++ .../datadog/jmxfetch/JmxTabularAttribute.java | 4 ++++ .../java/org/datadog/jmxfetch/TestApp.java | 14 +++++++++++ src/test/resources/jmx_class_include.yaml | 11 +++++++++ 9 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/jmx_class_include.yaml diff --git a/src/main/java/org/datadog/jmxfetch/Connection.java b/src/main/java/org/datadog/jmxfetch/Connection.java index fa1ced60b..75632c2da 100644 --- a/src/main/java/org/datadog/jmxfetch/Connection.java +++ b/src/main/java/org/datadog/jmxfetch/Connection.java @@ -50,6 +50,13 @@ public MBeanAttributeInfo[] getAttributesForBean(ObjectName beanName) return mbs.getMBeanInfo(beanName).getAttributes(); } + /** Gets class for matching bean name. */ + public String getClassForBean(ObjectName beanName) + throws InstanceNotFoundException, IntrospectionException, ReflectionException, + IOException { + return mbs.getMBeanInfo(beanName).getClassName(); + } + /** Queries beans on specific scope. Returns set of matching query names.. */ public Set queryNames(ObjectName name) throws IOException { String scope = (name != null) ? name.toString() : "*:*"; diff --git a/src/main/java/org/datadog/jmxfetch/Filter.java b/src/main/java/org/datadog/jmxfetch/Filter.java index 40ffed831..5801dd38e 100644 --- a/src/main/java/org/datadog/jmxfetch/Filter.java +++ b/src/main/java/org/datadog/jmxfetch/Filter.java @@ -8,6 +8,7 @@ class Filter { HashMap filter; Pattern domainRegex; + Pattern classNameRegex; ArrayList beanRegexes = null; ArrayList excludeTags = null; HashMap additionalTags = null; @@ -150,6 +151,23 @@ public Pattern getDomainRegex() { return this.domainRegex; } + public String getClassName() { + return (String) filter.get("class"); + } + + public Pattern getClassNameRegex() { + if (this.filter.get("class_regex") == null) { + return null; + } + + if (this.classNameRegex == null) { + this.classNameRegex = Pattern.compile((String) this.filter.get("class_regex")); + } + + return this.classNameRegex; + } + + public Object getAttribute() { return filter.get("attribute"); } diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index ac5f8cebb..f23522f29 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -21,8 +21,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import javax.management.MBeanAttributeInfo; -import javax.management.ObjectName; +import javax.management.*; import javax.security.auth.login.FailedLoginException; @Slf4j @@ -477,7 +476,6 @@ private void getMatchingAttributes() throws IOException { } } MBeanAttributeInfo[] attributeInfos; - try { // Get all the attributes for bean_name log.debug("Getting attributes for bean: " + beanName); @@ -494,6 +492,16 @@ private void getMatchingAttributes() throws IOException { continue; } + String className; + + try { + className = connection.getClassForBean(beanName); + log.debug("ClassName: " + className); + } catch (Exception e) { + log.warn("Cannot get class name " + e.getMessage()); + continue; + } + for (MBeanAttributeInfo attributeInfo : attributeInfos) { if (metricsCount >= maxReturnedMetrics) { @@ -521,6 +529,7 @@ private void getMatchingAttributes() throws IOException { new JmxSimpleAttribute( attributeInfo, beanName, + className, instanceName, connection, tags, @@ -537,6 +546,7 @@ private void getMatchingAttributes() throws IOException { new JmxComplexAttribute( attributeInfo, beanName, + className, instanceName, connection, tags, @@ -552,6 +562,7 @@ private void getMatchingAttributes() throws IOException { new JmxTabularAttribute( attributeInfo, beanName, + className, instanceName, connection, tags, diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index 86f62f30a..e5d757f08 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -35,6 +35,7 @@ public abstract class JmxAttribute { "bean_name", "bean", "bean_regex", + "class_name", "attribute", "exclude_tags", "tags"); @@ -48,6 +49,7 @@ public abstract class JmxAttribute { private Connection connection; private ObjectName beanName; private String domain; + private String className; private String beanStringName; private HashMap beanParameters; private String attributeName; @@ -61,6 +63,7 @@ public abstract class JmxAttribute { JmxAttribute( MBeanAttributeInfo attribute, ObjectName beanName, + String className, String instanceName, Connection connection, Map instanceTags, @@ -68,6 +71,7 @@ public abstract class JmxAttribute { boolean emptyDefaultHostname) { this.attribute = attribute; this.beanName = beanName; + this.className = className; this.matchingConf = null; this.connection = connection; this.attributeName = attribute.getName(); @@ -272,7 +276,6 @@ boolean matchDomain(Configuration conf) { return (includeDomain == null || includeDomain.equals(domain)) && (includeDomainRegex == null || includeDomainRegex.matcher(domain).matches()); } - boolean excludeMatchDomain(Configuration conf) { String excludeDomain = conf.getExclude().getDomain(); Pattern excludeDomainRegex = conf.getExclude().getDomainRegex(); @@ -281,6 +284,25 @@ boolean excludeMatchDomain(Configuration conf) { || excludeDomainRegex != null && excludeDomainRegex.matcher(domain).matches(); } + + boolean matchClassName(Configuration conf) { + String includeClassName = conf.getInclude().getClassName(); + Pattern includeClassNameRegex = conf.getInclude().getClassNameRegex(); + log.debug("matchClassName className: " + className); + log.debug("matchClassName includeClassName: " + includeClassName); + + return (includeClassName == null || includeClassName.equals(className)) + && (includeClassNameRegex == null || includeClassNameRegex.matcher(className).matches()); + } + + boolean excludeMatchClassName(Configuration conf) { + String excludeClassName = conf.getExclude().getClassName(); + Pattern excludeClassNameRegex = conf.getExclude().getClassNameRegex(); + + return excludeClassName != null && excludeClassName.equals(className) + || excludeClassNameRegex != null && excludeClassNameRegex.matcher(className).matches(); + } + Object convertMetricValue(Object metricValue, String field) { Object converted = metricValue; diff --git a/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java index a0d0fce89..679551877 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxComplexAttribute.java @@ -23,6 +23,7 @@ public class JmxComplexAttribute extends JmxAttribute { public JmxComplexAttribute( MBeanAttributeInfo attribute, ObjectName beanName, + String className, String instanceName, Connection connection, Map instanceTags, @@ -30,6 +31,7 @@ public JmxComplexAttribute( super( attribute, beanName, + className, instanceName, connection, instanceTags, @@ -125,8 +127,10 @@ private Object getMetricType(String subAttribute) { @Override public boolean match(Configuration configuration) { if (!matchDomain(configuration) + || !matchClassName(configuration) || !matchBean(configuration) || excludeMatchDomain(configuration) + || excludeMatchClassName(configuration) || excludeMatchBean(configuration)) { return false; } diff --git a/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java index e3f7cf983..68700011e 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxSimpleAttribute.java @@ -20,6 +20,7 @@ public class JmxSimpleAttribute extends JmxAttribute { public JmxSimpleAttribute( MBeanAttributeInfo attribute, ObjectName beanName, + String className, String instanceName, Connection connection, Map instanceTags, @@ -28,6 +29,7 @@ public JmxSimpleAttribute( super( attribute, beanName, + className, instanceName, connection, instanceTags, @@ -53,9 +55,11 @@ public LinkedList> getMetrics() /** Returns whether an attribute matches in a configuration spec. */ public boolean match(Configuration configuration) { return matchDomain(configuration) + && matchClassName(configuration) && matchBean(configuration) && matchAttribute(configuration) && !(excludeMatchDomain(configuration) + || excludeMatchClassName(configuration) || excludeMatchBean(configuration) || excludeMatchAttribute(configuration)); } diff --git a/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java index 2e0a96b63..e7b9f12c0 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java @@ -32,6 +32,7 @@ public class JmxTabularAttribute extends JmxAttribute { public JmxTabularAttribute( MBeanAttributeInfo attribute, ObjectName beanName, + String className, String instanceName, Connection connection, Map instanceTags, @@ -39,6 +40,7 @@ public JmxTabularAttribute( super( attribute, beanName, + className, instanceName, connection, instanceTags, @@ -266,8 +268,10 @@ private Object getMetricType(String subAttribute) { @Override public boolean match(Configuration configuration) { if (!matchDomain(configuration) + || !matchClassName(configuration) || !matchBean(configuration) || excludeMatchDomain(configuration) + || excludeMatchClassName(configuration) || excludeMatchBean(configuration)) { return false; } diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 44c7449fb..24cb5ccd9 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -255,6 +255,20 @@ public void testDomainRegex() throws Exception { assertEquals(15, metrics.size()); } + @Test + public void testClassInclude() throws Exception { + // We expose a few metrics through JMX + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.includeme:type=AType"); + initApplication("jmx_class_include.yaml"); + + // Collecting metrics + run(); + LinkedList> metrics = getMetrics(); + + // First filter 29 = 13 metrics from java.lang + 16 metrics implicitly defined + assertEquals(29, metrics.size()); + } + @Test public void testParameterMatch() throws Exception { // Do not match beans which do not contain types specified in the conf diff --git a/src/test/resources/jmx_class_include.yaml b/src/test/resources/jmx_class_include.yaml new file mode 100644 index 000000000..3f9bbb695 --- /dev/null +++ b/src/test/resources/jmx_class_include.yaml @@ -0,0 +1,11 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + conf: + - include: + class: org.datadog.jmxfetch.SimpleTestJavaApp +# domain: org.datadog.jmxfetch.includeme + + From 0803441f29339a340bb8d455af6090bbc621d4c6 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Tue, 25 Feb 2020 00:36:37 +0100 Subject: [PATCH 2/8] Add tests --- .../org/datadog/jmxfetch/JmxAttribute.java | 4 +- .../datadog/jmxfetch/SimpleTestJavaApp2.java | 5 +++ .../java/org/datadog/jmxfetch/TestApp.java | 41 +++++++++++++++++++ src/test/resources/jmx_class_exclude.yaml | 13 ++++++ src/test/resources/jmx_class_include.yaml | 3 -- src/test/resources/jmx_class_regex.yaml | 14 +++++++ 6 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java create mode 100644 src/test/resources/jmx_class_exclude.yaml create mode 100644 src/test/resources/jmx_class_regex.yaml diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index e5d757f08..dbdc5d58c 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -35,7 +35,8 @@ public abstract class JmxAttribute { "bean_name", "bean", "bean_regex", - "class_name", + "class", + "class_regex", "attribute", "exclude_tags", "tags"); @@ -298,7 +299,6 @@ boolean matchClassName(Configuration conf) { boolean excludeMatchClassName(Configuration conf) { String excludeClassName = conf.getExclude().getClassName(); Pattern excludeClassNameRegex = conf.getExclude().getClassNameRegex(); - return excludeClassName != null && excludeClassName.equals(className) || excludeClassNameRegex != null && excludeClassNameRegex.matcher(className).matches(); } diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java new file mode 100644 index 000000000..e4104be4f --- /dev/null +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java @@ -0,0 +1,5 @@ +package org.datadog.jmxfetch; + +public class SimpleTestJavaApp2 extends SimpleTestJavaApp { + +} diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 24cb5ccd9..3217bec70 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -269,6 +269,47 @@ public void testClassInclude() throws Exception { assertEquals(29, metrics.size()); } + @Test + public void testClassExclude() throws Exception { + // We expose a few metrics through JMX + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.includeme:type=AType"); + registerMBean(new SimpleTestJavaApp2(), "org.datadog.jmxfetch.includeme2:type=AnotherType"); + + // Initializing application + initApplication("jmx_class_exclude.yaml"); + + // Collecting metrics + run(); + LinkedList> metrics = getMetrics(); + + // First filter 14 = 13 metrics from java.lang + 2 metrics explicitly define- 1 implicitly + // defined in the exclude section + assertEquals(14, metrics.size()); + } + + @Test + public void testClassRegex() throws Exception { + class SimpleTestJavaAppIncludeMe extends SimpleTestJavaApp { } + class SimpleTestJavaAppIncludeMeToo extends SimpleTestJavaApp { } + class SimpleTestJavaAppIncludeMeNotMeX extends SimpleTestJavaApp { } + + // We expose a few metrics through JMX + registerMBean(new SimpleTestJavaAppIncludeMe(), "org.datadog.jmxfetch.includeme:type=AType"); + registerMBean(new SimpleTestJavaAppIncludeMeToo(), "org.datadog.jmxfetch.includeme.too:type=AType"); + registerMBean(new SimpleTestJavaAppIncludeMeNotMeX(), "org.datadog.jmxfetch.includeme.not.me:type=AType"); + + // Initializing application + initApplication("jmx_class_regex.yaml"); + + // Collecting metrics + run(); + LinkedList> metrics = getMetrics(); + + // First filter 15 = 13 metrics from java.lang + 3 metrics explicitly defined - 1 implicitly + // defined in exclude section + assertEquals(15, metrics.size()); + } + @Test public void testParameterMatch() throws Exception { // Do not match beans which do not contain types specified in the conf diff --git a/src/test/resources/jmx_class_exclude.yaml b/src/test/resources/jmx_class_exclude.yaml new file mode 100644 index 000000000..3b405ea70 --- /dev/null +++ b/src/test/resources/jmx_class_exclude.yaml @@ -0,0 +1,13 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + conf: + - include: + attribute: + ShouldBe100: + metric_type: gauge + alias: this.is.100 + exclude: + class: org.datadog.jmxfetch.SimpleTestJavaApp2 diff --git a/src/test/resources/jmx_class_include.yaml b/src/test/resources/jmx_class_include.yaml index 3f9bbb695..560e80071 100644 --- a/src/test/resources/jmx_class_include.yaml +++ b/src/test/resources/jmx_class_include.yaml @@ -6,6 +6,3 @@ instances: conf: - include: class: org.datadog.jmxfetch.SimpleTestJavaApp -# domain: org.datadog.jmxfetch.includeme - - diff --git a/src/test/resources/jmx_class_regex.yaml b/src/test/resources/jmx_class_regex.yaml new file mode 100644 index 000000000..a74274b8d --- /dev/null +++ b/src/test/resources/jmx_class_regex.yaml @@ -0,0 +1,14 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + conf: + - include: + class_regex: .*IncludeMe.* + attribute: + ShouldBe100: + metric_type: gauge + alias: this.is.100 + exclude: + class_regex: .*Me$ From db59c39fd99b43a09b296a51f9dc971bddb05fc2 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Sun, 1 Mar 2020 13:56:56 +0100 Subject: [PATCH 3/8] Rename method --- src/main/java/org/datadog/jmxfetch/Connection.java | 2 +- src/main/java/org/datadog/jmxfetch/Instance.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/Connection.java b/src/main/java/org/datadog/jmxfetch/Connection.java index 75632c2da..4d26d5205 100644 --- a/src/main/java/org/datadog/jmxfetch/Connection.java +++ b/src/main/java/org/datadog/jmxfetch/Connection.java @@ -51,7 +51,7 @@ public MBeanAttributeInfo[] getAttributesForBean(ObjectName beanName) } /** Gets class for matching bean name. */ - public String getClassForBean(ObjectName beanName) + public String getClassNameForBean(ObjectName beanName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { return mbs.getMBeanInfo(beanName).getClassName(); diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index f23522f29..40ad1ca39 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -495,7 +495,7 @@ private void getMatchingAttributes() throws IOException { String className; try { - className = connection.getClassForBean(beanName); + className = connection.getClassNameForBean(beanName); log.debug("ClassName: " + className); } catch (Exception e) { log.warn("Cannot get class name " + e.getMessage()); From 8714944bc291c0751cefc473e0cd309985478975 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Sun, 1 Mar 2020 14:17:47 +0100 Subject: [PATCH 4/8] Refactor getMatchingAttributes --- .../java/org/datadog/jmxfetch/Instance.java | 18 ++++++------------ .../org/datadog/jmxfetch/JmxSubAttribute.java | 4 ++-- .../java/org/datadog/jmxfetch/TestApp.java | 6 +++--- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index d7a6af4ec..d9a527605 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -477,30 +477,24 @@ private void getMatchingAttributes() throws IOException { break; } } + String className; MBeanAttributeInfo[] attributeInfos; try { + log.debug("Getting class name for bean: " + beanName); + className = connection.getClassNameForBean(beanName); + // Get all the attributes for bean_name log.debug("Getting attributes for bean: " + beanName); attributeInfos = connection.getAttributesForBean(beanName); } catch (IOException e) { // we should not continue - log.warn("Cannot get bean attributes " + e.getMessage()); + log.warn("Cannot get bean attributes or class name: " + e.getMessage()); if (e.getMessage() == connection.CLOSED_CLIENT_CAUSE) { throw e; } continue; } catch (Exception e) { - log.warn("Cannot get bean attributes " + e.getMessage()); - continue; - } - - String className; - - try { - className = connection.getClassNameForBean(beanName); - log.debug("ClassName: " + className); - } catch (Exception e) { - log.warn("Cannot get class name " + e.getMessage()); + log.warn("Cannot get bean attributes or class name: " + e.getMessage()); continue; } diff --git a/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java index bb8e8ecb0..24b7973cb 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java @@ -8,10 +8,10 @@ abstract class JmxSubAttribute extends JmxAttribute { private Map cachedMetrics = new HashMap(); - public JmxSubAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, + public JmxSubAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String className, String instanceName, String checkName, Connection connection, Map instanceTags, boolean cassandraAliasing, boolean emptyDefaultHostname) { - super(attribute, beanName, instanceName, checkName, connection, instanceTags, + super(attribute, beanName, className, instanceName, checkName, connection, instanceTags, cassandraAliasing, emptyDefaultHostname); } diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 98257a094..7ab46f9be 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -264,7 +264,7 @@ public void testClassInclude() throws Exception { // Collecting metrics run(); - LinkedList> metrics = getMetrics(); + List> metrics = getMetrics(); // First filter 29 = 13 metrics from java.lang + 16 metrics implicitly defined assertEquals(29, metrics.size()); @@ -281,7 +281,7 @@ public void testClassExclude() throws Exception { // Collecting metrics run(); - LinkedList> metrics = getMetrics(); + List> metrics = getMetrics(); // First filter 14 = 13 metrics from java.lang + 2 metrics explicitly define- 1 implicitly // defined in the exclude section @@ -304,7 +304,7 @@ class SimpleTestJavaAppIncludeMeNotMeX extends SimpleTestJavaApp { } // Collecting metrics run(); - LinkedList> metrics = getMetrics(); + List> metrics = getMetrics(); // First filter 15 = 13 metrics from java.lang + 3 metrics explicitly defined - 1 implicitly // defined in exclude section From 2114b509ff5c7e8da50e9a643041374f53a7dca3 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Sun, 1 Mar 2020 14:22:30 +0100 Subject: [PATCH 5/8] Minor clean up --- src/main/java/org/datadog/jmxfetch/Connection.java | 2 +- src/main/java/org/datadog/jmxfetch/Instance.java | 3 ++- src/main/java/org/datadog/jmxfetch/JmxAttribute.java | 2 -- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/Connection.java b/src/main/java/org/datadog/jmxfetch/Connection.java index fdf3d2ad8..90cb2688e 100644 --- a/src/main/java/org/datadog/jmxfetch/Connection.java +++ b/src/main/java/org/datadog/jmxfetch/Connection.java @@ -50,7 +50,7 @@ public MBeanAttributeInfo[] getAttributesForBean(ObjectName beanName) return mbs.getMBeanInfo(beanName).getAttributes(); } - /** Gets class for matching bean name. */ + /** Gets class name for matching bean name. */ public String getClassNameForBean(ObjectName beanName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index d9a527605..ef39d5189 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -20,7 +20,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import javax.management.*; +import javax.management.MBeanAttributeInfo; +import javax.management.ObjectName; import javax.security.auth.login.FailedLoginException; @Slf4j diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index 5598d59c6..a8414a19b 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -291,8 +291,6 @@ boolean excludeMatchDomain(Configuration conf) { boolean matchClassName(Configuration conf) { String includeClassName = conf.getInclude().getClassName(); Pattern includeClassNameRegex = conf.getInclude().getClassNameRegex(); - log.debug("matchClassName className: " + className); - log.debug("matchClassName includeClassName: " + includeClassName); return (includeClassName == null || includeClassName.equals(className)) && (includeClassNameRegex == null || includeClassNameRegex.matcher(className).matches()); From 0577cbc71b6bee3211852103e300b9df5e364023 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Sun, 1 Mar 2020 15:14:37 +0100 Subject: [PATCH 6/8] Fix style --- src/main/java/org/datadog/jmxfetch/JmxAttribute.java | 7 +++++-- src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index a8414a19b..9dd21f39d 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -279,6 +279,7 @@ boolean matchDomain(Configuration conf) { return (includeDomain == null || includeDomain.equals(domain)) && (includeDomainRegex == null || includeDomainRegex.matcher(domain).matches()); } + boolean excludeMatchDomain(Configuration conf) { String excludeDomain = conf.getExclude().getDomain(); Pattern excludeDomainRegex = conf.getExclude().getDomainRegex(); @@ -293,14 +294,16 @@ boolean matchClassName(Configuration conf) { Pattern includeClassNameRegex = conf.getInclude().getClassNameRegex(); return (includeClassName == null || includeClassName.equals(className)) - && (includeClassNameRegex == null || includeClassNameRegex.matcher(className).matches()); + && (includeClassNameRegex == null + || includeClassNameRegex.matcher(className).matches()); } boolean excludeMatchClassName(Configuration conf) { String excludeClassName = conf.getExclude().getClassName(); Pattern excludeClassNameRegex = conf.getExclude().getClassNameRegex(); return excludeClassName != null && excludeClassName.equals(className) - || excludeClassNameRegex != null && excludeClassNameRegex.matcher(className).matches(); + || excludeClassNameRegex != null + && excludeClassNameRegex.matcher(className).matches(); } Object convertMetricValue(Object metricValue, String field) { diff --git a/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java index 24b7973cb..cfeb35f4b 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxSubAttribute.java @@ -8,9 +8,10 @@ abstract class JmxSubAttribute extends JmxAttribute { private Map cachedMetrics = new HashMap(); - public JmxSubAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String className, String instanceName, - String checkName, Connection connection, Map instanceTags, - boolean cassandraAliasing, boolean emptyDefaultHostname) { + public JmxSubAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String className, + String instanceName, String checkName, Connection connection, + Map instanceTags, boolean cassandraAliasing, + boolean emptyDefaultHostname) { super(attribute, beanName, className, instanceName, checkName, connection, instanceTags, cassandraAliasing, emptyDefaultHostname); } From 3e9207dd17e89d75966614fe8ebf33555b0bbd64 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Sun, 1 Mar 2020 23:34:50 +0100 Subject: [PATCH 7/8] Factor match name --- .../org/datadog/jmxfetch/JmxAttribute.java | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index 9dd21f39d..638fcaec8 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -273,37 +273,39 @@ Object getJmxValue() } boolean matchDomain(Configuration conf) { - String includeDomain = conf.getInclude().getDomain(); - Pattern includeDomainRegex = conf.getInclude().getDomainRegex(); - - return (includeDomain == null || includeDomain.equals(domain)) - && (includeDomainRegex == null || includeDomainRegex.matcher(domain).matches()); + return includeMatchName(domain, + conf.getInclude().getDomain(), + conf.getInclude().getDomainRegex()); } boolean excludeMatchDomain(Configuration conf) { - String excludeDomain = conf.getExclude().getDomain(); - Pattern excludeDomainRegex = conf.getExclude().getDomainRegex(); - - return excludeDomain != null && excludeDomain.equals(domain) - || excludeDomainRegex != null && excludeDomainRegex.matcher(domain).matches(); + return excludeMatchName(domain, + conf.getExclude().getDomain(), + conf.getExclude().getDomainRegex()); } boolean matchClassName(Configuration conf) { - String includeClassName = conf.getInclude().getClassName(); - Pattern includeClassNameRegex = conf.getInclude().getClassNameRegex(); - - return (includeClassName == null || includeClassName.equals(className)) - && (includeClassNameRegex == null - || includeClassNameRegex.matcher(className).matches()); + return includeMatchName(className, + conf.getInclude().getClassName(), + conf.getInclude().getClassNameRegex()); } + boolean excludeMatchClassName(Configuration conf) { - String excludeClassName = conf.getExclude().getClassName(); - Pattern excludeClassNameRegex = conf.getExclude().getClassNameRegex(); - return excludeClassName != null && excludeClassName.equals(className) - || excludeClassNameRegex != null - && excludeClassNameRegex.matcher(className).matches(); + return excludeMatchName(className, + conf.getExclude().getClassName(), + conf.getExclude().getClassNameRegex()); + } + + private boolean includeMatchName(String name, String includeName, Pattern includeNameRegex) { + return (includeName == null || includeName.equals(name)) + && (includeNameRegex == null || includeNameRegex.matcher(name).matches()); + } + + private boolean excludeMatchName(String name, String excludeName, Pattern excludeNameRegex) { + return (excludeName != null && excludeName.equals(name)) + || (excludeNameRegex != null && excludeNameRegex.matcher(name).matches()); } Object convertMetricValue(Object metricValue, String field) { From 77d90247a9de0f5fb0fcc16556c580d2416a4d94 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Tue, 3 Mar 2020 09:49:23 +0100 Subject: [PATCH 8/8] Make SimpleTestJavaApp2 a inner class --- src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java | 5 ----- src/test/java/org/datadog/jmxfetch/TestApp.java | 6 +++++- src/test/resources/jmx_class_exclude.yaml | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) delete mode 100644 src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java deleted file mode 100644 index e4104be4f..000000000 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp2.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.datadog.jmxfetch; - -public class SimpleTestJavaApp2 extends SimpleTestJavaApp { - -} diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 7ab46f9be..3c7fa8381 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -272,9 +272,13 @@ public void testClassInclude() throws Exception { @Test public void testClassExclude() throws Exception { + class SimpleTestJavaAnotherApp extends SimpleTestJavaApp { + + } + // We expose a few metrics through JMX registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.includeme:type=AType"); - registerMBean(new SimpleTestJavaApp2(), "org.datadog.jmxfetch.includeme2:type=AnotherType"); + registerMBean(new SimpleTestJavaAnotherApp(), "org.datadog.jmxfetch.includeme2:type=AnotherType"); // Initializing application initApplication("jmx_class_exclude.yaml"); diff --git a/src/test/resources/jmx_class_exclude.yaml b/src/test/resources/jmx_class_exclude.yaml index 3b405ea70..22c06256e 100644 --- a/src/test/resources/jmx_class_exclude.yaml +++ b/src/test/resources/jmx_class_exclude.yaml @@ -10,4 +10,4 @@ instances: metric_type: gauge alias: this.is.100 exclude: - class: org.datadog.jmxfetch.SimpleTestJavaApp2 + class: org.datadog.jmxfetch.TestApp$1SimpleTestJavaAnotherApp