Lesson 2: Actuators in Boot

Note: There is an Upgrade Notes section below for this video.

1. Goals

In this lesson, we'll focus on a core feature of Spring Boot - actuators.


2. Lesson Notes

The relevant module you need to import when you're starting with this lesson is: m4-actuators-in-boot-start

If you want have a look at the fully implemented lesson, as a reference, feel free to import: m4-actuators-in-boot-end


2.1. Actuators

Simply put, actuators are monitoring tools. They bring production ready features into our app for very low effort.

More specifically, they provide various endpoints mainly exposed via HTTP (but also JMX) which basically help in monitoring and, to some extent, managing our application.


2.2. Actuators in Spring Boot

Through actuators, Spring Boot provides these built-in endpoints focused on auditing, health checks, and displaying metrics information.

The best way to enable actuators is to add the spring-boot-starter-actuator dependency:

Boot provides a number of built-in endpoints, well documented in the official reference.

Out of these, two endpoints are enabled by default: the /health and /info endpoints.


2.3. The /health Endpoint

Let’s run the application and have a look at the /health endpoint first. We can open up the browser and access this at: http://localhost:8080/actuator/health.

Naturally, the health endpoint shows information about the health of the application:

{"status":"UP"}

The default output is, of course, the bare minimum - just enough to know our application is up and running.


2.4. The /info Endpoint

Let’s hit the /info endpoint: http://localhost:8080/actuator/info

This endpoint shows information about our application. At this point, we haven’t configured or defined any, so the endpoint by default, won’t contain any data:

{}

Of course, the format is JSON as we expect.

Populating the response of this endpoint is quite easy, as everything is configurable via properties.

Let’s add some info about our app name and description:

Now, when we hit the info endpoint, we’ll see:

Next, let’s do some simple configuration of the actuators.


2.5. Actuators Context Path

By default, all actuators are available at /actuators/{endpoint_name} path. This can be easily configured via properties.

Let’s open up application.properties and change the base path of all actuators:

Now all endpoints will be available at: /monitoring/{endpoint_name}.

We can further change paths of specific actuators also. For example, if we want to make our info actuator available at /monitoring/information, we’ll do:

This will make our info available at /monitoring/information.


2.6. Boot Highlight

Actuators are a purely Spring Boot specific functionality that we can use out-of-the-box. We can reproduce this functionality in a plain Spring application by designing and implementing similar APIs.


2.7. The /loggers Endpoint (extra)

Spring Boot Actuator also exposes a /loggers endpoint which allows us to view and configure logging levels of our app at runtime.

Since this endpoint is not enabled by default, we can easily enable it by including it in the list of enabled endpoints in the application.properties:

On enabling, we can hit the /actuator/loggers endpoint and view the entire list of loggers and their configuration.

Since we have changed the actuator context path to /monitoring in our app, we will be accessing the loggers at: http://localhost:8080/monitoring/loggers:

We can also configure the logging level for individual loggers at runtime by hitting the POST /loggers/{{logger}} request.

For example, we can set the ROOT logging level to DEBUG in our app by hitting the endpoint:

POST http://localhost:8080/monitoring/loggers/ROOT

with the payload:

Now our app will start logging at debug level.

This can be very useful when troubleshooting issues without having to restart our app.


2.8. Upgrade Notes

Note that since Spring Boot 2.5, the actuator's "info" HTTP endpoint is not enabled by default anymore. That means we now have to explicitly include it in the list of enabled endpoints property (management.endpoints.web.exposure.include) as we've shown in this lesson if we want to use it.

Additionally, since version 2.6 the "info" endpoint doesn't retrieve the "info." prefixed properties from the Spring Environment by default (as the info.lsapp.name and info.lsapp.description ones we've defined above). To enable this, we have to add the following application property in application.properties file:

LS - M4 - L2.pdf
Complete and Continue