diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 65867e20..06d86006 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -160,6 +160,15 @@ public Instance( Object maxReturnedMetrics = this.instanceMap.get("max_returned_metrics"); if (maxReturnedMetrics == null) { this.maxReturnedMetrics = MAX_RETURNED_METRICS; + } else if (maxReturnedMetrics instanceof String) { + try { + this.maxReturnedMetrics = Integer.parseInt((String) maxReturnedMetrics); + } catch (NumberFormatException e) { + log.warn( + "Cannot convert max_returned_metrics to integer in your instance configuration." + + " Defaulting to {}.", MAX_RETURNED_METRICS); + this.maxReturnedMetrics = MAX_RETURNED_METRICS; + } } else { this.maxReturnedMetrics = (Integer) maxReturnedMetrics; } diff --git a/src/test/java/org/datadog/jmxfetch/TestInstance.java b/src/test/java/org/datadog/jmxfetch/TestInstance.java index 4fa8adf5..3a1d4b1a 100644 --- a/src/test/java/org/datadog/jmxfetch/TestInstance.java +++ b/src/test/java/org/datadog/jmxfetch/TestInstance.java @@ -97,6 +97,31 @@ public void testBaselineDefaultHostname() throws Exception { } } + @Test + public void testParsableMaxReturnedMetrics() throws Exception { + SimpleTestJavaApp testApp = new SimpleTestJavaApp(); + // Populate testApp with a lot of metrics (>350) ! + testApp.populateHashMap(400); + // Exposing a few metrics through JMX + registerMBean(testApp, "org.datadog.jmxfetch.test:type=ParsableMaxReturnedMetricsTest"); + initApplication("jmx_parsable_max_returned_metrics_string.yaml"); + run(); + + List> metrics = getMetrics(); + assertEquals(429, metrics.size()); + } + + @Test + public void testNonParsableMaxReturnedMetrics() throws Exception { + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=NonParsableMaxReturnedMetricsTest"); + initApplication("jmx_non_parsable_max_returned_metrics_string.yaml"); + run(); + + // Despite non parsable max_returned_metrics, metrics should still be collected. + List> metrics = getMetrics(); + assertEquals(29, metrics.size()); + } + // assertServiceTags is used by testServiceTagGlobal and testServiceTagInstanceOverride private void assertServiceTag(List tagList, List services) throws Exception { for (String service: services) { diff --git a/src/test/resources/jmx_non_parsable_max_returned_metrics_string.yaml b/src/test/resources/jmx_non_parsable_max_returned_metrics_string.yaml new file mode 100644 index 00000000..4daf69e8 --- /dev/null +++ b/src/test/resources/jmx_non_parsable_max_returned_metrics_string.yaml @@ -0,0 +1,9 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + max_returned_metrics: "foo" + conf: + - include: + domain: org.datadog.jmxfetch.test diff --git a/src/test/resources/jmx_parsable_max_returned_metrics_string.yaml b/src/test/resources/jmx_parsable_max_returned_metrics_string.yaml new file mode 100644 index 00000000..0502e998 --- /dev/null +++ b/src/test/resources/jmx_parsable_max_returned_metrics_string.yaml @@ -0,0 +1,10 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + max_returned_metrics: "500" + conf: + - include: + domain: org.datadog.jmxfetch.test +