2323import java .util .concurrent .CompletionException ;
2424import java .util .stream .Collectors ;
2525
26+ import io .cryostat .events .SerializableEventTypeInfo ;
2627import io .cryostat .expressions .MatchExpression .ExpressionEvent ;
2728import io .cryostat .targets .Target ;
2829import io .cryostat .targets .Target .Annotations ;
2930import io .cryostat .targets .Target .TargetDiscovery ;
31+ import io .cryostat .targets .TargetConnectionManager ;
3032
3133import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
3234import io .quarkus .cache .CacheManager ;
3335import io .quarkus .cache .CacheResult ;
3436import io .quarkus .cache .CompositeCacheKey ;
37+ import io .quarkus .logging .Log ;
3538import io .quarkus .vertx .ConsumeEvent ;
3639import jakarta .annotation .Nullable ;
3740import jakarta .enterprise .context .ApplicationScoped ;
4245import jdk .jfr .Label ;
4346import jdk .jfr .Name ;
4447import org .jboss .logging .Logger ;
48+ import org .projectnessie .cel .EnvOption ;
49+ import org .projectnessie .cel .Library ;
50+ import org .projectnessie .cel .ProgramOption ;
4551import org .projectnessie .cel .checker .Decls ;
52+ import org .projectnessie .cel .common .types .ListT ;
53+ import org .projectnessie .cel .interpreter .functions .Overload ;
4654import org .projectnessie .cel .tools .Script ;
4755import org .projectnessie .cel .tools .ScriptCreateException ;
4856import org .projectnessie .cel .tools .ScriptException ;
@@ -56,6 +64,7 @@ public class MatchExpressionEvaluator {
5664 @ Inject ScriptHost scriptHost ;
5765 @ Inject Logger logger ;
5866 @ Inject CacheManager cacheManager ;
67+ @ Inject TargetConnectionManager connectionManager ;
5968
6069 @ ConsumeEvent (value = MatchExpression .EXPRESSION_ADDRESS , blocking = true )
6170 void onMessage (ExpressionEvent event ) {
@@ -104,6 +113,7 @@ Script createScript(String matchExpression) throws ScriptCreateException {
104113 Decls .newVar (
105114 "target" ,
106115 Decls .newObjectType (SimplifiedTarget .class .getName ())))
116+ .withLibraries (List .of (new EventTypesLibrary (connectionManager )))
107117 .build ();
108118 } finally {
109119 evt .end ();
@@ -221,6 +231,7 @@ public static class ScriptCreation extends Event {}
221231 * expression-relevant fields exposed, connection URI exposed as a String, etc.
222232 */
223233 private static record SimplifiedTarget (
234+ long id ,
224235 boolean agent ,
225236 String connectUrl ,
226237 String alias ,
@@ -240,6 +251,7 @@ private static record SimplifiedTarget(
240251
241252 static SimplifiedTarget from (Target target ) {
242253 return new SimplifiedTarget (
254+ target .id ,
243255 target .isAgent (),
244256 target .connectUrl .toString (),
245257 target .alias ,
@@ -248,4 +260,57 @@ static SimplifiedTarget from(Target target) {
248260 target .annotations );
249261 }
250262 }
263+
264+ static class EventTypesLibrary implements Library {
265+
266+ private final TargetConnectionManager connectionManager ;
267+
268+ EventTypesLibrary (TargetConnectionManager connectionManager ) {
269+ this .connectionManager = connectionManager ;
270+ }
271+
272+ @ Override
273+ public List <EnvOption > getCompileOptions () {
274+ return List .of (
275+ EnvOption .declarations (
276+ Decls .newFunction (
277+ "jfrEventTypeIds" ,
278+ Decls .newOverload (
279+ "jfrEventTypeIds_void" ,
280+ List .of (
281+ Decls .newObjectType (
282+ SimplifiedTarget .class .getName ())),
283+ Decls .newListType (Decls .String )))));
284+ }
285+
286+ @ Override
287+ public List <ProgramOption > getProgramOptions () {
288+ return List .of (
289+ ProgramOption .functions (
290+ Overload .unary (
291+ "jfrEventTypeIds" ,
292+ (arg ) ->
293+ ListT .newStringArrayList (
294+ getJfrEventTypeIds (
295+ (SimplifiedTarget ) arg .value ())))));
296+ }
297+
298+ private String [] getJfrEventTypeIds (SimplifiedTarget st ) {
299+ Target target = Target .find ("id" , st .id ()).singleResult ();
300+ try {
301+ return connectionManager .executeConnectedTask (
302+ target ,
303+ connection ->
304+ connection .getService ().getAvailableEventTypes ().stream ()
305+ .map (SerializableEventTypeInfo ::fromEventTypeInfo )
306+ .map (SerializableEventTypeInfo ::typeId )
307+ .distinct ()
308+ .toList ()
309+ .toArray (new String [0 ]));
310+ } catch (Exception e ) {
311+ Log .error (e );
312+ return new String [0 ];
313+ }
314+ }
315+ }
251316}
0 commit comments