Cloud Native 13 min read

Auto‑Instrumenting Java in GraalVM Native Images: A Practical OTel Solution

This article explains how Alibaba Cloud engineers tackled the challenges of long startup times and high memory usage in Java serverless workloads by designing a static Java Agent instrumentation that works with GraalVM Native Image, preserving OpenTelemetry observability while dramatically improving performance.

Alibaba Cloud Observability
Alibaba Cloud Observability
Alibaba Cloud Observability
Auto‑Instrumenting Java in GraalVM Native Images: A Practical OTel Solution

Background

OpenTelemetry's influence is growing; Alibaba Cloud contributes heavily to OTel Java Instrumentation, ranking first in APAC. At the 2024 OpenTelemetry Community Day, engineers presented a solution for automatically enhancing OTel Java Agent under GraalVM static compilation.

Challenges

Serverless Java apps suffer long startup times and high memory usage due to JVM initialization, class loading, and JIT compilation. GraalVM Native Image removes the VM, reducing startup time and memory, but disables dynamic features like reflection and Java Agent bytecode rewriting.

Solution Overview

The proposed approach introduces a static Java Agent instrumentation that works before compilation. It consists of a pre‑run phase where OTel Java Agent and a Native Image agent collect transformed classes, and a static compilation phase that replaces original classes with the transformed ones, producing an executable with built‑in observability.

Implementation Details

Native Image agent is extended with an interceptor to record class bytecode changes. Additional adaptations handle JDK classes, classloader differences, and shading. Class replacement can be done via the JDK -classpath or --module‑path patching mechanisms.

Demo & Results

Videos demonstrate pre‑execution, static compilation, and runtime of the compiled binary, showing startup time reduced to ~0.07 s and memory usage dramatically lower while still collecting traces and metrics. Benchmarks on Spring Boot, Kafka, Redis, and MySQL confirm the benefits.

Future Plans

Further work includes comprehensive testing of metrics, traces, and logs, and merging the pre‑run phase into compilation to capture all transformed classes, reducing reliance on test coverage.

Community Involvement

Alibaba Cloud continues to contribute to OpenTelemetry and GraalVM communities, with PRs, issue discussions, and upcoming open‑source releases, and will present at KubeCon + CloudNativeCon 2024.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceCloud NativeOpenTelemetrygraalvmnative-imageJava Agent
Alibaba Cloud Observability
Written by

Alibaba Cloud Observability

Driving continuous progress in observability technology!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.