From 711c0fea7ce997a43ee55437ccc7180dfaff0add Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 15 Feb 2024 14:19:01 -0500 Subject: [PATCH] enable retrieval of custom event templates for creating recordings --- .../io/cryostat/events/S3TemplateService.java | 2 +- .../events/TargetTemplateService.java | 6 +- .../cryostat/recordings/RecordingHelper.java | 94 ++++++++++--------- 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/main/java/io/cryostat/events/S3TemplateService.java b/src/main/java/io/cryostat/events/S3TemplateService.java index be6683a0c..7613f22b5 100644 --- a/src/main/java/io/cryostat/events/S3TemplateService.java +++ b/src/main/java/io/cryostat/events/S3TemplateService.java @@ -77,7 +77,7 @@ import software.amazon.awssdk.services.s3.model.Tagging; @ApplicationScoped -class S3TemplateService implements MutableTemplateService { +public class S3TemplateService implements MutableTemplateService { static final String EVENT_TEMPLATE_CREATED = "TemplateUploaded"; static final String EVENT_TEMPLATE_DELETED = "TemplateDeleted"; diff --git a/src/main/java/io/cryostat/events/TargetTemplateService.java b/src/main/java/io/cryostat/events/TargetTemplateService.java index ba3060e08..12c4dc9c9 100644 --- a/src/main/java/io/cryostat/events/TargetTemplateService.java +++ b/src/main/java/io/cryostat/events/TargetTemplateService.java @@ -32,13 +32,13 @@ import jakarta.inject.Inject; import org.jsoup.nodes.Document; -class TargetTemplateService implements TemplateService { +public class TargetTemplateService implements TemplateService { @ApplicationScoped - static class Factory { + public static class Factory { @Inject TargetConnectionManager connectionManager; - TargetTemplateService create(Target target) { + public TargetTemplateService create(Target target) { return new TargetTemplateService(connectionManager, target); } } diff --git a/src/main/java/io/cryostat/recordings/RecordingHelper.java b/src/main/java/io/cryostat/recordings/RecordingHelper.java index aecb9a073..f551573b7 100644 --- a/src/main/java/io/cryostat/recordings/RecordingHelper.java +++ b/src/main/java/io/cryostat/recordings/RecordingHelper.java @@ -50,8 +50,9 @@ import io.cryostat.core.net.JFRConnection; import io.cryostat.core.sys.Clock; import io.cryostat.core.sys.FileSystem; -import io.cryostat.core.templates.Template; import io.cryostat.core.templates.TemplateType; +import io.cryostat.events.S3TemplateService; +import io.cryostat.events.TargetTemplateService; import io.cryostat.recordings.ActiveRecording.Listener.ActiveRecordingEvent; import io.cryostat.recordings.ActiveRecording.Listener.ArchivedRecordingEvent; import io.cryostat.recordings.Recordings.ArchivedRecording; @@ -108,23 +109,24 @@ public class RecordingHelper { Pattern.compile("^template=([\\w]+)(?:,type=([\\w]+))?$"); public static final String DATASOURCE_FILENAME = "cryostat-analysis.jfr"; - @Inject Logger logger; + @Inject S3Client storage; + + @Inject WebClient webClient; + @Inject FileSystem fs; + @Inject Clock clock; @Inject TargetConnectionManager connectionManager; + @Inject RemoteRecordingInputStreamFactory remoteRecordingStreamFactory; @Inject RecordingOptionsBuilderFactory recordingOptionsBuilderFactory; @Inject EventOptionsBuilder.Factory eventOptionsBuilderFactory; - @Inject EventBus bus; - - @Inject Clock clock; + @Inject TargetTemplateService.Factory targetTemplateServiceFactory; + @Inject S3TemplateService customTemplateService; @Inject @Named(Producers.BASE64_URL) Base64 base64Url; - @Inject RemoteRecordingInputStreamFactory remoteRecordingStreamFactory; - @Inject S3Client storage; - @Inject FileSystem fs; - - @Inject WebClient webClient; + @Inject EventBus bus; + @Inject Logger logger; @ConfigProperty(name = ConfigProperties.AWS_BUCKET_NAME_ARCHIVES) String archiveBucket; @@ -147,7 +149,7 @@ public ActiveRecording startRecording( throws Exception { String recordingName = (String) recordingOptions.get(RecordingOptionsBuilder.KEY_NAME); TemplateType preferredTemplateType = - getPreferredTemplateType(connection, templateName, templateType); + getPreferredTemplateType(target, templateName, templateType); getDescriptorByName(connection, recordingName) .ifPresent( previous -> { @@ -169,7 +171,7 @@ public ActiveRecording startRecording( .getService() .start( recordingOptions, - enableEvents(connection, templateName, preferredTemplateType)); + enableEvents(target, templateName, preferredTemplateType)); Map labels = metadata.labels(); labels.put("template.name", templateName); @@ -304,54 +306,56 @@ public Pair parseEventSpecifierToTemplate(String eventSpec throw new BadRequestException(eventSpecifier); } - private IConstrainedMap enableAllEvents(JFRConnection connection) - throws Exception { - EventOptionsBuilder builder = eventOptionsBuilderFactory.create(connection); + private IConstrainedMap enableAllEvents(Target target) throws Exception { + return connectionManager.executeConnectedTask( + target, + connection -> { + EventOptionsBuilder builder = eventOptionsBuilderFactory.create(connection); - for (IEventTypeInfo eventTypeInfo : connection.getService().getAvailableEventTypes()) { - builder.addEvent(eventTypeInfo.getEventTypeID().getFullKey(), "enabled", "true"); - } + for (IEventTypeInfo eventTypeInfo : + connection.getService().getAvailableEventTypes()) { + builder.addEvent( + eventTypeInfo.getEventTypeID().getFullKey(), "enabled", "true"); + } - return builder.build(); + return builder.build(); + }); } public IConstrainedMap enableEvents( - JFRConnection connection, String templateName, TemplateType templateType) - throws Exception { + Target target, String templateName, TemplateType templateType) throws Exception { if (templateName.equals("ALL")) { - return enableAllEvents(connection); + return enableAllEvents(target); + } + TemplateType type = getPreferredTemplateType(target, templateName, templateType); + switch (type) { + case TARGET: + return targetTemplateServiceFactory + .create(target) + .getEvents(templateName, type) + .orElseThrow(); + case CUSTOM: + return customTemplateService.getEvents(templateName, templateType).orElseThrow(); + default: + throw new BadRequestException( + String.format("Invalid/unknown event template %s", templateName)); } - // if template type not specified, try to find a Custom template by that name. If none, - // fall back on finding a Target built-in template by the name. If not, throw an - // exception and bail out. - TemplateType type = getPreferredTemplateType(connection, templateName, templateType); - return connection.getTemplateService().getEvents(templateName, type).get(); } public TemplateType getPreferredTemplateType( - JFRConnection connection, String templateName, TemplateType templateType) - throws Exception { + Target target, String templateName, TemplateType templateType) throws Exception { + // if template type not specified, try to find a Custom template by that name. If none, + // fall back on finding a Target built-in template by the name. If not, throw an + // exception and bail out. if (templateType != null) { return templateType; } - if (templateName.equals("ALL")) { - // special case for the ALL meta-template - return TemplateType.TARGET; - } - List