Skip to content

GraalVM and Spring Native Project Implementation for 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

As a runtime environment, GraalVM is unique in that it offers 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. During runtime, the application loads and executes normally on the JVM. The JVM passes the bytecode of Java or any other native JVM 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 themselves run as Java programs on the JVM.

Native Image

Native Image is an innovative technology that compiles Java code into standalone binary executables or native shared libraries. The Java bytecode processed during the native image build includes all application classes, dependencies, third-party dependency libraries, and any required JDK classes. The generated self-contained native executable is specific to each individual operating system and machine architecture and does not require the 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 integrates with Guance using ddtrace.

Configuration Steps

Warning

The version information used in this example is as follows: DataKit 1.4.0, CentOS 7.9, Cloud Server 4-core 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 a Spring Boot Project

Go to spring.io, select 「Spring Native」 and 「Spring Web」, choose 「Maven Project」 for Project, 「Java」 for Language, define your own Project Name, and click 「GENERATE」.

image

Add Application and Controller as shown in the sample project springboot-native-demo.

image

5 Package the Project

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

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

image

Check the target directory; you should see the binary file springboot-native-demo and the springboot-native-demo-1.0-SNAPSHOT-exec.jar file.

image

6 Integrate Observability

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

To enable RUM, you need to allow remote access to DataKit's port 9529. Edit the following file.

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

Change 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 the 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 Data

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

image

Log in to 「Guance」 - 「APM」 to view the spring-native-demo service.

image

In the 「Trace」 interface, you can view detailed traces and flame graphs.

image

image

Feedback

Is this page helpful? ×