Skip to content

Commit

Permalink
enable retrieval of custom event templates for creating recordings
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 15, 2024
1 parent c17c5d9 commit ab40d5f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/cryostat/events/S3TemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/cryostat/events/TargetTemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
94 changes: 49 additions & 45 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 -> {
Expand All @@ -169,7 +171,7 @@ public ActiveRecording startRecording(
.getService()
.start(
recordingOptions,
enableEvents(connection, templateName, preferredTemplateType));
enableEvents(target, templateName, preferredTemplateType));

Map<String, String> labels = metadata.labels();
labels.put("template.name", templateName);
Expand Down Expand Up @@ -304,54 +306,56 @@ public Pair<String, TemplateType> parseEventSpecifierToTemplate(String eventSpec
throw new BadRequestException(eventSpecifier);
}

private IConstrainedMap<EventOptionID> enableAllEvents(JFRConnection connection)
throws Exception {
EventOptionsBuilder builder = eventOptionsBuilderFactory.create(connection);
private IConstrainedMap<EventOptionID> 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<EventOptionID> 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<Template> matchingNameTemplates =
connection.getTemplateService().getTemplates().stream()
.filter(t -> t.getName().equals(templateName))
.toList();
boolean custom =
matchingNameTemplates.stream()
.anyMatch(t -> t.getType().equals(TemplateType.CUSTOM));
if (custom) {
if (customTemplateService.getTemplates().stream()
.anyMatch(t -> t.getName().equals(templateName))) {
return TemplateType.CUSTOM;
}
boolean target =
matchingNameTemplates.stream()
.anyMatch(t -> t.getType().equals(TemplateType.TARGET));
if (target) {
if (targetTemplateServiceFactory.create(target).getTemplates().stream()
.anyMatch(t -> t.getName().equals(templateName))) {
return TemplateType.TARGET;
}
throw new BadRequestException(
Expand Down

0 comments on commit ab40d5f

Please sign in to comment.