Friends of OpenJDK Today

Monitoring Spring Boot Applications (Part 1)

January 12, 2022

Author(s)

  • Avatar photo
    Ashish Choudhary

    I am a software engineer and technical writer who loves Java, Spring Boot, DevOps, and the Cloud.

In part 1 of this series, we will learn about JDK Flight Recorder and how we can use it to monitor a Spring Boot application.

Introduction

Application monitoring is one thing that we as developers cannot overlook. It doesn’t matter if you run a monolith or microservices application because application monitoring is a must for production workloads to know what’s happening with your application. If you are using Spring Boot, there are different ways to monitor an application. The most popular way to monitor a Spring Boot application is to enable the spring-boot-actuator module to achieve the desired result.

More often than not, we rely on third-party tools like Appdynamics, Dynatrace, Prometheus, and Grafana to monitor our workloads. But what if I told you that you could use a tool that comes with JDK to monitor your Spring Boot applications. Could you not believe me yet? Let me introduce you to the JDK flight recorder in this article that we will use to monitor a Spring Boot application.

What is a JDK Flight Recorder?

JDK Flight Recorder is a diagnostic tool that collects events of running Java applications, JVM and OS.

JFR was a commercial feature earlier, but it has been open-sourced and made GA from OpenJDK 11 onwards. See this JEP. If you are stuck on JDK 8 like everyone else, then no worries as it seems, the work is completed to backport it to OpenJDK 8. There is no need to download it separately as JFR comes bundled with your JDK.

JDK Flight Recorder Features

  1. You can analyze the recordings offline.
  2. View detailed information on memory usage, garbage collection, hot methods and classes, threads, Java locks, and more.
  3. Minimal impact on performance(<1%).
  4. It can store events on disk and memory as well.
  5. Create customized events as per your need with API.
  6. Support for Event Streaming from OpenJDK 14 onwards.

Enabling JDK Flight Recorder

  1. Using Command-line
  2. Using jcmd tool
  3. Using JMC

Let’s understand these options in detail.

Using Command-line

If you are using OpenJDK 11 or higher version, you can enable flight recorder by passing the flag -XX:StartFlightRecording while starting your Spring Boot or Java applications. Since you can pass the flag at an application startup, it is good to rely on JFR for your application monitoring. Following is the example:

java -XX:StartFlightRecording:filename=myrecording.jfr,duration=60s -jar myapp.jar

In this example, after enabling JFR, the data is dumped to myrecording.jfr file and analyzed using a client-side tool JDK Mission Control. JMC has to be downloaded separately as it’s not part of your JDK. It is available for download from here.

Using jcmd tool

Suppose you don’t always want JFR to monitor your application as discussed in the first option but only want to diagnose when any issue occurs. In that case, jcmd is a handy option as it will send diagnostic commands to running Java applications. You have to pass the PID of the running java process or the main class and the actual command.

jcmd 24597 JFR.start duration=60s filename=myapprecording.jfr

In the above example, we have started JFR recordings for 60-seconds on the running Java process with the PID 24597 and saved it to myapprecording.jfr file in the current directory. To view all the available commands for a running Java application, specify jcmd help. Following are some of the commands that we can use with jcmd.

jcmd 24597 help

24597:

The following commands are available:

JFR.check

JFR.configure

JFR.dump

JFR.start

JFR.stop

Here is a summary of those commands: https://gist.github.com/yrashish/0fb966ef0294a5c336e9253ba78effe7

Using JMC

JDK Mission Control aka JMC is a GUI tool for examining the recording created by JFR. Not just that with JMC you can start recording events of a running java process. As shown in the following screenshot from the left-hand side navigation bar in JMC first you have to choose a running java process. Then right-click and choose Start Flight Recording.

Start Flight Recording
Start Flight Recording

In the next window, you can go with default options or tweak them as per your needs. Then click on Finish to start the recording of events for the Spring Boot application.

Once the recording is finished automated analysis results are shown.

Automated Analysis Results
Automated Analysis Results

As shown in the following screenshots you can also view Java application Threads, Heap, and CPU-related details. Moreover, you can also view JVM Internals and Environment related details.

Options to view results by different categories
Options to view results by different categories

Java Application Thread, CPU and Heap Usage
Java Application Thread, CPU and Heap Usage

That’s it we are done.

Conclusion

In this article, we have only covered how we can enable and use the JDK flight recorder to monitor a Spring Boot application. JDK flight recorder is a reasonable tool for monitoring application performance.

Having said that in part 2 of this series, we will try to monitor simple spring boot application startup events with JFR. We will also understand how we can use JFR event streaming to continuously monitor a Spring Boot application.

Related Articles

View All
  • A Simple Service with Spring Boot

    I will demonstrate how to create a simple Web Service using Spring Boot.  This framework makes it almost effortless to develop web services, so long as the appropriate dependencies are in place.

    In this example, I will create a Web Service that will read the current temperature from a file and make it available to clients via a RESTful endpoint.

    Read More
    Avatar photo
    September 22, 2020
  • Annotation-free Spring

    Some, if not most, of our judgments regarding technology stacks come either from third-party opinions or previous experiences. Yet, we seem to be adamant about them!

    Read More
    Avatar photo
    September 17, 2021
  • Better Error Handling for Your Spring Boot REST APIs

    One of the things that distinguishes a decent API from one that is a pleasure to work with is robust error handling. Nothing is more frustrating than using some API and getting back cryptic errors where you can only guess why the server is not accepting your request.

    Spring Boot lets you customize the error handling for your application, but there is quite a lot of low-level coding involved if you want to do this correctly.

    Read More
    August 21, 2021

Author(s)

  • Avatar photo
    Ashish Choudhary

    I am a software engineer and technical writer who loves Java, Spring Boot, DevOps, and the Cloud.

Comments (1)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Nils

Did you already finish the mentioned part 2 of the series and will you link it here?

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard