Using Java Flight Recorder (JFR) in Quarkus
1. Introduction to Java Flight Recorder in Quarkus Setup
Monitoring and profiling Java applications is essential for ensuring performance and stability, especially in production environments. Java Flight Recorder (JFR) is a powerful profiling and diagnostics tool built into the JVM. With the increasing popularity of cloud-native frameworks, developers are now integrating JFR into modern stacks like Quarkus. In this article, we’ll walk through the java flight recorder quarkus setup custom events process, enabling you to gain deep insights into your application’s runtime behavior.
2. Maven Dependency for java flight recorder quarkus setup
To get started with the java flight recorder quarkus setup, you’ll need to add the appropriate JFR and Quarkus dependencies to your Maven project. JFR is part of the JDK starting from Java 11, so no additional library is required for the core functionality. However, for structured logging and custom event support, you can add the following dependencies:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>org.openjdk.jmc</groupId>
<artifactId>jmc-core</artifactId>
<version>8.3.0</version>
</dependency>
Make sure you are using Java 11 or higher, as JFR is bundled with the JDK.
3. Project Configuration for JFR & Quarkus Custom Events Setup
No extensive Quarkus-specific configuration is required to enable JFR, but you may want to adjust your JVM parameters to support JFR recordings. Add the following JVM options when running your Quarkus application:
java -XX:StartFlightRecording=filename=quarkus.jfr,dumponexit=true,settings=profile -jar target/quarkus-app/quarkus-run.jar
This starts a flight recording as soon as the application launches and saves the dump to quarkus.jfr
upon exit.
You can also add this to your application.properties
if you’re using a wrapper script or containerized environment:
quarkus.jvm.args=-XX:StartFlightRecording=filename=quarkus.jfr,dumponexit=true,settings=profile
4. Saving JFR Events
To generate and save JFR data, simply run your Quarkus app with the recording options enabled. You can also trigger recordings dynamically via JCMD:
jcmd <pid> JFR.start name=QuarkusRecord settings=profile filename=recording.jfr
jcmd <pid> JFR.stop name=QuarkusRecord
This is useful in containerized or live environments where startup options are harder to modify.
5. Opening the JFR Dump File
Once your application runs and shuts down (or you stop the recording manually), the .jfr
file is available for analysis. Use JDK Mission Control (JMC) to open the file:
jmc
From the UI, navigate to File > Open File… and select the quarkus.jfr
file. You can inspect various runtime metrics, including CPU usage, memory allocation, GC events, and method profiling.
6. Adding Custom Events
A standout feature of JFR is the ability to define and emit custom events, enabling you to capture application-specific behaviors. Here’s how to add custom JFR events in a Quarkus application:
Define the Custom Event Class
import jdk.jfr.Event;
import jdk.jfr.Label;
@Label("CustomBusinessEvent")
public class CustomBusinessEvent extends Event {
@Label("Action Name")
public String action;
@Label("Duration")
public long duration;
public CustomBusinessEvent(String action, long duration) {
this.action = action;
this.duration = duration;
}
}
Emit Custom Events in Your Code
public void processBusinessLogic() {
long start = System.nanoTime();
// Your business logic here
long end = System.nanoTime();
CustomBusinessEvent event = new CustomBusinessEvent("ProcessOrder", end - start);
event.commit();
}
These custom events will now show up in JMC under the Custom Events section, making it a highly effective strategy for tracking application-specific metrics.
7. Conclusion
Integrating Java Flight Recorder in Quarkus applications provides a low-overhead, high-precision way to monitor and profile runtime behavior. With minimal setup, developers can unlock powerful observability features, especially when combined with custom JFR events. Whether you’re troubleshooting a performance issue or just want to gain better insight into your application’s behavior, the java flight recorder – quarkus custom events combination is an excellent toolset to have in your DevOps workflow.