Closed MatthewVaadin closed 10 months ago
In the bakery app, I have attempted to create a dependency jar that adds the O11y Kit agent at runtime. I have tried using two methods:
Java attach API
VirtualMachine vm = VirtualMachine.attach(processId);
vm.loadAgent(agentJar);
vm.detatch();
Which gives the following error:
java.lang.RuntimeException: java.io.IOException: Can not attach to current VM
Byte buddy agent
ByteBuddyAgent.attach(agentJar, processId);
Which gives the following error:
java.lang.IllegalStateException: Could not self-attach to current VM using external process
These look like the same issue to me.
The above issue can be resolved by setting the environment variable JDK_JAVA_OPTIONS
to the value -Djdk.attach.allowAttachSelf=true
.
A way to work around the self-attach issue, may be to launch from the application an external java process that will do the agent load stuff.
This approach seems to work, give the following conditions
main
method that takes the agent jar path and a JVM process id as arguments and loads the agent into the process.WEB-INF/lib
for WAR deploymentOpenTelemetryAgent
class file on classpath (Classloader.getResources()
)java.home
system property or by using OS tools like which/where
as does Flow to search npm/pnpm
.java -classpath xxxxx com.vaadin.extension.AgentLoader /path/to/agent.jar PID
)AgentLoader
class. Some pitfalls
otel.javaagent.configuration-file
from the AgentLoader
. Environment variables and OpenTelemetry system properties seem to work only if provided ~at boot to the~ by the application process at bootstrap or at runtime.SecurityManager
)ResettableClassFileTransformer
to "unload" the agent (through OpenTelemetry ClassFileTransformerHolder
), this may not work due to JVM restrictionsAdditional note: the attach API is present only on JDK. If a JRE is used at runtime, the above code cannot be executed
@knoobie thank you for the information
Closing this given the research results and the discussed security implications.
According to the Java documentation, it should be possible to add a Java agent JAR to the classpath at runtime. Doing this would allow O11y Kit to be deployed as a single Maven dependency.