fstab / promagent

Prometheus Monitoring for Java Web Applications without Modifying their Source Code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Detect custom Hooks via Java ServiceLoader API

thomasdarimont opened this issue · comments

It would be great if promagent could automatically find and register new instrumentation Hooks via the Java ServiceLoader API.

I agree automatic discovery of instrumentation hooks would be great. There are different alternatives how this could be implemented. There is the Service Loader API you mentioned, but it would also be possible to scan promagent-hook-VERSION.jar on startup and discover all classes annotated with @Hook.

With regards to the Service Loader API: I understand this works best if there is a comprehensive interface for hooks. However, I found it hard to define an interface for hooks, because the signatures of the @Before and @After methods depend on the methods to be instrumented. This is why I ended up with custom annotations instead of an interface for hooks. Therefore, I think scanning promagent-hook-VERSION.jar for classes annotated with @Hook might be a better fit than using the Service Loader API for this specific scenario.

I implemented loading all hooks from promagent-hook-VERSION.jar and removed the hard-coded hooks array from HookFactory.

Thanks for having a look at this :)
Discovering the hooks in the agent jar itself is quite handy with the currently suggested development model of forking the agent lib to create custom agents.

Another option is to simply create a marker interface and use that with the ServiceLoader API, similar to: https://www.tutorials.de/threads/einfacher-plugin-mechanismus-mit-dem-serviceloader-api.377348/ (quite old...)
In custom hook classes you would then just implement the marker interface, add it to the service manifest file, put the jar on the agent classpath and then the agent infrastructure could analyze the provided classes and register their hooks accordingly.