Skip to content

Commit

Permalink
Merge pull request #477 from DataDog/display-internal-metrics-on-agen…
Browse files Browse the repository at this point in the history
…t-status

Display internal metrics on status
  • Loading branch information
rayz authored Sep 7, 2023
2 parents ab0ee2e + 81cf78f commit d002d62
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/org/datadog/jmxfetch/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -73,15 +75,17 @@ public void addInstanceStats(
int metricCount,
int serviceCheckCount,
String message,
String status) {
String status,
InstanceTelemetry instanceTelemetryBean) {
addStats(
checkName,
instance,
metricCount,
serviceCheckCount,
message,
status,
INITIALIZED_CHECKS);
INITIALIZED_CHECKS,
instanceTelemetryBean);
}

public void addErrorStats(int errors) {
Expand All @@ -96,7 +100,8 @@ private void addStats(
int serviceCheckCount,
String message,
String status,
String key) {
String key,
InstanceTelemetry instanceTelemetryBean) {
List<Map<String, Object>> checkStats;
Map<String, Object> initializedChecks;
initializedChecks = (Map<String, Object>) this.instanceStats.get(key);
Expand All @@ -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() {
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/datadog/jmxfetch/StatusTest.java
Original file line number Diff line number Diff line change
@@ -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<Map<String, Object>> fakeCheck = (List<Map<String, Object>>) initializedChecks.get("fake_check");
Map<String, Object> 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"));
}
}

0 comments on commit d002d62

Please sign in to comment.