Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display internal metrics on status #477

Merged
merged 10 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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][]
rayz marked this conversation as resolved.
Show resolved Hide resolved

# 0.47.10 / 2023-08-10

Expand Down Expand Up @@ -753,6 +754,7 @@ Changelog
[#457]: https:/DataDog/jmxfetch/issues/457
[#449]: https:/DataDog/jmxfetch/issues/449
[#469]: https:/DataDog/jmxfetch/issues/469
[#477]: https:/DataDog/jmxfetch/issues/477
[@alz]: https:/alz
[@aoking]: https:/aoking
[@arrawatia]: https:/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:/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"));
}
}
Loading