From 26d264d604b1e0970cf2adb914697a19d912ac64 Mon Sep 17 00:00:00 2001 From: Carlos Roman Date: Fri, 31 May 2024 17:05:24 +0100 Subject: [PATCH] Will this break? --- src/main/java/org/datadog/jmxfetch/App.java | 14 +++++++++++--- src/main/java/org/datadog/jmxfetch/Instance.java | 4 +++- src/test/java/org/datadog/jmxfetch/TestApp.java | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 8c78e8ac3..4193726dc 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -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() { @@ -168,7 +171,7 @@ private void initTelemetryBean() { | MBeanRegistrationException | NotCompliantMBeanException e) { log.warn("Could not register bean named '{}' for instance: ", - appTelemetryBeanName.toString(), e); + appTelemetryBeanName, e); } this.appTelemetry = bean; @@ -176,6 +179,11 @@ private void initTelemetryBean() { } 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) { @@ -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); } } diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 9da8de750..89616b866 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -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) diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 5fec8d252..a46b0c263 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -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(), @@ -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(),