vaadin / observability-kit

Other
5 stars 3 forks source link

Investigate adding the Java agent at runtime #144

Closed MatthewVaadin closed 10 months ago

MatthewVaadin commented 1 year ago

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.

MatthewVaadin commented 1 year 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:

  1. 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

  2. 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.

MatthewVaadin commented 1 year ago

The above issue can be resolved by setting the environment variable JDK_JAVA_OPTIONS to the value -Djdk.attach.allowAttachSelf=true.

mcollovati commented 1 year ago

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

Some pitfalls

mcollovati commented 1 year ago

Additional note: the attach API is present only on JDK. If a JRE is used at runtime, the above code cannot be executed

knoobie commented 1 year ago

FYI: https://mail.openjdk.org/pipermail/serviceability-dev/2023-March/046868.html

mcollovati commented 1 year ago

@knoobie thank you for the information

heruan commented 10 months ago

Closing this given the research results and the discussed security implications.