From 34f18af5f1f967a7d361fa9372c6d2a2d6eb8663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Bavelier?= Date: Fri, 23 Aug 2024 10:59:06 +0200 Subject: [PATCH 1/2] parseInt if string and fallback to default if unable to parse & tests --- .../java/org/datadog/jmxfetch/Instance.java | 7 ++++++ .../org/datadog/jmxfetch/TestInstance.java | 25 +++++++++++++++++++ ..._parsable_max_returned_metrics_string.yaml | 9 +++++++ ..._parsable_max_returned_metrics_string.yaml | 9 +++++++ 4 files changed, 50 insertions(+) create mode 100644 src/test/resources/jmx_non_parsable_max_returned_metrics_string.yaml create mode 100644 src/test/resources/jmx_parsable_max_returned_metrics_string.yaml diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 65867e201..c17bb67dc 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -160,6 +160,13 @@ 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 4fa8adf55..3a1d4b1a3 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 000000000..0db155a88 --- /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 \ No newline at end of file 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 000000000..a6ff4aeb7 --- /dev/null +++ b/src/test/resources/jmx_parsable_max_returned_metrics_string.yaml @@ -0,0 +1,9 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + max_returned_metrics: "500" + conf: + - include: + domain: org.datadog.jmxfetch.test \ No newline at end of file From 3ce2122dd8b8e2b3f8f81fc9fcbb1f3591fa1e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Bavelier?= Date: Fri, 23 Aug 2024 11:33:16 +0200 Subject: [PATCH 2/2] break log warn for lint --- src/main/java/org/datadog/jmxfetch/Instance.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index c17bb67dc..2290e20ee 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -164,7 +164,9 @@ public Instance( 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); + log.warn( + "Cannot convert max_returned_metrics to integer in your instance configuration. Defaulting to '{}'.", + MAX_RETURNED_METRICS); this.maxReturnedMetrics = MAX_RETURNED_METRICS; } } else {