Skip to content

Commit

Permalink
fixes tests?
Browse files Browse the repository at this point in the history
  • Loading branch information
scottopell committed May 31, 2024
1 parent 7686c87 commit 0d6a8cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ public App(final AppConfig appConfig) {
}
this.configs = getConfigs(this.appConfig);

// if (this.appConfig.getJmxfetchTelemetry()) {
this.appTelemetry = new AppTelemetry();

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

private ObjectName getAppTelemetryBeanName() {
Expand All @@ -156,9 +158,8 @@ private ObjectName getAppTelemetryBeanName() {
return appTelemetryBeanName;
}

private void initTelemetryBean() {
private void registerTelemetryBean(AppTelemetry bean) {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
AppTelemetry bean = new AppTelemetry();
ObjectName appTelemetryBeanName = getAppTelemetryBeanName();
if (appTelemetryBeanName == null) {
return;
Expand All @@ -174,7 +175,6 @@ private void initTelemetryBean() {
appTelemetryBeanName.toString(), e);
}

this.appTelemetry = bean;
return;
}

Expand Down Expand Up @@ -523,7 +523,9 @@ void start() {
}

void stop() {
this.teardownTelemetry();
if (this.appConfig.getJmxfetchTelemetry()) {
this.teardownTelemetry();
}
this.collectionProcessor.stop();
this.recoveryProcessor.stop();
}
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,20 @@ public Instance(
log.info("collect_default_jvm_metrics is false - not collecting default JVM metrics");
}

instanceTelemetryBean = createInstanceTelemetryBean();
instanceTelemetryBean = new InstanceTelemetry();
if (appConfig.getJmxfetchTelemetry()) {
registerTelemetryBean(instanceTelemetryBean);
}
}

private ObjectName getObjName(String domain,String instance)
throws MalformedObjectNameException {
return new ObjectName(domain + ":target_instance=" + ObjectName.quote(instance));
}

private InstanceTelemetry createInstanceTelemetryBean() {
private InstanceTelemetry registerTelemetryBean(InstanceTelemetry bean) {
log.debug("Registering jmx bean for instance: " + this.getCheckName());
mbs = ManagementFactory.getPlatformMBeanServer();
InstanceTelemetry bean = new InstanceTelemetry();
log.debug("Created jmx bean for instance: " + this.getCheckName());

try {
instanceTelemetryBeanName = getObjName(appConfig.getJmxfetchTelemetryDomain(),
Expand Down Expand Up @@ -516,8 +518,8 @@ public List<Metric> getMetrics() throws IOException {
+ " With beans fetched = " + instanceTelemetryBean.getBeansFetched()
+ " top attributes = " + instanceTelemetryBean.getTopLevelAttributeCount()
+ " metrics = " + instanceTelemetryBean.getMetricCount()
+ " wildcard domain query count = "
+ instanceTelemetryBean.getWildcardDomainQueryCount()
+ " wildcard domain query count = "
+ instanceTelemetryBean.getWildcardDomainQueryCount()
+ " bean match ratio = " + instanceTelemetryBean.getBeanMatchRatio());
}
return metrics;
Expand Down Expand Up @@ -710,15 +712,15 @@ private void getMatchingAttributes() throws IOException {
reporter.displayNonMatchingAttributeName(jmxAttribute);
}
if (jmxAttribute.getMatchingConf() != null) {
attributeMatched = true;
attributeMatched = true;
}
}
if (attributeMatched) {
beansWithAttributeMatch += 1;
}
}
if (instanceTelemetryBean != null) {
instanceTelemetryBean.setBeanMatchRatio((double)
instanceTelemetryBean.setBeanMatchRatio((double)
beansWithAttributeMatch / beans.size());
}
log.info("Found {} matching attributes", matchingAttributes.size());
Expand Down Expand Up @@ -837,6 +839,10 @@ public boolean isLimitReached() {
}

private void cleanupTelemetryBean() {
if (!appConfig.getJmxfetchTelemetry()) {
// If telemetry is not enabled, no need to unregister the bean
return;
}
try {
mbs.unregisterMBean(instanceTelemetryBeanName);
log.debug("Successfully unregistered bean for instance: " + this.getCheckName());
Expand Down
5 changes: 0 additions & 5 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class TestApp extends TestCommon {
@Test
public void testBeanRegexTags() throws Exception {
log.info("********************\nSTARTING\n********************");
// When we enable JMXFetch telemetry
when(appConfig.getJmxfetchTelemetry()).thenReturn(true);


log.info("********************\nregisterMBean\n********************");
Expand Down Expand Up @@ -64,9 +62,6 @@ 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

0 comments on commit 0d6a8cf

Please sign in to comment.