Skip to content

Commit 421c00b

Browse files
author
David Grieve
committed
use JFR.check to determine recording id parameter
1 parent bbd8832 commit 421c00b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

jfr-connection/src/main/java/io/opentelemetry/contrib/jfr/connection/FlightRecorderDiagnosticCommandConnection.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ final class FlightRecorderDiagnosticCommandConnection implements FlightRecorderC
3737
"com.sun.management:type=DiagnosticCommand";
3838
private static final String JFR_START_REGEX = "Started recording (\\d+?)\\.";
3939
private static final Pattern JFR_START_PATTERN = Pattern.compile(JFR_START_REGEX, Pattern.DOTALL);
40+
private static final String JFR_CHECK_REGEX = "(recording|name)=(\\d+?)";
41+
private static final Pattern JFR_CHECK_PATTERN = Pattern.compile(JFR_CHECK_REGEX, Pattern.DOTALL);
4042

4143
// All JFR commands take String[] parameters
4244
private static final String[] signature = new String[] {"[Ljava.lang.String;"};
@@ -157,10 +159,35 @@ private static Object[] formOptions(
157159
return mkParamsArray(params);
158160
}
159161

162+
//
163+
// Whether to use the 'name' or 'recording' parameter depends on the JVM.
164+
// Use JFR.check to determine which one to use.
165+
//
166+
private String getRecordingParam(long recordingId) throws JfrConnectionException, IOException {
167+
try {
168+
Object[] params = new String[]{};
169+
String jfrCheck = (String) mBeanServerConnection.invoke(objectName, "jfrCheck", params, signature);
170+
Matcher matcher = JFR_CHECK_PATTERN.matcher(jfrCheck);
171+
while (matcher.find()) {
172+
String id = matcher.group(2);
173+
if (id.equals(Long.toString(recordingId))) {
174+
return matcher.group(0);
175+
}
176+
}
177+
} catch (InstanceNotFoundException | MBeanException | ReflectionException e) {
178+
throw JfrConnectionException.canonicalJfrConnectionException(getClass(), "jfrCheck", e);
179+
}
180+
throw JfrConnectionException.canonicalJfrConnectionException(
181+
getClass(),
182+
"jfrCheck",
183+
new IllegalStateException("No recording found for id: '" + recordingId + "'"));
184+
185+
}
186+
160187
@Override
161188
public void stopRecording(long id) throws JfrConnectionException {
162189
try {
163-
Object[] params = mkParams("recording=" + id);
190+
Object[] params = mkParams(getRecordingParam(id));
164191
mBeanServerConnection.invoke(objectName, "jfrStop", params, signature);
165192
} catch (InstanceNotFoundException | MBeanException | ReflectionException | IOException e) {
166193
throw JfrConnectionException.canonicalJfrConnectionException(getClass(), "stopRecording", e);
@@ -170,7 +197,7 @@ public void stopRecording(long id) throws JfrConnectionException {
170197
@Override
171198
public void dumpRecording(long id, String outputFile) throws IOException, JfrConnectionException {
172199
try {
173-
Object[] params = mkParams("filename=" + outputFile, "recording=" + id);
200+
Object[] params = mkParams("filename=" + outputFile, getRecordingParam(id));
174201
mBeanServerConnection.invoke(objectName, "jfrDump", params, signature);
175202
} catch (InstanceNotFoundException | MBeanException | ReflectionException e) {
176203
throw JfrConnectionException.canonicalJfrConnectionException(getClass(), "dumpRecording", e);

0 commit comments

Comments
 (0)