Learnitweb

Spring Boot Actuator

1. Introduction

We know that Spring Boot is an opinionated framework i.e. can be configured based on our definitions (‘starter’ dependencies we use). Spring Boot provides production ready features to monitor and manage your application by using Actuator module. In this tutorial, we’ll discuss Spring Boot Actuator. Spring Boot Actuator module allows you to manage and monitor your application by using HTTP endpoints or with JMX.

Following are few of the features which Actuator allows your do.

  • Allows you to know which Spring Boot beans are automatically registered.
  • Allows you to determine mapping of URL with controller. That is, which controller will be used to handle request URL.
  • Allows to get configuration parameters applied for the running application.
  • Allows you to get health of the application. You can get metrics of the application, such as memory and thread allocation etc.
  • Allows you to shutdown application.
  • Allows you to get session information.
  • Allows you to take thread dump.

Spring Boot provides various actuator endpoints which provide various production-ready features such as monitoring, metrics health checks etc.

2. Enabling Actuator

The recommended way to enable Actuator is to add dependency spring-boot-starter-actuator.

In a Maven based project:

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

3. Endpoints

Actuator endpoints allow you to monitor and manage your application. Spring Boot comes with many built-in endpoints. You can also create your own endpoint if you want.

Each actuator endpoint can be enabled or disabled. The endpoint is exposed over HTTP or JMX. To access an endpoint it must be available. An endpoint is said to be available only if it is enabled and exposed. The built-in endpoints can be configured only when they are available.

Following are few built-in endpoints:

EndpointDescription
auditeventsExposes audit events information for the current application.
beansDisplays a complete list of all the Spring beans in your application.
healthShows application health information.
infoDisplays application info.
loggersDisplays and modifies the configuration of loggers in the application.
mappingsDisplays @RequestMapping paths.
scheduledtasksDisplays the scheduled tasks in your application.
shutdownAllows you to gracefully shutdown your application. This endpoint is disabled by default.
startupDisplays startup information.
threaddumpAllows you to perform thread dump.

When an endpoint is exposed via HTTP, the endpoint format is /actuator/<endpoint>. For example, the /actuator/health.

4. Enabling Endpoints

To enable the endpoint, you can configure the property in application.properties or yaml file. The format of the property is management.endpoint.<id>.enabled. For example, to enable the shutdown endpoint:

management.endpoint.shutdown.enabled=true

By default, all endpoints except for shutdown are enabled.

If you want to disable all endpoints by default, you can set the management.endpoints.enabled-by-default property to false. If you choose to disable the endpoints by default then you have to enable each endpoint individually.

Disabled endpoints are removed entirely from the application context.

5. Exposing endpoints

You can include or exclude an endpoint for a particular technology (web or jmx). The format of the property is management.endpoints.<technology>.exposure.<include/exclude>

Following are few examples:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=health,info

management.endpoints.jmx.exposure.include=health,info

6. Working example of Endpoint

To see a working example of actuator endpoint, create a new Spring Boot web application and will add Actuator dependency to the project. So our project will have two main dependencies:

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

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

We have also created a HelloController, but it is not required to see Actuator working.

package com.learnitweb.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@GetMapping("/hello")
	public String index() {
		return "Hello world";
	}
}

Now, in application.properties include which endpoints you want to expose.

management.endpoints.web.exposure.include=health,mappings

Now, run the application and access the endpoint which you want to access. For example, the output of the http://localhost:8080/actuator/mappings endpoint is something like the following:

{
    "contexts": {
        "application": {
            "mappings": {
                "dispatcherServlets": {
                    "dispatcherServlet": [
                        {
                            "handler": "Actuator web endpoint 'mappings'",
                            "predicate": "{GET [/actuator/mappings], produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}",
                            "details": {
                                "handlerMethod": {
                                    "className": "org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping.OperationHandler",
                                    "name": "handle",
                                    "descriptor": "(Ljavax/servlet/http/HttpServletRequest;Ljava/util/Map;)Ljava/lang/Object;"
                                },
                                "requestMappingConditions": {
                                    "consumes": [],
                                    "headers": [],
                                    "methods": [
                                        "GET"
                                    ],
                                    "params": [],
                                    "patterns": [
                                        "/actuator/mappings"
                                    ],
                                    "produces": [
                                        {
                                            "mediaType": "application/vnd.spring-boot.actuator.v3+json",
                                            "negated": false
                                        },
                                        {
                                            "mediaType": "application/vnd.spring-boot.actuator.v2+json",
                                            "negated": false
                                        },
                                        {
                                            "mediaType": "application/json",
                                            "negated": false
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "handler": "com.learnitweb.hello.HelloController#index()",
                            "predicate": "{GET [/hello]}",
                            "details": {
                                "handlerMethod": {
                                    "className": "com.learnitweb.hello.HelloController",
                                    "name": "index",
                                    "descriptor": "()Ljava/lang/String;"
                                },
                                "requestMappingConditions": {
                                    "consumes": [],
                                    "headers": [],
                                    "methods": [
                                        "GET"
                                    ],
                                    "params": [],
                                    "patterns": [
                                        "/hello"
                                    ],
                                    "produces": []
                                }
                            }
                        },
                        {
                            "handler": "ResourceHttpRequestHandler [classpath [META-INF/resources/webjars/]]",
                            "predicate": "/webjars/**",
                            "details": null
                        },
                        {
                            "handler": "ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]",
                            "predicate": "/**",
                            "details": null
                        }
                    ]
                },
                "servlets": [
                    {
                        "mappings": [
                            "/"
                        ],
                        "name": "dispatcherServlet",
                        "className": "org.springframework.web.servlet.DispatcherServlet"
                    }
                ]
            },
            "parentId": null
        }
    }
}

This is not the complete output. Some part of output is removed.

7. Configuring endpoints for cache response

Endpoints which do not take any parameters cache responses to read operations. You can configure the amount of time for which an endpoint will cache a response. For example, the following configuration will allow endpoint to cache response for 20 seconds.

management.endpoint.beans.cache.time-to-live=20s

8. Conclusion

In this tutorial we discussed the actuator and how to make an actuator available. This is a brief introduction of actuator. We’ll continue discussion about Actuator in upcoming tutorials.