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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Observability
Driving continuous progress in observability technology!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
