JavaSpring

Enable Spring Boot Application Startup Metrics to Diagnose Slow Startup

A quick guide to enable Spring Boot Application Startup (ApplicationStartup) Metrics to monitor in startup actuator endpoint and Java Flight Recorder. The ApplicationStartup Metrics helps to Diagnose Spring Boot Application that has a slow startup.

Overview

During an application startup process, Spring Boot performs a lot of work in the background. This work involves creating Spring Application Context, creating a various beans, auto-wiring and auto configuration of various components and finally, starting the application. When a Spring Boot Application has 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 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 & 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 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 provides a detailed and stepped down logs from component initialisation, bean instantiations and their dependencies linkages. Also, the metrics provide start and end time 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 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>
Code language: HTML, XML (xml)

or build.gradle

plugins { id 'org.springframework.boot' version '2.4.2' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' }
Code language: Gradle (gradle)

The SpringApplication class, which starts any Spring Boot Application, now gets setStartup method which takes an implementation of ApplicationStartup interface.

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; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication application = new SpringApplication(Application.class); application.setApplicationStartup(new BufferingApplicationStartup(10000)); application.run(args); } }
Code language: Java (java)

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.

public static void main(String[] args) { SpringApplication application = new SpringApplication(Application.class); application.setApplicationStartup(new BufferingApplicationStartup(10000)); application.run(args); }
Code language: Java (java)

We are specifying the buffer capacity of 10000. The application startup metrics are captured into a buffer made available in /startup endpoint of actuator.

Next, will enable the /startup endpoint in actuator properties.

In an application.yml file

management: endpoints: web: exposure: include: 'startup'
Code language: YAML (yaml)

Or, in an application.properties file

management.endpoints.web.exposure.include=startup
Code language: Properties (properties)

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'
Code language: Bash (bash)

Next, is a snippet of output which shows StudentRepository and StudentController beans and their creation start and end times.

[ { "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" }, ... ]
Code language: JSON / JSON with Comments (json)

It is important to note that although the /startup endpoint is returning a JSON, the endpoint follows 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.

public static void main(String[] args) { SpringApplication application = new SpringApplication(Application.class); application.setApplicationStartup(new FlightRecorderApplicationStartup()); application.run(args); }
Code language: Java (java)

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
Code language: Bash (bash)

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 tool 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 slow startup of Spring Boot Applications. We also learned that, using BufferingApplicationStartup, we can enable Spring Boot Actuator endpoint of /startup, which provides useful metrics. Alternatively we can use FlightRecodingApplicationStartup to record the startup metrics using Java Flight Recorder. More more on Spring & Spring Boot, please visit Spring Tutorials.


For full source code of the examples used here, please visit https://github.com/amitrp/spring-boot-crud-jpa-jpa.