-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support runtime attachment for OSGi containers #1085
Comments
First step of elastic#1085
The way to close this issue is update the integration tests to use runtime attach on all tested Servlet containers (basically - change |
Now that indy plugin migration + separate classloader are completed, we should natively support this. |
@SylvainJuge since the classloader is separate now, how do we load the classes , so that we can write our own instrumentation. Currently i am in a situation where the .esclazz files are not been loaded by the classloader which avoids compiling the code. any help is appreciated. |
Hi @chetankokil, the agent classes must be loaded with an internal classloader, and using the While the agent is packaged as a jar archive, it is not meant to be used as an application dependency because there is a complex class-loading strategy required to make the agent work properly, including in OSGi environments. It used to be possible to load agent classes from the jar previously, but it that never was something we officially supported. In your case, you should be able to use one of the officially supported ways to setup the agent, and if you really need to extend agent features you should be able to create an external plugin. If your use-case does not fit what external plugins currently allow, then let's discuss this in our forum, that will be a better place for conversation rather than in a github issue (just make sure to pose in Observability / APM + tag it with |
Background
We have experimental support for runtime attachment via a CLI tool and a self-attachment Java API. The reason that it's still in an experimental stage is that this doesn't work for OSGi containers.
Our current strategy of setting the
org.osgi.framework.bootdelegation
system property to allow theco.elastic.apm.agent
package to be loaded by the bootstrap class loader only works when attaching the agent as a-javaagent
. That is because when doing runtime attachment, the class OSGi loaders are already initialized and setting the bootdelegation doesn't have any effect.Solutions
Instrument OSGi containers to allow loading the agent classes
Pros:
Cons:
Inject a single dispatcher class into the
java.lang
namespaceThis would require a major overhaul of our current class loading architecture. But it would change it for the better. It would even allow us to do fancy things like unloading/live-updating the agent down the road.
Currently, our whole agent is loaded by the bootstrap classloader. This exposes our classes to everyone. This slows down classpath scanning tools and can cause errors. With the new design, there would only be a handful of classes to be injected into the bootstrap classloader.
We would rely on the fact that all filtering classloaders have a whitelist for the
java.lang
package. That is because all classes should naturally have access tojava.lang.String
, for example. Otherwise, no Java application could run.Therefore, we would inject a dispatcher class (for example
java.lang.apm.Dispatcher
) into the bootstrap classloader. The dispatcher is basically a globally-accessibleConcurrentHashMap
.The rest of the agent is loaded from a separate class loader that is a child of the bootstrap classloader.
The advices would only communicate with the rest of the agent via
MethodHandle
s that are stored in the dispatcher map.This is a diagram of how it would look like in a typical web application:
As this would mean quite a lot of changes, let's break it down to several steps:
apm-agent-core
moduleco.elastic.apm.agent
namespace initiallyelastic-apm-agent.jar
will still be loaded by the bootstrap CLMethodHandle
java.lang
namespaceThe text was updated successfully, but these errors were encountered: