Search code examples
javawildfly-10javaagents

adding a java agent to wildfly10.1.Final server


Good afternoon everyone,

My problem: I can't seem to make a java agent (https://github.com/krpors/delver) work on WildFly10.1.final

I deployed the same demo spring-boot application(war) on both tomcat and Wildfly

adding the agent to tomcat was straight forward and easy (having only to add the -javaagent:... argument to CATALINA_OPTS) the tomcat server started and the agent worked exactly as expected (listing all the methods in the specified packages in the XML config file of the agent and tracking how many times the method was called and for how long ...)

when everything seemed to work flawlessly with tomcat and the agent I tried to add the delver agent to WildFly and that's when everything started to go wrong.

It took me solid 4 days to get the wildly 10.1 server to start with the agent by adding these configs :

set JAVA_OPTS= -Djava.util.logging.manager=org.jboss.logmanager.LogManager %JAVA_OPTS%
set JAVA_OPTS= -Xbootclasspath/p:%JBOSS_HOME%\modules\system\layers\base\org\jboss\logmanager\main\jboss-logmanager-2.0.4.Final.jar;%JBOSS_HOME%\modules\system\layers\base\org\jboss\log4j\logmanager\main\log4j-jboss-logmanager-1.1.2.Final.jar %JAVA_OPTS%
set JAVA_OPTS= -javaagent:D:\wildfly-10.1.0.Final\delver\delverJar0.jar=D:\wildfly-10.1.0.Final\delver\config.xml %JAVA_OPTS%

in standalone.bat and these

set "JAVA_OPTS= -Djboss.modules.system.pkgs=com.singularity,org.jboss.byteman,com.wily,com.wily.*,org.jboss.logmanager %JAVA_OPTS%"

in standalone.conf.bat

and then I faced two problems

problem 1

using only these configs the war deployed on the server won't work (404 when I try to access the /Users endpoint ) with this stacktrace

15:38:03,908 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 60) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./demoRest: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./demoRest: java.lang.NoClassDefFoundError: nl/omgwtfbbq/delver/Signature
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
        at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.NoClassDefFoundError: nl/omgwtfbbq/delver/Signature
        at com.example.demo.ServletInitializer.configure(ServletInitializer.java:10)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:138)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:95)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
        at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:186)
        at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:171)
        at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
        at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:234)
        at 
....

problem 2: I tried to solve the NoClassDefFoundError of the signature class (a class used inside the agent, see the GitHub link) by adding the agent jar as a global module in the server(created a module with the jar and added it as global)

now the application (war) works and the agent display all the methods of the specified package as expected BUT its doesn't display how many time the method was called or the duration

these are the errors in the server console (no exceptions are thrown)

15:40:22,664 ERROR [stderr] (ServerService Thread Pool -- 58)   at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
15:40:22,665 ERROR [stderr] (ServerService Thread Pool -- 58)   at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
15:40:22,665 ERROR [stderr] (ServerService Thread Pool -- 58)   at java.lang.ClassLoader.defineClass1(Native Method)
15:40:22,666 ERROR [stderr] (ServerService Thread Pool -- 58)   at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
15:40:22,666 ERROR [stderr] (ServerService Thread Pool -- 58)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15:40:22,667 ERROR [stderr] (ServerService Thread Pool -- 58)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
15:40:22,669 ERROR [stderr] (ServerService Thread Pool -- 58)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
15:40:22,672 ERROR [stderr] (ServerService Thread Pool -- 58)   at java.lang.reflect.Method.invoke(Method.java:498)
15:40:22,673 ERROR [stderr] (ServerService Thread Pool -- 58)   at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:535)
....

It seems that I solved problem 1 in a hacky way which resulted in problem 2

and I can't help but feel that I need to change something in the source code of the agent to adapt to the class loading of WildFly 10.1. Final

ClassPool cp = ClassPool.getDefault();
cp.childFirstLookup = true;
cp.insertClassPath(new LoaderClassPath(loader));
 

            

I would really appreciate any help I get been stuck on this for a week now :(


Solution

  • As it turned out all I had to do was add the Signature package nl.omgwtfbbq.delver to jboss system modules so it can be visible to all applications

    set "JAVA_OPTS= -Djboss.modules.system.pkgs=nl.omgwtfbbq.delver,org.jboss.byteman,org.jboss.logmanager %JAVA_OPTS%"