diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index ded593d60..04654059a 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -434,7 +434,8 @@ public Connection getConnection( /** Initializes the instance. May force a new connection.. */ public void init(boolean forceNewConnection) - throws IOException, FailedLoginException, SecurityException { + throws IOException, FailedLoginException, SecurityException, + MalformedObjectNameException { log.info("Trying to connect to JMX Server at " + this.toString()); connection = getConnection(instanceMap, forceNewConnection); log.info( @@ -467,7 +468,7 @@ public String toString() { } /** Returns a map of metrics collected. */ - public List getMetrics() throws IOException { + public List getMetrics() throws IOException, MalformedObjectNameException { // In case of ephemeral beans, we can force to refresh the bean list x seconds // post initialization and every x seconds thereafter. @@ -509,13 +510,18 @@ public List getMetrics() throws IOException { } } } - instanceTelemetryBean.setBeansFetched(beans.size()); - instanceTelemetryBean.setTopLevelAttributeCount(matchingAttributes.size()); - instanceTelemetryBean.setMetricCount(metrics.size()); - log.debug("Updated jmx bean for instance: " + this.getCheckName() - + " With beans fetched = " + instanceTelemetryBean.getBeansFetched() - + " top attributes = " + instanceTelemetryBean.getTopLevelAttributeCount() - + " metrics = " + instanceTelemetryBean.getMetricCount()); + if (instanceTelemetryBean != null) { + instanceTelemetryBean.setBeansFetched(beans.size()); + instanceTelemetryBean.setTopLevelAttributeCount(matchingAttributes.size()); + instanceTelemetryBean.setMetricCount(metrics.size()); + log.debug("Updated jmx bean for instance: " + this.getCheckName() + + " With beans fetched = " + instanceTelemetryBean.getBeansFetched() + + " top attributes = " + instanceTelemetryBean.getTopLevelAttributeCount() + + " metrics = " + instanceTelemetryBean.getMetricCount() + + " domains queried = " + instanceTelemetryBean.getDomainsQueried() + + " wildcard query count = " + instanceTelemetryBean.getWildcardQueryCount() + + " attribute match ratio = " + instanceTelemetryBean.getAttributeMatchRatio()); + } return metrics; } @@ -547,11 +553,14 @@ private void getMatchingAttributes() throws IOException { this.failingAttributes.clear(); int metricsCount = 0; + int beansWithAttributeMatch = 0; + if (!action.equals(AppConfig.ACTION_COLLECT)) { reporter.displayInstanceName(this); } for (ObjectName beanName : this.beans) { + boolean attributeMatched = false; if (limitReached) { log.debug("Limit reached"); if (action.equals(AppConfig.ACTION_COLLECT)) { @@ -702,7 +711,17 @@ private void getMatchingAttributes() throws IOException { || action.equals(AppConfig.ACTION_LIST_NOT_MATCHING))) { reporter.displayNonMatchingAttributeName(jmxAttribute); } + if (jmxAttribute.getMatchingConf() != null) { + attributeMatched = true; + } } + if (attributeMatched) { + beansWithAttributeMatch += 1; + } + } + if (instanceTelemetryBean != null) { + instanceTelemetryBean.setAttributeMatchRatio((double) + beansWithAttributeMatch / beans.size()); } log.info("Found {} matching attributes", matchingAttributes.size()); } @@ -719,7 +738,7 @@ public List getBeansScopes() { * Query and refresh the instance's list of beans. Limit the query scope when possible on * certain actions, and fallback if necessary. */ - private void refreshBeansList() throws IOException { + private void refreshBeansList() throws IOException, MalformedObjectNameException { this.beans = new HashSet(); String action = appConfig.getAction(); boolean limitQueryScopes = @@ -733,14 +752,24 @@ private void refreshBeansList() throws IOException { ObjectName name = new ObjectName(scope); this.beans.addAll(connection.queryNames(name)); } - } catch (Exception e) { + if (instanceTelemetryBean != null) { + instanceTelemetryBean.setDomainsQueried(beanScopes.size()); + } + } catch (MalformedObjectNameException e) { + log.error("Unable to create ObjectName", e); + } catch (IOException e) { log.error( - "Unable to compute a common bean scope, querying all beans as a fallback", - e); + "Unable to query mbean server", e); } } - this.beans = (this.beans.isEmpty()) ? connection.queryNames(null) : this.beans; + if (this.beans.isEmpty()) { + this.beans = connection.queryNames(null); + if (instanceTelemetryBean != null) { + int wildcardQueryCount = instanceTelemetryBean.getWildcardQueryCount(); + instanceTelemetryBean.setWildcardQueryCount(wildcardQueryCount + 1); + } + } this.lastRefreshTime = System.currentTimeMillis(); } diff --git a/src/main/java/org/datadog/jmxfetch/Status.java b/src/main/java/org/datadog/jmxfetch/Status.java index e6ecfc494..637ada311 100644 --- a/src/main/java/org/datadog/jmxfetch/Status.java +++ b/src/main/java/org/datadog/jmxfetch/Status.java @@ -127,6 +127,11 @@ private void addStats( instStats.put("instance_attribute_count", instanceTelemetryBean.getTopLevelAttributeCount()); instStats.put("instance_metric_count", instanceTelemetryBean.getMetricCount()); + instStats.put("instance_domains_queried", instanceTelemetryBean.getDomainsQueried()); + instStats.put("instance_wildcard_query_count", + instanceTelemetryBean.getWildcardQueryCount()); + instStats.put("instance_attribute_match_ratio", + instanceTelemetryBean.getAttributeMatchRatio()); } instStats.put("message", message); instStats.put("status", status); diff --git a/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java b/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java index b216b54f5..7bc04b51e 100644 --- a/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java +++ b/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java @@ -7,12 +7,18 @@ public class InstanceTelemetry implements InstanceTelemetryMBean { private int beansFetched; private int topLevelAttributeCount; private int metricCount; + private int domainsQueried; + private int wildcardQueryCount; + private double attributeMatchRatio; /** Jmxfetch telemetry bean constructor. */ public InstanceTelemetry() { beansFetched = 0; topLevelAttributeCount = 0; metricCount = 0; + domainsQueried = 0; + wildcardQueryCount = 0; + attributeMatchRatio = 0.0; } public int getBeansFetched() { @@ -27,6 +33,17 @@ public int getMetricCount() { return metricCount; } + public int getDomainsQueried() { + return domainsQueried; + } + + public int getWildcardQueryCount() { + return wildcardQueryCount; + } + + public double getAttributeMatchRatio() { + return attributeMatchRatio; + } public void setBeansFetched(int count) { beansFetched = count; @@ -40,5 +57,16 @@ public void setMetricCount(int count) { metricCount = count; } + public void setDomainsQueried(int count) { + domainsQueried = count; + } + + public void setWildcardQueryCount(int count) { + wildcardQueryCount = count; + } + + public void setAttributeMatchRatio(double ratio) { + attributeMatchRatio = ratio; + } } diff --git a/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetryMBean.java b/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetryMBean.java index b80d126c0..3f5b028c4 100644 --- a/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetryMBean.java +++ b/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetryMBean.java @@ -8,4 +8,6 @@ public interface InstanceTelemetryMBean { int getMetricCount(); + int getDomainsQueried(); + } diff --git a/src/test/java/org/datadog/jmxfetch/StatusTest.java b/src/test/java/org/datadog/jmxfetch/StatusTest.java index 471aac120..fe6ae0456 100644 --- a/src/test/java/org/datadog/jmxfetch/StatusTest.java +++ b/src/test/java/org/datadog/jmxfetch/StatusTest.java @@ -33,10 +33,16 @@ public void TestStatus() throws IOException { int fakeBeansFetched = 11; int fakeMetricCount = 29; int fakeAttributeCount = 55; + int fakeDomainsQueried = 3; + int fakeWildcardQueryCount = 9; + double fakeBeansAttributeMatchRatio = .4; instance.setBeansFetched(fakeBeansFetched); instance.setMetricCount(fakeMetricCount); instance.setTopLevelAttributeCount(fakeAttributeCount); + instance.setDomainsQueried(fakeDomainsQueried); + instance.setWildcardQueryCount(fakeWildcardQueryCount); + instance.setAttributeMatchRatio(fakeBeansAttributeMatchRatio); status.addInstanceStats("fake_check", "fake_instance", 10, 3, "fake_message", Status.STATUS_OK, instance); status.flush(); @@ -55,6 +61,9 @@ public void TestStatus() throws IOException { assertEquals(fakeBeansFetched, stats.get("instance_bean_count")); assertEquals(fakeAttributeCount, stats.get("instance_attribute_count")); assertEquals(fakeMetricCount, stats.get("instance_metric_count")); + assertEquals(fakeDomainsQueried, stats.get("instance_domains_queried")); + assertEquals(fakeWildcardQueryCount, stats.get("instance_wildcard_query_count")); + assertEquals(fakeBeansAttributeMatchRatio, stats.get("instance_attribute_match_ratio")); assertEquals("fake_message", stats.get("message")); assertEquals(Status.STATUS_OK, stats.get("status")); }