Monitoring APIs With Actuators

Introduction

In this article, we will inspect actuators, although the actuator concept came to light because of SpringBoot. It’s a sub-project of the SpringBoot framework, but the fundamentals can be adopted in other languages and frameworks as well. Python has adopted it and developed a library called ‘Pyctuator’. In the article, we will cover,

  1. What are Actuators
  2. Examples of Actuators
  3. Details about them

What are Actuators

Actuators are endpoints that help in monitoring the application, in short, it allows us to see what’s going on in the running application. With actuators, we will be able to check,

  1. Applications Health status
  2. Metrics
  3. Auditing
  4. Caching details
  5. heap dump
  6. environment details
  7. thread dump
  8. event monitoring and alerting.

 All the above points are extremely helpful especially in Microservices architecture, let’s understand by a few practical scenarios.

  1. Imagine a service is running on any environment that is not performing, by simply Invoking endpoint /dump the entire thread activity will be available to us like deadlock detection, system resource detection, etc. for better analyzing the problem.
  2. Consider a REST service it has an in-memory cache of Objects, we can always invoke /cache endpoint to monitor what’s going on with our cache internally
  3. With /env endpoint, we can better analyze the environmental details like a port on which the service is running, JVM version, system environment details.
  4. If any endpoint is taking a lot of time to respond, then with the help of the endpoint ‘/prometheus’ endpoint we can get the information of how much time the endpoint has taken to respond.

Let’s explore some of the actuator endpoints with a sample SpringBoot service. We will explore four actuator endpoints,

  1. /actuator/health
  2. /actuator/env
  3. /actuator/prometheus
  4. /actuator/heapdump

SpringBoot provides these actuator endpoints out of the box, all we need to do is to import an actuator library and enable the actuators we want.

.gradle 

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

.properties

Management.endpoints.web.exposure.include=health,info,dump,metrics,env,prometheus,heapdump

/actuator/health

The health endpoint provides information about the health of an application. Since the service is running on my local, the endpoint is ‘http://localhost:8080/actuator/health, which results in

This indicates that the service is up and running and there are no issues observed, if any component is down or not behaving properly the status endpoint changes,

/actuator/env

The /env endpoint provides the details about the environment of the application, it returns environmental details in form of a list that contains properties as JSON.

These sub-properties can further be looked into,

/actuator/prometheus

Prometheus is used for getting metrics, there are many variations like ‘micrometer’, ‘new relic, in our example we have used a micrometer, which can help in taking the measurements of the application. In one of the endpoint, I have added a delay within the method

private List<String> getItemList(){
    return List.of("Phone", "TV", "Car");
}
@GetMapping(path = "/items")
@Timed(value="items", description = "Time Taken to Return Items")
public List<String> getItems() throws InterruptedException{
    Thread.sleep(2000);
    return getItemList();
}

With the endpoint, we can see how much time the endpoint has taken to respond,

Upon searching for a String ‘Time Taken to Return Items’, but for the below metrics to generate, we need to first invoke the endpoint ‘http://localhost:8080/items’.

/actuator/heapdump

This endpoint retrieves the heap dump of the JVM on which the application is running, upon executing the endpoint http://localhost:8080/actuator/heapdump, the heapdump is downloaded. Upon importing the heapdump to any memory analyzer, I am using Eclipse MAT.

The heapdump file tries to provide all kinds of Leak suspects. Wonderful.

Summary

Actuators are extremely useful in Production environments; I keep using them to analyze them periodically. A lot can be written around it and it can be automated as well, to check the health status etc. If the framework you are using doesn’t supports it, I recommend to write your own.


Similar Articles