From 993080b995f5c9050fc15df76345e25cf0dbee1e Mon Sep 17 00:00:00 2001 From: jplumb Date: Wed, 30 Sep 2020 16:28:47 -0700 Subject: [PATCH] use setPredefinedConfiguration --- .../newrelic/jfr/daemon/JFRController.java | 7 ++- .../newrelic/jfr/daemon/JFRJMXRecorder.java | 44 +++++++++---------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRController.java b/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRController.java index 6a154962..d08f9f44 100644 --- a/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRController.java +++ b/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRController.java @@ -13,6 +13,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import javax.management.InstanceNotFoundException; +import javax.management.JMException; import javax.management.MBeanException; import javax.management.MalformedObjectNameException; import javax.management.ReflectionException; @@ -58,7 +59,7 @@ void setup() { } } - void loop(Duration harvestInterval) throws IOException { + void loop(Duration harvestInterval) throws IOException, JMException { while (!shutdown) { try { TimeUnit.MILLISECONDS.sleep(harvestInterval.toMillis()); @@ -90,9 +91,7 @@ void loop(Duration harvestInterval) throws IOException { executorService.shutdown(); } - private void restartRecording() - throws IOException, MalformedObjectNameException, ReflectionException, - InstanceNotFoundException, MBeanException, OpenDataException { + private void restartRecording() throws IOException, JMException { recorder = JFRJMXRecorder.connectWithBackOff(config); recorder.startRecordingWithBackOff(); } diff --git a/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRJMXRecorder.java b/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRJMXRecorder.java index 7f029bf5..22265cfe 100644 --- a/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRJMXRecorder.java +++ b/jfr-daemon/src/main/java/com/newrelic/jfr/daemon/JFRJMXRecorder.java @@ -12,7 +12,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.text.ParseException; import java.time.Duration; import java.util.HashMap; import java.util.List; @@ -21,7 +20,6 @@ import javax.management.*; import javax.management.openmbean.*; import javax.management.remote.JMXServiceURL; -import jdk.jfr.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,15 +61,11 @@ private static JFRJMXRecorder connectWithBackOff(DaemonConfig config, int backof } } - public void startRecordingWithBackOff() - throws MalformedObjectNameException, ReflectionException, IOException, - InstanceNotFoundException, MBeanException, OpenDataException { + public void startRecordingWithBackOff() throws JMException, IOException { startRecordingWithBackOff(0); } - public void startRecordingWithBackOff(int backoffIndex) - throws MalformedObjectNameException, ReflectionException, IOException, - InstanceNotFoundException, MBeanException, OpenDataException { + public void startRecordingWithBackOff(int backoffIndex) throws JMException, IOException { try { startRecording(); } catch (Exception e) { @@ -113,28 +107,17 @@ static JFRJMXRecorder connect(DaemonConfig config) throws IOException { return new JFRJMXRecorder(connection, config.getHarvestInterval(), config.streamFromJmx()); } - void startRecording() - throws MalformedObjectNameException, MBeanException, InstanceNotFoundException, - ReflectionException, IOException, OpenDataException { + void startRecording() throws JMException, IOException { logger.debug("In startRecording()"); - final var objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder"); + ObjectName objectName = makeFlightRecorderObjectName(); var o = connection.invoke(objectName, "newRecording", new Object[] {}, new String[] {}); if (!(o instanceof Long)) { throw new RuntimeException("JMX returned something that wasn't a Long: " + o); } recordingId = (Long) o; - try { - final String content = Configuration.getConfiguration("profile").getContents(); - connection.invoke( - objectName, - "setConfiguration", - new Object[] {recordingId, content}, - new String[] {"long", "java.lang.String"}); - } catch (ParseException e) { - // TODO: Something - } + configureDefaultProfile(); var maxAge = (harvestCycleDuration.toSeconds() + 10) + "s"; Map options = new HashMap<>(); @@ -152,6 +135,19 @@ void startRecording() objectName, "startRecording", new Object[] {recordingId}, new String[] {"long"}); } + private void configureDefaultProfile() throws IOException, JMException { + ObjectName objectName = makeFlightRecorderObjectName(); + connection.invoke( + objectName, + "setPredefinedConfiguration", + new Object[] {recordingId, "profile"}, + new String[] {"long", "java.lang.String"}); + } + + private ObjectName makeFlightRecorderObjectName() throws MalformedObjectNameException { + return new ObjectName("jdk.management.jfr:type=FlightRecorder"); + } + /** * Stores JFR recording data in a local file on disk. The data is either streamed over JMX or * copied from a shared filesystem. @@ -185,7 +181,7 @@ Path streamRecordingToFile() throws MalformedObjectNameException, ReflectionException, MBeanException, InstanceNotFoundException, IOException, OpenDataException { - var objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder"); + ObjectName objectName = makeFlightRecorderObjectName(); var oClone = connection.invoke( objectName, @@ -273,7 +269,7 @@ private TabularDataSupport makeOpenData(final Map options) Path copyRecordingToFile() throws MalformedObjectNameException, MBeanException, InstanceNotFoundException, ReflectionException, IOException { - var objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder"); + ObjectName objectName = makeFlightRecorderObjectName(); var oClone = connection.invoke(