SpringTechnology

Actuators in old Spring Boot (1.x)

A quick guide for you to Old Spring Boot 1.x actuators. You will learn the endpoints and configurations. Additionally, you will also learn writing Custom endpoints with an end-to-end Spring Boot Application.

Note:
This tutorial is about Old Spring Boot (1.x) Endpoints.
Otherwise, If you use Spring Boot 2.x, please visit: Spring Boot Actuator Endpoint with Spring Boot 2.

Enable Endpoints

Like, any other features the Spring Boot Actuator configuration starts with a starter dependency.

Dependency

For Maven

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

For Gradle

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

Configure

Once you add this dependency Spring Boot enables all of its endpoints. However you will find only /health & /info are active by default. Because, Spring Boot reveal some sensitive details about your application. Hence, Spring has secured them by-default. However, you can enable those sensitive endpoints using the properties file.

endpoints:
  autoconfig:
    sensitive: false
    id: <SOME IDENTIFER >
    path: autoconf
    enabled: true
  • sensitive: It tells spring boot if the information it reveals is sensitive.
  • id: Unique identifier to the endpoint.
  • path: Id is a default path of any endpoint. Additionally, a different path can be set here.
  • enabled: Enabled by-default. However, can be disabled using this property.

Existing Endpoints

Now, quickly have a look at some of the existing endpoints.

  • /health: It Shows application’s health information. Moreover, It is not sensitive.
  • /auditevents: Shows Audit information from the application.
  • /autoconfig: Shows the autoconfiguration report. Like, list of automatically configured beans etc
  • /beans: List of spring beans in the application.
  • /dump: Performs and provides thread dump.
  • /configprops: Publishes collective list of application properties.
  • /info: Shows application’s information. Also, It is not sensitive.
  • /metrics: Shows important metrics about application. For Instance, memory, heap, number of threads etc.
  • /mappings: Information about all the Http Request Mappings in the application.
  • /trace: Information of last 100 Http Requests served by the application.
  • /shutdown: Can be used to shutdown an application gracefully.
  • /docs: Documentation to all the endpoints. Also has sample request and response. To enable you need to add dependency on spring-boot-actuator-docs.

Custom Endpoint

Although, Spring Boot has provided handful of ready to use endpoints, some cases you may want to have your own endpoints. For example, you want to provide timestamp at server through /server-time endpoint.

Adding a custom endpoint is very simple. You only need to implement an Endpoint interface with required generic-type. For instance, see the below example.

@Component
public class ServerTimeEndpoint implements Endpoint<Map<String, String>> {
    private final String id = "server-time";

    @Override
    public String getId() {
        return id;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean isSensitive() {
        return false;
    }

    @Override
    public Map<String, String> invoke() {
        Map<String, String> map = new HashMap<>();
        map.put("server.date", LocalDate.now().toString());
        map.put("server.time", LocalTime.now().toString());
        return map;
    }
}

We have set enabled to true and also marked is non-sensitive. Therefore, we can directly call a GET on this endpoint using the id.

➜  ~ curl http://localhost:8995/server-time

{
  "server.date": "2019-02-21",
  "server.time": "09:33:55.685"
}

Summary

You have reached to the end of this short tutorial of Actuator in old Spring Boot (1.x).
To sum up, you learnt to enable, configure Spring Boot 1.x endpoints. Also, you learnt to write your own custom endpoints to expose more specific data from your application. Additionally, you saw Spring Boot treats all of the endpoints except /health and /info as sensitive.

As told earlier, this tutorial this tutorial is for the legacy Spring Boot users. For Spring Boot 2.x users there is a separate post on the same topic Spring Boot Actuator Endpoint.

You can find the full source code of the examples used here at gihub