Skip to content

GraalVM and Spring Native Project Implementation of Trace Observability


Introduction

GraalVM is a high-performance, cloud-native, multi-language JDK distribution designed to accelerate the execution of applications written in Java and other JVM languages. It also provides runtimes for JavaScript, Python, and many other popular languages.

image

GraalVM is unique as a runtime environment, offering multiple operational modes: JVM runtime mode, Native Image, and Java on Truffle (the same Java application can run on any of these).

JVM Runtime Mode

When running programs on the HotSpot JVM, GraalVM uses the GraalVM Compiler as the top-level JIT compiler by default. At runtime, the application is normally loaded and executed on the JVM. The JVM passes the bytecode of Java or any other JVM-native language to the compiler, which compiles it into machine code and returns it to the JVM. Interpreters for supported languages written on the Truffle Framework itself runs as a Java program on the JVM.

Native Image

Native Image is an innovative technology that compiles Java code into standalone binary executables or native shared libraries. Java bytecode processed during the native image build includes all application classes, dependencies, third-party dependency libraries, and any required JDK classes. The resulting self-contained native executable is specific to each individual operating system and machine architecture that does not require a JVM.

Spring Native provides support for compiling Spring applications into native executables using the GraalVM native image compiler. This article uses the JVM runtime mode and implements trace access with ddtrace for Guance.

Operation Steps

Warning

The version information used in this example is as follows: DataKit 1.4.0, CentOS 7.9, Cloud Server 4 cores 8GB, Git 1.8.3.1, Maven 3.8.5, OpenJDK 17.0.3, GraalVM CE 22.1.0

1 Deploy Git

yum install -y git
git --version

image

2 Deploy GraalVM

cd /usr/local/df-demo
wget https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-22.1.0/graalvm-ce-java17-linux-amd64-22.1.0.tar.gz
tar -zxvf  graalvm-ce-java17-linux-amd64-22.1.0.tar.gz

Edit /etc/profile and add the following content.

export JAVA_HOME=/usr/local/df-demo/graalvm-ce-java17-22.1.0
export CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH

Install and configure native-image.

gu install native-image
yum install zlib-devel -y

3 Deploy Maven

cd /usr/local/df-demo
wget https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
tar -zxvf  apache-maven-3.8.5-bin.tar.gz

Edit /etc/profile and add the following content.

 export MAVEN_HOME=/usr/local/df-demo/apache-maven-3.8.5
 export PATH=$PATH:$MAVEN_HOME/bin:$JAVA_HOME/bin   # Modify
source /etc/profile
cd /usr/local/df-demo/apache-maven-3.8.5/conf

Edit settings.xml and add the following content.

   <mirrors>
       <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
    </mirrors>

4 Create Spring Boot Project

Enter spring.io, select 「Spring Native」 and 「Spring Web」, choose 「Maven Project」 for Project, select 「Java」 for Language, define Project Name as desired, and click 「GENERATE」.

image

Add Application and Controller, such as the sample project springboot-native-demo.

image

5 Package the Project

Upload the project to the /usr/local/df-demo directory on the cloud service and execute the packaging command.

cd /usr/local/df-demo/springboot-native-demo
mvn -Pnative -DskipTests clean package

image

Check the target directory; there should be binary files springboot-native-demo and springboot-native-demo-1.0-SNAPSHOT-exec.jar.

image

6 Trace Access

6.1 Install DataKit

Log in to 「Guance」, navigate sequentially to 「Integration」 - 「DataKit」 - 「Linux」, and click 「Copy Icon」 to copy the installation command.

image

Log in to the Linux server and execute the copied command.

image

6.2 Enable Collector

Enable RUM by allowing remote access to DataKit's port 9529, edit the following file.

/usr/local/datakit/conf.d/datakit.conf

Set the listen value to 0.0.0.0:9529

image

Enable the ddtrace collector.

cd /usr/local/datakit/conf.d/ddtrace
cp ddtrace.conf.sample ddtrace.conf

image

6.3 Restart DataKit

systemctl restart datakit

6.4 Start Application

cd /usr/local/df-demo/springboot-native-demo/target
 java  -DspringAot=true -javaagent:/usr/local/datakit/data/dd-java-agent.jar  -Ddd.service.name=spring-native-demo    -Ddd.env=dev  -Ddd.agent.port=9529  -jar springboot-native-demo-1.0-SNAPSHOT-exec.jar

image

6.5 Report Trace

Access the application interface curl localhost:8090/ping to report trace data.

image

Log in to 「Guance」 - 「Application Performance Monitoring」, and you will see the spring-native-demo service.

image

In the 「Trace」 interface, you can view trace details, flame graphs, etc.

image

image

Feedback

Is this page helpful? ×