Skip to content

Commit

Permalink
Will this break?
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosroman committed May 31, 2024
1 parent fc57775 commit d1c7275
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public App(final AppConfig appConfig) {
}
this.configs = getConfigs(this.appConfig);

this.initTelemetryBean();
if (this.appConfig.getJmxfetchTelemetry()) {
log.info("Enabling JMX Fetch Telemetry");
this.initTelemetryBean();
}
}

private ObjectName getAppTelemetryBeanName() {
Expand Down Expand Up @@ -168,14 +171,19 @@ private void initTelemetryBean() {
| MBeanRegistrationException
| NotCompliantMBeanException e) {
log.warn("Could not register bean named '{}' for instance: ",
appTelemetryBeanName.toString(), e);
appTelemetryBeanName, e);
}

this.appTelemetry = bean;
return;
}

private void teardownTelemetry() {
if (!this.appConfig.getJmxfetchTelemetry()) {
log.debug("Skipping teardown telemetry as not enabled");
return;
}

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName appTelemetryBeanName = getAppTelemetryBeanName();
if (appTelemetryBeanName == null) {
Expand All @@ -187,7 +195,7 @@ private void teardownTelemetry() {
log.debug("Succesfully unregistered app telemetry bean");
} catch (MBeanRegistrationException | InstanceNotFoundException e) {
log.warn("Could not unregister bean named '{}' for instance: ",
appTelemetryBeanName.toString(), e);
appTelemetryBeanName, e);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ public Instance(
log.info("collect_default_jvm_metrics is false - not collecting default JVM metrics");
}

instanceTelemetryBean = createInstanceTelemetryBean();
if (this.appConfig.getJmxfetchTelemetry()) {
this.instanceTelemetryBean = createInstanceTelemetryBean();
}
}

private ObjectName getObjName(String domain,String instance)
Expand Down Expand Up @@ -837,11 +839,14 @@ public boolean isLimitReached() {
}

private void cleanupTelemetryBean() {
if (this.instanceTelemetryBean == null) {
return;
}
try {
mbs.unregisterMBean(instanceTelemetryBeanName);
log.debug("Successfully unregistered bean for instance: " + this.getCheckName());
log.debug("Successfully unregistered bean for instance: {}", this.getCheckName());
} catch (MBeanRegistrationException | InstanceNotFoundException e) {
log.debug("Unable to unregister bean for instance: " + this.getCheckName());
log.debug("Unable to unregister bean for instance: {}", this.getCheckName());
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class TestApp extends TestCommon {
/** Tag metrics with MBean parameters based on user supplied regex */
@Test
public void testBeanRegexTags() throws Exception {
// When we enable JMXFetch telemetry
when(appConfig.getJmxfetchTelemetry()).thenReturn(true);

// We expose a few metrics through JMX
registerMBean(
new SimpleTestJavaApp(),
Expand Down Expand Up @@ -53,6 +56,9 @@ public void testBeanRegexTags() throws Exception {
/** Tag metrics with MBeans parameters. */
@Test
public void testBeanTags() throws Exception {
// When we enable JMXFetch telemetry
when(appConfig.getJmxfetchTelemetry()).thenReturn(true);

// We expose a few metrics through JMX
registerMBean(
new SimpleTestJavaApp(),
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class TestCommon {
AppConfig appConfig = spy(AppConfig.builder().build());
App app;
MBeanServer mbs;
List<ObjectName> objectNames = new ArrayList<ObjectName>();
List<ObjectName> objectNames = new ArrayList<>();
List<Map<String, Object>> metrics;
List<Map<String, Object>> serviceChecks;

Expand All @@ -89,8 +89,8 @@ protected void registerMBean(Object application, String objectStringName)
NotCompliantMBeanException, MalformedObjectNameException {
mbs = (mbs == null) ? ManagementFactory.getPlatformMBeanServer() : mbs;
ObjectName objectName = new ObjectName(objectStringName);
objectNames.add(objectName);
mbs.registerMBean(application, objectName);
objectNames.add(objectName);
}

/**
Expand All @@ -104,6 +104,7 @@ public void unregisterMBeans() throws MBeanRegistrationException, InstanceNotFou
for (ObjectName objectName : objectNames) {
mbs.unregisterMBean(objectName);
}
objectNames.clear();
}
}

Expand Down

0 comments on commit d1c7275

Please sign in to comment.