From 72a392327a933b45fa59ec6ef54ceba09991781c Mon Sep 17 00:00:00 2001 From: Caleb Metz <135133572+cmetz100@users.noreply.github.com> Date: Thu, 27 Jul 2023 15:14:54 -0400 Subject: [PATCH] Update management agent logic and comments for java 7 vs 8 vs 9 (#457) * restructured loadjmxagent * added version log msg * reverted to maven target 1.7 * updated comments * updated formatting * updated format * updated more formatting --- .../datadog/jmxfetch/AttachApiConnection.java | 41 ++++++++++--------- .../datadog/jmxfetch/ConnectionFactory.java | 2 + 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java index 6b975ccb9..db05d4c94 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -59,31 +59,32 @@ private String getJmxUrlForProcessRegex(String processRegex) } // management-agent.jar has been removed in java 8+ - // Once JMXFetch drops java7 support, this should be simplified to simply invoke - // vm.startLocalManagementAgent + // Once JMXFetch drops java 7 support, this should be simplified to simply invoke + // vm.startLocalManagementAgent which is accessible in java 8 if tools.jar is added + // to the classpath and java 9+ by default // ref https://bugs.openjdk.org/browse/JDK-8179063 private void loadJmxAgent(com.sun.tools.attach.VirtualMachine vm) throws IOException { - String agent = - vm.getSystemProperties().getProperty("java.home") - + File.separator - + "lib" - + File.separator - + "management-agent.jar"; try { - vm.loadAgent(agent); - } catch (Exception e) { - log.warn("Error initializing JMX agent from management-agent.jar", e); - + Method method = com.sun.tools.attach.VirtualMachine + .class.getMethod("startLocalManagementAgent"); + log.info("Found startLocalManagementAgent API, attempting to use it."); + method.invoke(vm); + } catch (NoSuchMethodException noMethodE) { + log.warn("startLocalManagementAgent method not found, must be on java 7", noMethodE); + String agent = vm.getSystemProperties().getProperty("java.home") + + File.separator + + "lib" + + File.separator + + "management-agent.jar"; try { - Method method = com.sun.tools.attach.VirtualMachine - .class.getMethod("startLocalManagementAgent"); - log.info("Found startLocalManagementAgent API, attempting to use it."); - method.invoke(vm); - } catch (NoSuchMethodException noMethodE) { - log.warn("startLocalManagementAgent method not found, must be on java7", noMethodE); - } catch (Exception reflectionE) { - log.warn("Error invoking the startLocalManagementAgent method", reflectionE); + vm.loadAgent(agent); + } catch (Exception e) { + log.warn("Error initializing JMX agent from management-agent.jar", e); } + } catch (Exception reflectionE) { + log.warn("Error invoking the startLocalManagementAgent method", reflectionE); } + + } } diff --git a/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java b/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java index 61b7e32db..8855e42a6 100644 --- a/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java +++ b/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java @@ -24,6 +24,8 @@ public static Connection createConnection(Map connectionParams) if (connectionParams.get(PROCESS_NAME_REGEX) != null) { try { + // AttachNotSupportedException is accessible in java 7 and 8 through tools.jar + // and java 9+ by default Class.forName("com.sun.tools.attach.AttachNotSupportedException"); } catch (ClassNotFoundException e) { throw new IOException(