DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Trending

  • Agentic AI Systems: Smarter Automation With LangChain and LangGraph
  • Proactive Security in Distributed Systems: A Developer’s Approach
  • How to Format Articles for DZone
  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  1. DZone
  2. Coding
  3. Frameworks
  4. Enable Spring Boot ApplicationStartup Metrics to Diagnose Slow Startup

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.

By 
Amit Phaltankar user avatar
Amit Phaltankar
·
Feb. 09, 21 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

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.

Java
 




xxxxxxxxxx
1
12


 
1
package com.amitph.spring.tutorials.students;
2
import org.springframework.boot.SpringApplication;
3
import org.springframework.boot.autoconfigure.SpringBootApplication;
4
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
5
@SpringBootApplication
6
public class Application {
7
    public static void main(String[] args) {
8
        SpringApplication application = new SpringApplication(Application.class);
9
        application.setApplicationStartup(new BufferingApplicationStartup(10000));
10
        application.run(args);
11
    }
12
}


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. 

Java
 




x


 
1
public static void main(String[] args) {
2
    SpringApplication application = new SpringApplication(Application.class);
3
    application.setApplicationStartup(
4
      new BufferingApplicationStartup(10000));
5
    application.run(args);
6
}


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:

YAML
 




xxxxxxxxxx
1


 
1
management:
2
  endpoints:
3
    web:
4
      exposure:
5
        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.

JSON
 




xxxxxxxxxx
1
32


 
1
            {
2
                "startupStep": {
3
                    "name": "spring.beans.instantiate",
4
                    "id": 123,
5
                    "parentId": 122,
6
                    "tags": [
7
                        {
8
                            "key": "beanName",
9
                            "value": "studentRepository"
10
                        }
11
                    ]
12
                },
13
                "startTime": "2021-01-26T06:38:09.234585991Z",
14
                "endTime": "2021-01-26T06:38:09.380445297Z",
15
                "duration": "PT0.145859306S"
16
            },
17
            {
18
                "startupStep": {
19
                    "name": "spring.beans.instantiate",
20
                    "id": 122,
21
                    "parentId": 5,
22
                    "tags": [
23
                        {
24
                            "key": "beanName",
25
                            "value": "studentsController"
26
                        }
27
                    ]
28
                },
29
                "startTime": "2021-01-26T06:38:09.231829732Z",
30
                "endTime": "2021-01-26T06:38:09.382129262Z",
31
                "duration": "PT0.15029953S"
32
            },



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.

Java
 




x


 
1
public static void main(String[] args) {
2
    SpringApplication application = new SpringApplication(Application.class);
3
    application.setApplicationStartup(new FlightRecorderApplicationStartup());
4
    application.run(args);
5
}



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.

Spring Framework Spring Boot Metric (unit) application

Published at DZone with permission of Amit Phaltankar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!