OpenTelemetry agent adaption for GraalVM native image #11068
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduction
GraalVM native image can improve Java applications's startup performance and runtime memory footprint significantly. But as everything is compiled in advance and no bytecode is left at runtime, agent instrumentation at runtime is not possible for native image.
The agent support PR from GraalVM community proposes an approach to address such scenario. It detects the transformed classes and compiles them into the native image. But still a few things need to do at the agent side:
premain
initializations in agent can't be simply applied to the native image, because it may do something unnecessary and incompatible with native image. For example, OTel installs bytebuddy during premain phase, but it is unnecessary in native image.This PR worked on the above two sides. Together with the agent support PR, the spring boot demo application can work with OTel agent now (see the next section for details).
But this PR only made a minimal adaption to get a few demo applications work. We hope this PR is the start point for OTel agent native adaption. More people in the community can join to complete the adaption.
Run the Spring Boot Demo Application
Demo project:
spring-boot.zip
Prequisite
GRAALVM_HOME
system variable refers to one of the above GraalVM binaries.Steps
Build this PR
1.1 Install graal-sdk-instru.jar. The graal sdk jar can be compiled from the agent support PR. A precompiled graal-sdk-23.0.2-instru.jar is enclosed in the demo project zip file. Run
mvn install:install-file -Dfile=path/to/graal-sdk-23.0.2-instru.jar -Dpackaging=jar -DgroupId=org.graalvm.sdk -DartifactId=graal-sdk -Dversion=23.0.2-instru
to install it for later usage.1.2
./gradlew publishToMavenLocal
. Build and install jars to local maven repository.1.3
mvn install:install-file -Dfile=./instrumentation-native-support/build/libs/instrumentation-native-support-2.3.0-alpha-SNAPSHOT.jar -Dpackaging=jar -DgroupId=io.opentelemetry.javaagent -DartifactId=instrumentation-native-support -Dversion=2.3.0-alpha-SNAPSHOT
. Add the native support jar to your local maven repository for later usage. Note: the version can be changed. Please modify it according to your actual value.Cd to the root of unzipped demo project
mvn package
: Build the jar package of this project./run.sh --jvm --collect --log
: Execute the project in JVM mode, enabling OpenTelemetry agent to log monitoring data. Meanwhile, native-image-agent is enabled to collect transformation classes and other dynamic data. When this step ends, data is dumped tonative-configs
directory../build.sh
: Build the native image../run.sh --svm --log
: Run the native image application.curl localhost:2327
: Send a request to the application.Limitations
GraalVM native image only has one classloader at runtime, so it cannot support classloader based class isolation which is used in the OTel agent. For example, the
inst/**/*.classdata
files are renamed back toclass
and loaded in the classpath at GraalVM build time for now. One solution is to shade them to new packages, but we are not sure about its impact. It is welcomed for discussion.