Actuators in Spring Boot

It is really important to be able to monitor various metrics of a production application. One way to achieve this is to use the Actuators in Spring Boot.

1. Configuration

Like many other things, the configuration of Spring Boot Actuators is really simple. Just add the below dependency in your application.

Gradle

[code language=”sql”]
compile group: ‘org.springframework.boot’, name: ‘spring-boot-starter-actuator’, version: ‘1.5.1.RELEASE’
[/code]

Maven

[code language=”xml”]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
[/code]

2. Some default Endpoints

Once configured, you can directly access the default endpoints provided by Spring. There are many such endpoints, we will discuss some of them in this post. Please have a look at the official docs to learn more.

3. The /info endpoint

The /info endpoint is meant to display some abstract information about the application. By default, it won’t have any information. So, let’s configure our application to provide some information about our application.

Add the below properties in your application.properties file.

[code language=”sql”]
info.app.name=Actuators sample
info.app.description=Demonstrates the use and configuration of Actuator endpoints
info.app.version=1.0.0
[/code]

Here, we are customizing the info endpoint. We are adding some properties to our info endpoint that provides some information about the application. Now, hit the /info endpoint and you should get something like below.

[code language=”javascript”]
{
“app”:{
“version”:“1.0.0”,
“description”:“Demonstrates the use and configuration of Actuator endpoints”,
“name”:“Actuators sample”
}
}
[/code]

Congratulations! You have just customized your first endpoint.

4. The /health endpoint

The /health endpoint is meant to perform a health check of your application. This endpoint might be utilized by some load balancer to monitor the health of an application. Hit the /health endpoint and you will get the below output.

[code language=”javascript”]
{
“status”:“UP”,
“diskSpace”:{
“status”:“UP”,
“total”:107374178304,
“free”:61373607936,
“threshold”:10485760
},
“db”:{
“status”:“UP”,
“database”:“MySQL”,
“hello”:1
}
}
[/code]

Let us move ahead and customize the health checking logic. This can be achieved by implementing the HealthIndicator interface.

[code language=”java”]
@Component
public class HealthChecker implements HealthIndicator {
@Override
public Health health() {
if (isApplicationHealthy()) {
return Health.up().build();
}
return Health.down().status(Status.OUT_OF_SERVICE).build();
}

public boolean isApplicationHealthy() {
// Your logic to check health
// might include DB calls
return false;
}
}
[/code]

Note that I am explicitly failing the health check just to demonstrate the difference in response.

[code language=”javascript”]
{
“status”:”OUT_OF_SERVICE”,
“healthChecker”:{
“status”:”OUT_OF_SERVICE”
},
“diskSpace”:{
“status”:”UP”,
“total”:107374178304,
“free”:61369597952,
“threshold”:10485760
},
“db”:{
“status”:”UP”,
“database”:”MySQL”,
“hello”:1
}
}
[/code]

As you can see, the status for “healthChecker” is “OUT_OF_SERVICE”. Of course, this is really a bare-bones implementation. However, this opens a lot of possibilities to perform the health check of your application.

 

5. The /metrics endpoint

This is a really important endpoint. This gives a lot of low-level details about your application.

[code language=”javascript”]
{
"mem":357143,
"mem.free":143358,
"processors":4,
"instance.uptime":54268,
"uptime":67081,
"systemload.average":-1,
"heap.committed":286208,
"heap.init":94208,
"heap.used":142849,
"heap":1323520,
"nonheap.committed":73152,
"nonheap.init":2496,
"nonheap.used":70935,
"nonheap":0,
"threads.peak":24,
"threads.daemon":22,
"threads.totalStarted":28,
"threads":24,
"classes":9568,
"classes.loaded":9568,
"classes.unloaded":0,
"gc.ps_scavenge.count":11,
"gc.ps_scavenge.time":224,
"gc.ps_marksweep.count":2,
"gc.ps_marksweep.time":296,
"httpsessions.max":-1,
"httpsessions.active":0,
"datasource.primary.active":0,
"datasource.primary.usage":0,
"gauge.response.health":222,
"gauge.response.metrics":26,
"gauge.response.star-star":36,
"gauge.response.api.users":21,
"counter.status.200.metrics":1,
"counter.status.200.api.users":2,
"counter.status.200.health":1,
"counter.status.404.star-star":3
}
[/code]

As you can see, the details regarding the heap, threads, classes etc. can be found here. At the bottom, there are several counters associated with various status codes as well.

Now let us add our own custom metric to the above list.

[code language=”java”]
@RestController
@RequestMapping("/api")
public class UserRestController {

@Autowired
UserService userService;

@Autowired
CounterService counterService;

@RequestMapping(value = "/users", method = RequestMethod.POST)
public ResponseEntity<User> createUser(@RequestBody User user) {
HttpStatus status = HttpStatus.CREATED;
User saved = userService.save(user);
return new ResponseEntity<>(saved, status);
}

@RequestMapping(value = "/users", method = RequestMethod.GET)
public List<User> getAllUsers() {
counterService.increment("counter.get.users");
return userService.getAllUsers();
}

}
[/code]

Observe that we are using Spring’s CounterService class for our requirement. We are incrementing the counter for every get request and setting a new metric “counter.get.users”. Once done, hit the /api/users endpoint several times and check the /metrics endpoint again. You will see the below metric added to the /metric endpoint

[code language=”javascript”]
"counter.get.users":2
[/code]

6. Conclusion

We have seen that Spring provides easy to configure and easy to use actuator endpoints. The Actuators in Spring Boot provides the details that can be used to monitor the performance of the web application, identify bottlenecks, and improve the throughput of the application. There are many other endpoints which we have not discussed. For example, check out the /dump endpoint that provides the thread dump of the application. Get the source code here.

2 thoughts on “Actuators in Spring Boot

Leave a Reply

Your email address will not be published. Required fields are marked *