Skip to content

Commit

Permalink
feat(agent): implement HTTP JFR snapshot creation (#1627)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Azores <[email protected]>
  • Loading branch information
aali309 and andrewazores committed Sep 19, 2023
1 parent f9546db commit c31e284
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/main/java/io/cryostat/net/AgentClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,57 @@ Future<IRecordingDescriptor> startRecording(StartRecordingRequest req) {
});
}

Future<IRecordingDescriptor> startSnapshot() {
StartRecordingRequest snapshotReq = new StartRecordingRequest("snapshot", "", "", 0, 0, 0);

Future<HttpResponse<String>> f =
invoke(
HttpMethod.POST,
"/recordings/",
Buffer.buffer(gson.toJson(snapshotReq)),
BodyCodec.string());

return f.map(
resp -> {
int statusCode = resp.statusCode();
if (HttpStatusCodeIdentifier.isSuccessCode(statusCode)) {
String body = resp.body();
return gson.fromJson(body, SerializableRecordingDescriptor.class)
.toJmcForm();
} else if (statusCode == 403) {
throw new UnsupportedOperationException();
} else {
throw new RuntimeException("Unknown failure");
}
});
}

Future<Void> updateRecordingOptions(long id, IConstrainedMap<String> newSettings) {

JsonObject jsonSettings = new JsonObject();
for (String key : newSettings.keySet()) {
jsonSettings.put(key, newSettings.get(key));
}
Future<HttpResponse<Void>> f =
invoke(
HttpMethod.PATCH,
String.format("/recordings/%d", id),
jsonSettings.toBuffer(),
BodyCodec.none());

return f.map(
resp -> {
int statusCode = resp.statusCode();
if (HttpStatusCodeIdentifier.isSuccessCode(statusCode)) {
return null;
} else if (statusCode == 403) {
throw new UnsupportedOperationException();
} else {
throw new RuntimeException("Unknown failure");
}
});
}

Future<Buffer> openStream(long id) {
Future<HttpResponse<Buffer>> f =
invoke(HttpMethod.GET, "/recordings/" + id, BodyCodec.buffer());
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/io/cryostat/net/AgentJFRService.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ public List<String> getServerTemplates() throws FlightRecorderException {

@Override
public IRecordingDescriptor getSnapshotRecording() throws FlightRecorderException {
throw new UnimplementedException();
try {
return client.startSnapshot().toCompletionStage().toCompletableFuture().get();
} catch (ExecutionException | InterruptedException e) {
throw new FlightRecorderException("Failed to create snapshot recording", e);
}
}

@Override
Expand Down Expand Up @@ -245,9 +249,18 @@ public void updateEventOptions(IRecordingDescriptor arg0, IConstrainedMap<EventO
}

@Override
public void updateRecordingOptions(IRecordingDescriptor arg0, IConstrainedMap<String> arg1)
public void updateRecordingOptions(
IRecordingDescriptor recordingDescriptor, IConstrainedMap<String> newSettings)
throws FlightRecorderException {
throw new UnimplementedException();
try {
long recordingId = recordingDescriptor.getId();
client.updateRecordingOptions(recordingId, newSettings)
.toCompletionStage()
.toCompletableFuture()
.get();
} catch (ExecutionException | InterruptedException e) {
throw new FlightRecorderException("Failed to update recording options", e);
}
}

@Override
Expand Down

0 comments on commit c31e284

Please sign in to comment.