-
Hello 👋 I'm trying to access a static field of the target application from a It seems it's not the same class when I access it from within the target application and when accessing it from the I've tried adding a What's the intended way of accessing a static field or sharing an instance of an object between the agent extension and the target application? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Application and extension are loaded in different class loaders, you can not directly read a field in application code from an extension. To share an object you'll have to place it in a class loader that is visible to both the application and the extension, that would be the boot loader. Unfortunately this currently can't be easily done with an extension. There is a sample for it in the custom distro, see https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/examples/distro/bootstrap/src/main/java/com/example/javaagent/bootstrap. Alternatively if the application class that you wish to access is accessible though system class loader you could use |
Beta Was this translation helpful? Give feedback.
-
Thanks @laurit! I've tried using reflection but that would mean performing every interaction with the shared instance (and any nested objects) through reflection so I will now switch to creating a custom distro as shown in your example. |
Beta Was this translation helpful? Give feedback.
Application and extension are loaded in different class loaders, you can not directly read a field in application code from an extension. To share an object you'll have to place it in a class loader that is visible to both the application and the extension, that would be the boot loader. Unfortunately this currently can't be easily done with an extension. There is a sample for it in the custom distro, see https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/examples/distro/bootstrap/src/main/java/com/example/javaagent/bootstrap. Alternatively if the application class that you wish to access is accessible though system class loader you could use
Class.forName
with…