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 Diagnose Spring Boot Application that has a slow startup.

Overview

Spring Boot performs a lot of work in the background during an application startup process. 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 initialize and slowing down the entire process.

Profiling the Spring Boot application doesn’t often help in diagnosing startup issues. This is because several beans are getting initialized, and it isn’t easy to figure out which ones are causing the latency. Spring Boot Application Startup Metrics are useful for such cases.

This tutorial explains how to 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 tough to identify the root cause of a slow startup. However, with Spring Boot 2.4.0, we get an opportunity to generate 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 have been a part of Spring Framework since version 5.3. The startup metrics provide detailed and stepped-down logs from component initialization, 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 are divided into multiple Steps, represented by the StartupStep interface. Each of these steps has a unique Id, name of the step, and 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

To enable the ApplicationStartup metrics, we must use Spring Boot version 2.4.0 or higher.

pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.7.5</version>
  <relativePath/>
</parent>Code language: HTML, XML (xml)

or build.gradle

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

The SpringApplication class, which starts any Spring Boot Application, now gets the setApplicationStartup() method which takes an instance of the 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) {
    pringApplication 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 the applicationStartup() on the application and pass 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 the ‘/startup‘ endpoint in Spring Boot Actuator. To do that, we need to plugin Buffering Application Startup and enable the ‘/startup‘ endpoint.

The first step is to plug in 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 a buffer capacity of 10000. The application startup metrics are captured into a buffer made available in the /startup endpoint of the actuator.

Next, it 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=startupCode language: Properties (properties)

Once enabled, we need to start the actuator and execute a POST request on the ‘/actuator/startup‘ endpoint.

curl --location --request \
  POST 'http://localhost:8080/actuator/startup'Code language: Bash (bash)

The following output snippet shows the StudentRepository, the StudentController, 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 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 analyze them.

To view ApplicationStartup metrics with Java Flight Recorder, we need to use the 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 done, we need to package our application into a JAR.

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

Here, we are enabling the flight recorder using ‘-XX:+FlightRecorder‘ argument. Next, we instruct it 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 in the ‘myrecording.jfr‘ file.

We can use tools like JDK Mission Control to open and analyze 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 the BufferingApplicationStartup can enable Spring Boot Actuator’s ‘/startup‘ endpoint, which provides useful metrics. Alternatively, we can use FlightRecodingApplicationStartup to record the startup metrics using Java Flight Recorder. For more on Spring & Spring Boot, please visit Spring Tutorials.

For the complete source code of the examples used here, please visit our GitHub Repository.