diff --git a/CHANGELOG.md b/CHANGELOG.md index e0fcb92de..9c7889205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Changelog * [FEATURE] Adds a configurable jmxfetch telemetry check to improve jmxfetch observability [#467][] * [FEATURE] Added an option to enable removal of extra quotation marks during tag extraction from Java management beans' parameters/attributes [#469][] +* [FEATURE] Updated status bean to report JMX Telemetry to Agent status [#477][] # 0.47.10 / 2023-08-10 @@ -753,6 +754,7 @@ Changelog [#457]: https://github.com/DataDog/jmxfetch/issues/457 [#449]: https://github.com/DataDog/jmxfetch/issues/449 [#469]: https://github.com/DataDog/jmxfetch/issues/469 +[#477]: https://github.com/DataDog/jmxfetch/issues/477 [@alz]: https://github.com/alz [@aoking]: https://github.com/aoking [@arrawatia]: https://github.com/arrawatia diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 680003c46..00ab71d4b 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -755,7 +755,7 @@ private void reportStatus( stats.addInstanceStats( checkName, instance.getName(), metricCount, reporter.getServiceCheckCount(checkName), - message, status); + message, status, instance.getInstanceTelemetryBean()); if (reporter.getHandler() != null) { stats.addErrorStats(reporter.getHandler().getErrors()); } diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 71a315024..a2c5cbce4 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -827,6 +827,10 @@ public int getMaxNumberOfMetrics() { return this.maxReturnedMetrics; } + public InstanceTelemetry getInstanceTelemetryBean() { + return this.instanceTelemetryBean; + } + /** Returns whether or not the instance has reached the maximum bean collection limit. */ public boolean isLimitReached() { return this.limitReached; diff --git a/src/main/java/org/datadog/jmxfetch/Status.java b/src/main/java/org/datadog/jmxfetch/Status.java index a2d73952e..e6ecfc494 100644 --- a/src/main/java/org/datadog/jmxfetch/Status.java +++ b/src/main/java/org/datadog/jmxfetch/Status.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.jr.ob.JSON; import lombok.extern.slf4j.Slf4j; + +import org.datadog.jmxfetch.util.InstanceTelemetry; import org.datadog.jmxfetch.util.MetadataHelper; import org.yaml.snakeyaml.Yaml; @@ -73,7 +75,8 @@ public void addInstanceStats( int metricCount, int serviceCheckCount, String message, - String status) { + String status, + InstanceTelemetry instanceTelemetryBean) { addStats( checkName, instance, @@ -81,7 +84,8 @@ public void addInstanceStats( serviceCheckCount, message, status, - INITIALIZED_CHECKS); + INITIALIZED_CHECKS, + instanceTelemetryBean); } public void addErrorStats(int errors) { @@ -96,7 +100,8 @@ private void addStats( int serviceCheckCount, String message, String status, - String key) { + String key, + InstanceTelemetry instanceTelemetryBean) { List> checkStats; Map initializedChecks; initializedChecks = (Map) this.instanceStats.get(key); @@ -117,15 +122,24 @@ private void addStats( if (serviceCheckCount != -1) { instStats.put("service_check_count", serviceCheckCount); } + if (instanceTelemetryBean != null) { + instStats.put("instance_bean_count", instanceTelemetryBean.getBeansFetched()); + instStats.put("instance_attribute_count", + instanceTelemetryBean.getTopLevelAttributeCount()); + instStats.put("instance_metric_count", instanceTelemetryBean.getMetricCount()); + } instStats.put("message", message); instStats.put("status", status); + // NOTE: jmxfetch template must be updated for any new keys in order for them + // to show up in the datadog-agent status + // https://github.com/DataDog/datadog-agent/blob/main/pkg/status/templates/jmxfetch.tmpl checkStats.add(instStats); initializedChecks.put(checkName, checkStats); this.instanceStats.put(key, initializedChecks); } public void addInitFailedCheck(String checkName, String message, String status) { - addStats(checkName, null, -1, -1, message, status, FAILED_CHECKS); + addStats(checkName, null, -1, -1, message, status, FAILED_CHECKS, null); } private String generateYaml() { diff --git a/src/test/java/org/datadog/jmxfetch/StatusTest.java b/src/test/java/org/datadog/jmxfetch/StatusTest.java new file mode 100644 index 000000000..471aac120 --- /dev/null +++ b/src/test/java/org/datadog/jmxfetch/StatusTest.java @@ -0,0 +1,61 @@ +package org.datadog.jmxfetch; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.datadog.jmxfetch.util.InstanceTelemetry; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.yaml.snakeyaml.Yaml; + +public class StatusTest { + + @Rule + public TemporaryFolder folder= new TemporaryFolder(); + + @Test + public void TestStatus() throws IOException { + + File tempFile= folder.newFile("tempFile.txt"); + String tempFilePath = tempFile.getAbsolutePath(); + + final Status status = new Status(tempFilePath); + InstanceTelemetry instance = new InstanceTelemetry(); + + int fakeBeansFetched = 11; + int fakeMetricCount = 29; + int fakeAttributeCount = 55; + + instance.setBeansFetched(fakeBeansFetched); + instance.setMetricCount(fakeMetricCount); + instance.setTopLevelAttributeCount(fakeAttributeCount); + + status.addInstanceStats("fake_check", "fake_instance", 10, 3, "fake_message", Status.STATUS_OK, instance); + status.flush(); + + Yaml yaml = new Yaml(); + InputStream inputStream = new FileInputStream(tempFilePath); + + HashMap yamlMap = yaml.load(inputStream); + HashMap checks = (HashMap) yamlMap.get("checks"); + HashMap initializedChecks = (HashMap) checks.get("initialized_checks"); + List> fakeCheck = (List>) initializedChecks.get("fake_check"); + Map stats = fakeCheck.get(0); + assertEquals("fake_instance", stats.get("instance_name")); + assertEquals(10, stats.get("metric_count")); + assertEquals(3, stats.get("service_check_count")); + assertEquals(fakeBeansFetched, stats.get("instance_bean_count")); + assertEquals(fakeAttributeCount, stats.get("instance_attribute_count")); + assertEquals(fakeMetricCount, stats.get("instance_metric_count")); + assertEquals("fake_message", stats.get("message")); + assertEquals(Status.STATUS_OK, stats.get("status")); + } +} \ No newline at end of file