Sub topic 11
Collecting metadata with the tracing agent
This chapter assumes you know what reachability metadata is and why reflection, resources and proxies need it under the closed-world assumption. It also assumes you can build a Spring Boot jar with Maven or Gradle. If a library uses reflection that Spring AOT does not know about, the tracing agent can find out what is needed.
What the agent does
The tracing agent is a JVM agent that ships with GraalVM. You run your application on a normal JVM with the agent attached. The agent watches for reflection calls, resource lookups, dynamic proxy creation, JNI use and serialization. It writes what it sees as JSON metadata files. native-image reads those files during the build and keeps the listed classes, resources and proxies in the executable.
The agent records what actually happens during the run. It does not analyze your code. It only finds what your run exercises.
Running the agent
Build the jar first, then start it with the agent attached:
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image -jar target/demo.jarThe config-output-dir option sets where the agent writes its files. The path META-INF/native-image on the classpath is a location native-image scans automatically, so no extra build flag is needed. The agent writes files such as reflect-config.json, resource-config.json, proxy-config.json and reachability-metadata.json, depending on the GraalVM version.
Use the java from a GraalVM installation. A JDK without the agent will fail to start with this option.
Exercising the application
The agent only records code paths that run. If you start the application and stop it at once, you get metadata for startup and nothing else. To get useful output:
- Start the application with the agent.
- Call every endpoint, including error paths.
- Trigger scheduled jobs, message listeners and other background work.
- Use each serialization format your application supports, such as JSON requests and responses.
- Stop the application normally so the agent can write its files.
Running your test suite on the JVM with the agent is another way to reach many code paths. Test code can add entries for test-only classes, so review the output.
Reviewing the output
Treat the generated files as a starting point. They are plain JSON, so read them before you commit them.
- Remove entries for test classes and tooling that you do not need at runtime.
- Check that nothing environment-specific was recorded, such as local file paths.
- Remove entries that Spring AOT already generates. Duplicates are harmless but add noise.
Rerun the agent after you upgrade a library or change code that uses reflection. Old metadata can hide a new gap, or keep classes that no longer exist.
Agent or runtime hints?
Both approaches produce the same kind of metadata. They differ in how you maintain it.
| Approach | Strength | Weakness |
|---|---|---|
| Tracing agent | Fast to start, finds usage you did not know about | Only covers what the run exercised, output needs review |
| `RuntimeHintsRegistrar` | Explicit, versioned with your Java code, can be tested | You must know what to register |
A common workflow is to use the agent to discover what is missing, then turn the important findings into RuntimeHintsRegistrar code for your own classes. Keep agent output for third-party libraries that have no hints of their own.
Exercise
You run your application with the tracing agent, call only the /health endpoint, and stop it. The native executable later fails on a POST /orders request with a reflection error on the Order class. Why did this happen, and what should you do?
Show answer
The agent records only code paths that execute. The POST /orders path never ran, so Order was not recorded. Rerun the application with the agent, exercise all endpoints including POST /orders, review the new metadata, and rebuild. Alternatively, register Order with a RuntimeHintsRegistrar.