Injecting classes into URL ClassLoader #1740
ananthcodekarma
started this conversation in
General
Replies: 1 comment
-
You can use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am trying to build an agent for instrumenting HTTP Services in java. Mostly written on Springboot or Dropwizard. I have a simple interceptor which intercepts the DispatcherServlet.
I have in-lined my agent @MethodExit code. However, that code invokes a static method of another class. I m seeing that, that class is accessible by the inlined code. However, when the class accesses the method, HTTPServeletRequest it throws a NoClassDefFound error. I understand the reason behind this.
ClassLoader of DoServiceAdvice:jdk.internal.loader.ClassLoaders$AppClassLoader@4e0e2f2a
ClassLoader of HttpServletRequest:org.springframework.boot.loader.LaunchedURLClassLoader@57a3e26a
ClassLoader of DispatcherServeletInstrumentationHelper:jdk.internal.loader.ClassLoaders$AppClassLoader@4e0e2f2a
So the AppClassLoader is not able to access a class from the LaunchedURLClassLoader.
I saw solutions of injecting the entire agent code as a JAR into bootstrap (Instrumentation:appendToBootstrapClassLoaderSearch). However, that created more issues for me as Bootstrap already had some classes and threw a bigger exception.
I saw an article written by elastic https://www.elastic.co/blog/embracing-invokedynamic-to-tame-class-loaders-in-java-agents. The solution provided requires us to a do a lot of things.
I tried adding the AgentBuilder.InitializationStrategy.SelfInjection.Eager(). Still it doesn't look my classes are injected.
My question finally.
Given that this is a common problem, is there any bytebuddy based method to do this easily.
We can either use invokeDynamic, but the logic of managing multiple classLoaders etc. is abstracted. All my agent code is in a classLoader which is a child of the URLClassLoader and the invokeDynamic dispatcher is present in bootstrap and all the wiring is done.
Is there any way to do this automatically like what open-telemetry does. By injecting all the classses that my Advice Depends on, into the URLClassLoader proactively so that everything is within the same scope. (I understand this might create conflicts for common packages, but lets assume we can shade).
I am pretty sure this is available. Any help to point me to how to get this done will be greatly helpful.
Beta Was this translation helpful? Give feedback.
All reactions