DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Magic With the Spring Boot Actuator

Magic With the Spring Boot Actuator

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

Rahul Lokurte user avatar by
Rahul Lokurte
·
Jan. 28, 19 · Presentation
Like (54)
Save
Tweet
Share
85.05K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Framework Spring Boot application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Choose the Right Streaming Database
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Introduction to Spring Cloud Kubernetes
  • REST vs. Messaging for Microservices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: