Enable Spring Boot ApplicationStartup Metrics to Diagnose Slow Startup
We will enable ApplicationStartup metrics in Spring Boot, enable Spring Boot Actuator startup endpoint, and record application startup metrics with Java Flight Recorder.
Join the DZone community and get the full member experience.
Join For FreeOverview
During an application startup process, Spring Boot performs a lot of work in the background. This work involves creating Spring Application Context, creating various beans, auto-wiring, and auto-configuration of various components, and finally, starting the application. When a Spring Boot Application has a slow startup, it can be one or more beans and related dependencies taking longer to initialise and slowing down the entire process.
Profiling Spring Boot application doesn’t often help in diagnosing the startup issues. This is because there are a number of beans getting initialised and it is really difficult to figure out which ones are causing the latency. Spring Boot Application Startup Metrics are useful for such cases.
In this tutorial, we will enable the ApplicationStartup metrics in a Spring Boot application. We will also Enable Spring Boot Actuator startup endpoint to monitor the startup metrics. Finally, we will record the application startup metrics with Java Flight Recorder.
ApplicationStartup and StartupStep
Before the release of Spring Boot 2.4, it was really hard to identify the root cause of a slow startup. However, with the Spring Boot 2.4.0, we get an opportunity of generating ApplicationStartup Metrics which can be used to identify exactly which part is taking longer.
Although, the ApplicationStartup
interface and its concept are new to Spring Boot, they are a part of Spring Framework since version 5.3. The startup metrics provide detailed and stepped down logs from component initialisation, bean instantiations, and their dependencies linkages. Also, the metrics provide the start and end times of every granular step, which is quite helpful in determining the slowest bit.
During the application startup metrics capturing the entire startup process is divided into multiple steps, which are represented by the StartupStep interface. Each of these steps has a unique Id, name of the step, parent Id. Also, the implementations log the step start and end times. Using them, we can find which step is slower than expected.
Enable ApplicationStartup Metrics
In order to enable the ApplicationStartup metrics, we need to make sure we are using Spring Boot version 2.4.0 or higher.
pom.xml:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> </parent>
or build.gradle:
plugins { id 'org.springframework.boot' version '2.4.2' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' }
The SpringApplication
class, which starts any Spring Boot Application, now gets the setStartup
method, which takes an implementation of ApplicationStartup
interface.
xxxxxxxxxx
package com.amitph.spring.tutorials.students;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.setApplicationStartup(new BufferingApplicationStartup(10000));
application.run(args);
}
}
In the above example, we create an instance of SpringApplication
, and before calling the run
method we set applicationStartup
on the application and passing an instance of BufferingApplicationStartup
. Alternatively, we can use FlightRecorderApplicationStartup
to monitor the startup metrics through JFR. Next, we will learn more about both of these startup implementations.
ApplicationStartup Metrics With /startup Actuator Endpoint
The Application Startup metrics can be monitored from /startup
endpoint in Spring Boot Actuator. To do that we need to plugin Buffering Application Startup and enable /startup
endpoint.
First step is to plugin BufferingApplicationStartup
.
x
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.setApplicationStartup(
new BufferingApplicationStartup(10000));
application.run(args);
}
We are specifying the buffer capacity of 10000. The application startup metrics are captured into a buffer made available in /startup
endpoint of the actuator.
Next, we will enable the /startup
endpoint in actuator properties.
In an application.yml file:
xxxxxxxxxx
management
endpoints
web
exposure
include'startup'
Or, in an application.properties file:
management.endpoints.web.exposure.include=startup
Once enabled, we need to start the actuator and execute a POST request on /actuator/startup
endpoint.
We can execute http://host:port/actuator/startup
from a Web Browser, or use curl
as shown next.
curl --location --request POST 'http://localhost:8080/actuator/startup'
Next, is a snippet of output which shows StudentRepository
and StudentController
beans and their creation start and end times.
xxxxxxxxxx
{
"startupStep": {
"name": "spring.beans.instantiate",
"id": 123,
"parentId": 122,
"tags": [
{
"key": "beanName",
"value": "studentRepository"
}
]
},
"startTime": "2021-01-26T06:38:09.234585991Z",
"endTime": "2021-01-26T06:38:09.380445297Z",
"duration": "PT0.145859306S"
},
{
"startupStep": {
"name": "spring.beans.instantiate",
"id": 122,
"parentId": 5,
"tags": [
{
"key": "beanName",
"value": "studentsController"
}
]
},
"startTime": "2021-01-26T06:38:09.231829732Z",
"endTime": "2021-01-26T06:38:09.382129262Z",
"duration": "PT0.15029953S"
},
It is important to note that although the /startup endpoint is returning a JSON, the endpoint follows the HTTP POST method. Also, the metrics are stored in a buffer, hence the endpoint will return them only once.
ApplicationStartup Metrics With Java Flight Recorder
The Java Flight Recorder (JFR) is a monitoring tool that is part of the JVM. It can collect various metrics and diagnostics data from a Java Application and provide tools to analyse them.
In order to view ApplicationStartup metrics with Java Flight Recorder, we need to use FlightRecordingApplicationStartup
.
x
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.setApplicationStartup(new FlightRecorderApplicationStartup());
application.run(args);
}
Once, it is done, we need to package our application into a JAR.
In order to capture metrics using Java Flight Recorder, we need to pass a few parameters while running the application:
java -XX:+FlightRecorder \ -XX:StartFlightRecording=duration=5s,filename=myrecording.jfr \ -jar target/spring-boot-crud-jpa.jar
Here, we are enabling flight recorder using -XX:+FlightRecorder
argument. Next, we instruct to start the metrics recording by passing the parameters XX:StartFlightRecording
with options like duration and file name. When the application is generated the Java Flight Recorder metrics will be recorded into the myrecording.jfr
file.
We can use tools like JDK Mission Control to open and analyse the metrics from the .jfr file.
Summary
In this tutorial, we learned how to Enable Spring Boot ApplicationStartup metrics that we can use to profile the startup routine and diagnose the slow startup of Spring Boot Applications. We also learned that using BufferingApplicationStartup
we can enable the Spring Boot Actuator endpoint of /startup
, which provides useful metrics. Alternatively, we can use FlightRecodingApplicationStartup
to record the startup metrics using Java Flight Recorder.
Published at DZone with permission of Amit Phaltankar. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments