-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Isolated agent classloader #2109
Conversation
What has previously been named elastic-apm-agent is now apm-agent. The new elastic-apm-agent module is just responsible for the startup (agentmain/premain) and loads the actual agent from an isolated class loader hierarchy.
The log shading config is more peculiar
❕ Build Aborted
Expand to view the summary
Build stats
Trends 🧪Log outputExpand to view the last 100 lines of log output
|
956268d
to
775e97b
Compare
Not needed anymore as all plugins are indy plugins now
This is to avoid having to use log4j2's package scanning to discover log plugins. The package scanner gets confused about our shaded agent classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
apm-agent-core/src/main/java/co/elastic/apm/agent/bci/classloading/IndyPluginClassLoader.java
Outdated
Show resolved
Hide resolved
elastic-apm-agent/src/main/java/co/elastic/apm/agent/premain/AgentMain.java
Outdated
Show resolved
Hide resolved
One remaining issue is that the |
Are you using the latest version of it? I factored out an |
Could you elaborate on how a custom implementation would look like in our case that reuses lookup keys but doesn’t rely on the agent class loader to be persistent? Does that include creating a persistent class loader as the parent of the agent class loader? |
Yes, you would need a separate class loader that serves as the parent for your agent. In your trampoline, you would create this class loader and store it in a field. If the field value is already set, you skip this step and reuse this class loader with the weak keys as a parent to your agent class loader. This way, you can get rid of the agent and still have your keys sticky. As the key class loader is referenced by the system class loader, it is persistent just as the system loader itself. (The key loader would in your case still have the platform loader as its parent.) |
...rofiling-plugin/src/main/java/co/elastic/apm/agent/profiler/asyncprofiler/AsyncProfiler.java
Outdated
Show resolved
Hide resolved
apm-agent-common/src/main/java/co/elastic/apm/agent/common/util/ResourceExtractionUtil.java
Outdated
Show resolved
Hide resolved
apm-agent-common/src/main/java/co/elastic/apm/agent/common/util/ResourceExtractionUtil.java
Show resolved
Hide resolved
apm-agent-common/src/test/java/co/elastic/apm/agent/common/util/ResourceExtractionUtilTest.java
Outdated
Show resolved
Hide resolved
apm-agent-core/src/main/java/co/elastic/apm/agent/bci/TracerAwareInstrumentation.java
Show resolved
Hide resolved
...c/main/java/co/elastic/apm/agent/errorlogging/Log4j2LoggerErrorCapturingInstrumentation.java
Show resolved
Hide resolved
elastic-apm-agent/src/main/java/co/elastic/apm/agent/premain/ShadedClassLoader.java
Show resolved
Hide resolved
elastic-apm-agent/src/main/java/co/elastic/apm/agent/premain/ShadedClassLoader.java
Outdated
Show resolved
Hide resolved
6727b0d
to
74c9132
Compare
I've implemented that in this commit: felixbarny@771c27e I'll create a PR once this one is merged. The biggest shift will be that the trampoline agent can't ship with the main agent. Instead, the main agent needs to be on the file system and updating the agent needs to happen by dropping in a new agent version in the agents folder (similar to hot-deploying a webapp). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
One request for extending the CL test.
Also, please add to the PR description which manual tests were applied.
elastic-apm-agent/src/test/java/co/elastic/apm/agent/premain/ShadedClassLoaderTest.java
Show resolved
Hide resolved
💚 Build Succeeded
Expand to view the summary
Build stats
Test stats 🧪
Trends 🧪💚 Flaky test reportTests succeeded. Expand to view the summary
Test stats 🧪
|
What does this PR do?
Module changes
elastic-apm-agent
toapm-agent
.Premain-Class
/Agent-Class
manifest entries (it cannot be used via-javaagent
)apm-agent-premain
toelastic-apm-agent
.Premain-Class
/Agent-Class
manifest entries.apm-agent
as shaded resource in theagent
folder..class
files are renamed to.esclass
so that they can't be loaded by regular class loadersShadedClassLoader
, aka the agent class loader, which can load the.esclass
files.java.lang.IndyBootstrapDispatcher
invisible to the regular class loader hierarchy.sun.misc.Unsafe
-based injection (InjectionStrategy.UsingUnsafe
/InjectionStrategy.UsingReflection
).apm-agent-common
module.during agent startup, in the agent itself, and in the attacher moduleResourceExtractionUtil
that extracts a class path resource to the temp dir, adds a md5 hash and re-uses already extracted resources if the hashes match.This is how the new structure looks like in detail:

More details about the indy-dispatching approach can be found in the Javadocs of
IndyBootstrap
Checklist