Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Magic With the Spring Boot Actuator

DZone's Guide to

Magic With the Spring Boot Actuator

Set sail for a magical journey with the Spring Boot actuator.

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

Spring Boot provides the Spring Boot actuator module for monitoring and managing your application when it is moved into production. Some of the production-ready features it provides are health monitoring of the application, auditing of the events, and gathering of metrics from the production environments.

For enabling the Spring Boot actuator, we need to add the following Spring Boot starter Maven dependency in pom.xml.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>


If you are using Gradle, then add the below dependency in the build.gradle:

dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
}


Magic #1: the Base Path Can Be Changed

The actuator provides various built-in endpoints, and it also lets us add our own. The ID of the endpoint, along with a prefix of /actuator, is mapped to the URL. For example, the /info   endpoint will be mapped to  /actuator/info.  

The /actuator base path can be changed by configuring the management.endpoints.web.base-path property in the application.properties. For example: 

management.endpoints.web.base-path=/mypath


The above property changes the endpoint URL from /actuator/{ID} to  /mypath/{ID}. For example, the health endpoint will become /mypath/health.

Magic #2: Health of the Application

If we want to know the health of an application, we can use the health endpoint. For getting the health information, we just need to make a GET request to /actuator/health, as shown below.

$ curl 'http://localhost:8080/actuator/health' -i -X GET


The resulting response, by default, will be:

{
  "status" : "UP"
}


If we want to get the complete details of the health of the application, we can add the following property in application.properties.

management.endpoint.health.show-details=always


The resulting response will be as shown below. If your application has a database like MongoDB, Redis, or MySQL, for instance, the health endpoint will show the status of those, which can also be seen below ( this is an example for Mongo):

{
   "status":"UP",
   "details":{
      "db":{
         "status":"UP",
         "details":{
            "database":"MONGO",
            "message": "Hello I am UP"
         }
      },
      "diskSpace":{
         "status":"UP",
         "details":{
            "total":250790436864,
            "free":100330897408,
            "threshold":10485760
         }
      }
   }
}


Magic #3: Custom Health Indicators

The actuator provides a HealthIndicator and AbstractHealthIndicator interface, which we can implement to provide custom health indicators, as shown below.

@Component
public class MyHealthIndicator extends AbstractHealthIndicator {

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // Use the builder to build the health status details that should be reported.
        // If you throw an exception, the status will be DOWN with the exception message.

        builder.up()
                .withDetail("app", "I am Alive")
                .withDetail("error", "Nothing!");
    }
}


Now, our health endpoint is shown below.

{
   "status":"UP",
   "details":{
      "custom":{
         "status":"UP",
         "details":{
            "app":"I am Alive",
            "error":"Nothing!"
         }
      },
      "diskSpace":{
         "status":"UP",
         "details":{
            "total":250790436864,
            "free":97949245440,
            "threshold":10485760
         }
      }
   }
}


Magic #4: Application Information With Info

The Spring Boot actuator provides the /info endpoint to display the information about the application. This is useful if you want to learn more about the version, name, and some other basic properties of the application. We can also display the Java version that your application uses. It takes the information from the Maven pom.xml file. Suppose you have the information in pom.xml, as given below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Rahul_DEV</groupId>
<artifactId>rahul_logging</artifactId>
<packaging>jar</packaging>
<name>rahul_logging</name>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<springfox-version>2.5.0</springfox-version>
</properties>


To expose the information using  /info, we can add the following properties in the application.properties file.

# INFO ENDPOINT CONFIGURATION
info.app.name=@project.name@
info.app.version=@project.version@
info.app.encoding=@project.build.sourceEncoding@
info.app.java.version=@java.version@


We will get the following information at the http://localhost:8080/actuator/info   endpoint.

{
    "app": {
        "name": "rahul_logging",
        "version": "1.0.0",
        "encoding": "UTF-8",
        "java": {
            "version": "1.8"
        }
}


Magic #5: Metrics About the Application Environment

The application level metrics are exposed via the /metrics  actuator endpoint. This is useful if you want to the know the OS and JVM information of the application running in an environement. The information is as follows when we hit the application atlocalhost:8080/actuator/metrics:

{
    "names": [
        "jvm.memory.max",
        "jvm.threads.states",
        "process.files.max",
        "jvm.gc.memory.promoted",
        "tomcat.cache.hit",
        "tomcat.servlet.error",
        "system.load.average.1m",
        "tomcat.cache.access",
        "jvm.memory.used",
        "jvm.gc.max.data.size",
        "jvm.gc.pause",
        "jvm.memory.committed",
        "system.cpu.count",
        "logback.events",
        "tomcat.global.sent",
        "jvm.buffer.memory.used",
        "tomcat.sessions.created",
        "jvm.threads.daemon",
        "system.cpu.usage",
        "jvm.gc.memory.allocated",
        "tomcat.global.request.max",
        "tomcat.global.request",
        "tomcat.sessions.expired",
        "jvm.threads.live",
        "jvm.threads.peak",
        "tomcat.global.received",
        "process.uptime",
        "http.client.requests",
        "tomcat.sessions.rejected",
        "process.cpu.usage",
        "tomcat.threads.config.max",
        "jvm.classes.loaded",
        "http.server.requests",
        "jvm.classes.unloaded",
        "tomcat.global.error",
        "tomcat.sessions.active.current",
        "tomcat.sessions.alive.max",
        "jvm.gc.live.data.size",
        "tomcat.servlet.request.max",
        "tomcat.threads.current",
        "tomcat.servlet.request",
        "process.files.open",
        "jvm.buffer.count",
        "jvm.buffer.total.capacity",
        "tomcat.sessions.active.max",
        "tomcat.threads.busy",
        "process.start.time"
    ]
}


If we want to look for a specific metric from the above result, actuator provides the information. For example, if want to see the JVM heap memory used, we can check at the endpoint  http://localhost:8080/actuator/metrics/jvm.memory.used. Then, it will display the following information:

{
    "name": "jvm.memory.used",
    "description": "The amount of used memory",
    "baseUnit": "bytes",
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 170223544
        }
    ],
    "availableTags": [
        {
            "tag": "area",
            "values": [
                "heap",
                "nonheap"
            ]
        },
        {
            "tag": "id",
            "values": [
                "Compressed Class Space",
                "PS Survivor Space",
                "PS Old Gen",
                "Metaspace",
                "PS Eden Space",
                "Code Cache"
            ]
        }
    ]
}


Conclusion

In this post, we looked at various features of the Spring Boot actuator, which provides us production-grade information like health, info, and other metrics. We have just scraped the surface of what the actuator will do for us. For further research and information, check the Spring Boot actuator official documentation mentioned in the reference.

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.

Topics:
spring boot 2.0 ,spring actuator ,spring boot ,actuator ,java ,tutorial ,json ,shell ,code ,example

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}