A Spring Boot sub-module, i.e., Spring Boot Actuator, offers significant advantages with minimal setup work. Developers and operations teams can see and control the behaviour of the application more effectively by integrating it. As a result, problems are easier to identify, diagnose, and fix before they become more serious, guaranteeing a more dependable and seamless user experience.
For every production-grade web application built with the Spring framework, the Spring Boot Actuator requirement must be given top priority.
Spring Boot Actuator provides a set of built-in endpoints that allow you to monitor and manage your Spring Boot application. These endpoints offer various insights into your application's health, metrics, and other operational data. Actuator endpoints can be accessed in two primary ways: HTTP and JMX. With HTTP, you can easily interact with the endpoints using a web browser or tools like cURL and Postman, making it convenient to check aspects such as your app's health or performance metrics. On the other hand, JMX is useful for Java environments, enabling integration with existing monitoring tools to delve deeper into the application's performance and configurations.
In this article, we will focus on using HTTP to interact with Spring Boot Actuator endpoints.
Spring Boot Actuator provides a range of features to monitor and manage production-grade applications. Here are the key functionalities it offers:
Actuator exposes various built-in endpoints that provide insights into different aspects of the application:
Most applications use HTTP endpoints, where the endpoint’s ID, prefixed with <span class="pink">/actuator</span>
, is mapped to the base URL of the application. This means that you can access various management and monitoring features by appending the appropriate endpoint path to the base URL of your application, starting with <span class="pink">/actuator</span>
. For example, if your base URL is <span class="pink">http://localhost:8080</span>
, then the health endpoint would be accessible at <span class="pink">http://localhost:8080/actuator/health</span>.
ConfigurableEnvironment</span>
, helping with debugging configuration issues.@RequestMapping</span>
paths in the application, assisting in understanding available API endpoints.By default, Spring Boot Actuator provides only the <span class="pink">/health</span>
endpoint. To expose additional endpoints, such as <span class="pink">/metrics</span>
, <span class="pink">/beans</span>
, and others, you need to explicitly enable them in your configuration.
💡 Custom Endpoints: It should be noted that we can design unique endpoints based on the needs of the application.
Metrics are measurements that help you understand how your application is performing. With Spring Boot Actuator, you can track different kinds of metrics to keep an eye on your application's health:
When you track and log significant events and actions in your program, including user logins, data modifications, or access to private areas, you are doing auditing.
For instance:
💡 Spring Boot Actuator does not provide built-in auditing features directly. However, it can be integrated with other frameworks and tools, such as Spring Security for tracking security-related events, Spring Data JPA for auditing database operations, and custom solutions for more specific auditing needs. These integrations help in capturing and managing detailed records of application activities and changes.
Actuator allows you to access and manage application configuration details, such as environment properties and configuration properties, to help with debugging and ensuring proper setup. Actuator endpoi$nts can be secured to restrict access to sensitive information and operations. You can configure access control using Spring Security to ensure that only authorised users can access management features.
Note: In the following section of the article, we will go over these functionalities in more depth using code.
This section will explain how to integrate Actuator into a Spring Boot-created RESTful web service and set significant endpoints, as well as how to use Actuator's features to obtain crucial insights into the performance, metrics, and health of your application. A Maven project will be utilised.
Visit start.spring.io to generate a Maven project with the following dependencies:
Because Spring Boot Actuator is designed for managing and monitoring web applications, we've included the Spring Web dependency in our project. You can use any version of Java 17 or newer, as Spring Boot 3 requires at least Java 17.
Spring Boot actuator dependency.
As soon as we start the application without any additional code or configuration, the console displays "Exposing one endpoint beneath base path/actuator." This message indicates that the <span class="pink">/actuator/health</span> endpoint, which provides basic health information about the application, is by default enabled.
accessing the endpoint: http://localhost:8080/actuator
By default, you will only get these end points, and these end points are hyperlinks.
In order to obtain endpoints only associated with metrics, info or other related data, it is necessary to manually specify them in the application.properties file.
After adding <span class="pink">management.endpoints.web.exposure.include=*</span>
to the <span class="pink">application.properties</span>
file and accessing the endpoint at <span class="pink">http://localhost:8080/actuator</span>
, you will see all the available Actuator endpoints on a hypermedia web page.
But don’t you worry. no one is going to use all these end points in an application. only a handful, so enable only what is required for your project.
“status”: “UP” means the application is healthy and running smoothly without any issues.
To view detailed health information about your application, you have to add an extra configuration in your application.properties file, i.e.,
<span class="pink">management.endpoint.health.show-details=always</span>
explanation:
Now, when we access <span class="pink">/actuator/health</span>
endpoint, we get a detailed health report about our application. The <span class="pink">"status": "UP"</span>
at the top level signifies that the application is currently running smoothly without any critical issues. The <span class="pink">"components"</span>
field contains detailed information about various subsystems:
<span class="pink">"status": "UP"</span>
: The disk space is sufficient and accessible.<span class="pink">"total"</span>
: The total disk space available (203,286,376,448 bytes).<span class="pink">"free"</span>
: The available free space (197,754,081,280 bytes).<span class="pink">"threshold"</span>
: The minimum amount of free space required (10,485,760 bytes).<span class="pink">"path"</span>
: The disk path being monitored ("D:\\actuatorDemo\\.").<span class="pink">"exists"</span>
: Confirms that the specified disk path exists."status": "UP"</span>
indicating responsiveness.
This endpoint shows all the beans currently present in the Spring ApplicationContext, providing detailed information about each bean, including its name, type, scope, and the dependencies it relies on.
We'll create a component class, retrieve it as a bean from the application context, and then examine its details.
CustomBean.java
retrieving the bean of the CustomBean class from the application context:
explanation: In this code, we retrieve the CustomBean from the ApplicationContext using getBean() method.
When we access the bean endpoint at <span class="pink">http://localhost:8080/actuator/beans</span>, we can find our custom bean listed there.
explanation:
This output shows:
The <span class="pink">actuator/info</span>
endpoint provides custom information about the Spring Boot application. It can display details like the app's version, description, and build information. This endpoint helps users quickly understand key aspects of the application's deployment and versioning.
At first, by default, accessing the info endpoint:
we have to configure a little our application.properties file to get the desired information:
explanation:
This setup adds details like the app's name, version, description, author, and system information such as Java or operating system info to the <span class="pink">/info</span>
endpoint in Spring Boot.
Now accessing <span class="pink">http://localhost:8080/actuator/info</span>
:
It provides information on my computer's Java setup, the operating system, the app name, version, description, author, and more.
The <span class="pink">/actuator/mappings</span>
endpoint shows all the different web addresses your app can respond to and which parts of your code handle them. It's like a list that tells you, "If someone goes to this URL, this is the part of the app that gets triggered."
We will do some hands-on coding and create a rest controller in our project’s base package named :
AcutatorController.java
Along with our custom mapping, such as <span class="pink">api/greeting</span>
, this URL, <span class="pink">http://localhost:8080/actuator/mappings</span>
, displays a number of default actuator mappings.
The api information, namely <span class="pink">/api/greeting</span>
, is displayed in full in the image below. It’s very helpful to debug when you are getting an error in any API; you can check the full details of that api through mappings.
The <span class="pink">/actuator/metrics</span>
endpoint in Spring Boot Actuator offers a tonne of valuable insights into your application's performance and resource utilisation. Imagine it as a dashboard that provides you with a comprehensive overview of different metrics, including memory usage, CPU load, and request statistics.
When you access this endpoint, you'll see a list of available metrics, like <span class="pink">jvm.memory.used</span> or <span class="pink">system.cpu.usage</span>. Each metric is like a sensor that tells you about a specific aspect of your application's environment. For example, <span class="pink">jvm.memory.used</span> indicates how much memory your Java Virtual Machine (JVM) is currently using, while <span class="pink">system.cpu.usage</span> shows the percentage of CPU being used by the system.
These metrics are incredibly useful for monitoring and diagnosing issues. If your application starts slowing down, you can check the <span class="pink">/actuator/metrics</span> endpoint to see if you're running low on memory or if the CPU is being overutilised. This helps you make informed decisions about scaling your application or optimising your code.
Accessing the metrics endpoint at: <span class="pink">http://localhost:8080/actuator/metrics</span>
explanation:
This is a list of key metrics you can check using Spring Boot Actuator's <span class="pink">/actuator/metrics</span>
endpoint. Here's a quick rundown:
application.ready.time</span>
and <span class="pink">application.started.time</span>
shows how long it takes for your app to get going.disk.free
and disk.total</span>
tell you how much disk space you have left and overall.executor.active
, executor.pool.size</span>
, and <span class="pink">executor.queue.remaining</span>
show how busy your thread pools are.jvm.memory.used</span>
), garbage collection data (<span class="pink">jvm.gc.memory.allocated</span>
), and thread counts (<span class="pink">jvm.threads.live</span>
).logback.events</span>
counts the log events.process.cpu.time</span>
and <span class="pink">process.cpu.usage</span>
show how much CPU your app is using, plus process uptime.system.cpu.count</span>
and <span class="pink">system.cpu.usage</span>
give an overview of your CPU's capacity and usage.tomcat.sessions.active.current</span>
and <span class="pink">tomcat.sessions.created</span>
track session activity on the Tomcat server.example:
You can access any of these metrics directly. For instance, if you want to check the live JVM threads, simply hit the URL: <span class="pink">http://localhost:8080/actuator/metrics/jvm.threads.live</span>
.
explanation:
The metric <span class="pink">jvm.threads.live</span>
indicates that there are currently 25 live threads, including both daemon and non-daemon threads, in the JVM.
Note: Just a heads up: we have got a bunch of Actuator endpoints that give you all sorts of cool info for managing and monitoring your app. We can not go over all of them here, but feel free to check them out on your own to get a better grasp.Once you finish reading this article, you will be all set to use Actuator like a pro.
You can use the built-in ends that come with the Spring Boot Actuator, but sometimes you need something different. You might want to share the most recent statistics from your app or show what is going on with a custom file.
To make a custom endpoint, you must first define a class that is marked with <span class="pink">@Endpoint</span>
. Then, based on what you want your endpoint to do (read data, change state, or delete something), you will add methods that are marked with <span class="pink">@ReadOperation</span>
, <span class="pink">@WriteOperation</span>
, or <span class="pink">@DeleteOperation</span>
.
For example, a simple read-only (@ReadOperation) endpoint might look like this:
CustomEndPoint.java
explanation:
We’ve set up a custom Actuator endpoint named <span class="pink">customEndpoint</span>
to manage and view app data.
Read Operation: The <span class="pink">customInfo()</span>
method is triggered when you access <span class="pink">/actuator/customEndpoint</span>
through the browser, make a GET request from Postman or any other tool.
So, accessing <span class="pink">http://localhost:8080/actuator/customEndpoint</span>
will display a status message.
Similarly, for write operations (@WriteOperation), we have implemented a method:
To write data using Postman:
http://localhost:8080/actuator/customEndpoint</span>
.Content-Type</span>
to <span class="pink">application/json</span>
.This setup allows you to add new data to your application through the custom endpoint.
When you access <span class="pink">http://localhost:8080/actuator/customEndpoint</span> through the browser, you'll see the data that was previously added, along with a status message.
Spring Boot Actuator endpoints provide valuable insights into the internals of our application, offering crucial information for monitoring, management, and troubleshooting. However, this wealth of information can be a double-edged sword. If left unsecured, these endpoints could potentially expose sensitive data or provide attackers with critical information about the system and application.
Step 1: Begin by adding the Spring Security dependency to the <span class="pink">pom.xml</span> file.
Step 2: Create a <span class="pink">config</span> package and, within it, add a <span class="pink">SecurityConfig</span> class. This class will be used to define our security configuration.
<span class="pink">SecurityConfig.java</span>
explanation:
The securityFilterChain method configures Spring Security for a web application. It:
/actuator/**</span>
" endpoints
Step3: setting username and password in <span class="pink">application.properties</span> file:
Note: This is not the standard method for setting up usernames and passwords. Implementing a fully customizable user registration and login system is a complex topic that requires more in-depth discussion, which we won't cover here. Our primary focus is on securing the Actuator endpoints.Spring Security is a highly customizable module, so you can tailor the security configurations as needed.
Demo: Testing our secured endpoint
Spring Boot Actuator is great for basic monitoring, but integrating it with tools like Prometheus and Grafana takes things to the next level. These external tools offer more advanced features that developers and ops teams really need.
With Prometheus and Grafana, you get deeper insights into your app's performance. They show you detailed metrics over time, not just snapshots. This helps you spot trends and potential issues before they become real problems.
These tools also make it easier to visualize data. Instead of raw numbers, you get graphs and charts that quickly show what's going on. This is super helpful when you're trying to figure out why your app might be slowing down or using too much memory.
Another big plus is the alerting system. You can set up alerts to ping you when something's not right, so you don't have to constantly watch your app.
For teams running multiple services, these tools are a game-changer. You can monitor all your apps from one place, which saves a ton of time and makes it easier to see how different parts of your system are working together.
💡 We'll delve into integrating Prometheus, Grafana, and other tools in separate articles, as this topic is too extensive to cover comprehensively in a single piece. While Spring Boot Actuator is a broad subject and can't be fully covered in one article, this piece should be more than sufficient to get you started and running. If you find any aspects particularly interesting, feel free to explore them further.
Cheers!
Happy Coding.