diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index ded593d60..1cc3b91f9 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -509,13 +509,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() + + " wildcard domain query count = " + + instanceTelemetryBean.getWildcardDomainQueryCount() + + " bean match ratio = " + instanceTelemetryBean.getBeanMatchRatio()); + } return metrics; } @@ -547,11 +552,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 +710,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.setBeanMatchRatio((double) + beansWithAttributeMatch / beans.size()); } log.info("Found {} matching attributes", matchingAttributes.size()); } @@ -733,14 +751,21 @@ private void refreshBeansList() throws IOException { ObjectName name = new ObjectName(scope); this.beans.addAll(connection.queryNames(name)); } - } catch (Exception e) { + } 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.getWildcardDomainQueryCount(); + instanceTelemetryBean.setWildcardDomainQueryCount(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..6a96a9031 100644 --- a/src/main/java/org/datadog/jmxfetch/Status.java +++ b/src/main/java/org/datadog/jmxfetch/Status.java @@ -127,6 +127,10 @@ private void addStats( instStats.put("instance_attribute_count", instanceTelemetryBean.getTopLevelAttributeCount()); instStats.put("instance_metric_count", instanceTelemetryBean.getMetricCount()); + instStats.put("instance_wildcard_domain_query_count", + instanceTelemetryBean.getWildcardDomainQueryCount()); + instStats.put("instance_bean_match_ratio", + instanceTelemetryBean.getBeanMatchRatio()); } 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..3cbb938fd 100644 --- a/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java +++ b/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java @@ -7,12 +7,16 @@ public class InstanceTelemetry implements InstanceTelemetryMBean { private int beansFetched; private int topLevelAttributeCount; private int metricCount; + private int wildcardDomainQueryCount; + private double beanMatchRatio; /** Jmxfetch telemetry bean constructor. */ public InstanceTelemetry() { beansFetched = 0; topLevelAttributeCount = 0; metricCount = 0; + wildcardDomainQueryCount = 0; + beanMatchRatio = 0.0; } public int getBeansFetched() { @@ -27,6 +31,13 @@ public int getMetricCount() { return metricCount; } + public int getWildcardDomainQueryCount() { + return wildcardDomainQueryCount; + } + + public double getBeanMatchRatio() { + return beanMatchRatio; + } public void setBeansFetched(int count) { beansFetched = count; @@ -40,5 +51,12 @@ public void setMetricCount(int count) { metricCount = count; } + public void setWildcardDomainQueryCount(int count) { + wildcardDomainQueryCount = count; + } + + public void setBeanMatchRatio(double ratio) { + beanMatchRatio = 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..4c98b5200 100644 --- a/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetryMBean.java +++ b/src/main/java/org/datadog/jmxfetch/util/InstanceTelemetryMBean.java @@ -8,4 +8,8 @@ public interface InstanceTelemetryMBean { int getMetricCount(); + int getWildcardDomainQueryCount(); + + double getBeanMatchRatio(); + } diff --git a/src/test/java/org/datadog/jmxfetch/StatusTest.java b/src/test/java/org/datadog/jmxfetch/StatusTest.java index 471aac120..f8a8b06ec 100644 --- a/src/test/java/org/datadog/jmxfetch/StatusTest.java +++ b/src/test/java/org/datadog/jmxfetch/StatusTest.java @@ -33,10 +33,14 @@ public void TestStatus() throws IOException { int fakeBeansFetched = 11; int fakeMetricCount = 29; int fakeAttributeCount = 55; + int fakeWildcardDomainQueryCount = 9; + double fakeBeanMatchRatio = .4; instance.setBeansFetched(fakeBeansFetched); instance.setMetricCount(fakeMetricCount); instance.setTopLevelAttributeCount(fakeAttributeCount); + instance.setWildcardDomainQueryCount(fakeWildcardDomainQueryCount); + instance.setBeanMatchRatio(fakeBeanMatchRatio); status.addInstanceStats("fake_check", "fake_instance", 10, 3, "fake_message", Status.STATUS_OK, instance); status.flush(); @@ -55,6 +59,8 @@ 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(fakeWildcardDomainQueryCount, stats.get("instance_wildcard_domain_query_count")); + assertEquals(fakeBeanMatchRatio, stats.get("instance_bean_match_ratio")); assertEquals("fake_message", stats.get("message")); assertEquals(Status.STATUS_OK, stats.get("status")); }