37. Endpoints

Actuator endpoints allow you to monitor and interact with your application. Spring Boot includes a number of built-in endpoints and you can also add your own. For example the health endpoint provides basic application health information.

The way that endpoints are exposed will depend on the type of technology that you choose. Most applications choose HTTP monitoring, where the ID of the endpoint is mapped to a URL. For example, by default, the health endpoint will be mapped to /health.

The following endpoints are available:

IDDescriptionSensitive

autoconfig

Displays an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied.

true

beans

Displays a complete list of all the Spring Beans in your application.

true

configprops

Displays a collated list of all @ConfigurationProperties.

true

dump

Performs a thread dump.

true

env

Exposes properties from Spring’s ConfigurableEnvironment.

true

health

Shows application health information (defaulting to a simple ‘OK’ message).

false

info

Displays arbitrary application info.

false

metrics

Shows ‘metrics’ information for the current application.

true

mappings

Displays a collated list of all @RequestMapping paths.

true

shutdown

Allows the application to be gracefully shutdown (not enabled by default).

true

trace

Displays trace information (by default the last few HTTP requests).

true

[Note]Note

Depending on how an endpoint is exposed, the sensitive parameter may be used as a security hint. For example, sensitive endpoints will require a username/password when they are accessed over HTTP (or simply disabled if web security is not enabled).

37.1 Customizing endpoints

Endpoints can be customized using Spring properties. You can change if an endpoint is enabled, if it is considered sensitive and even its id.

For example, here is an application.properties that changes the sensitivity and id of the beans endpoint and also enables shutdown.

endpoints.beans.id=springbeans
endpoints.beans.sensitive=false
endpoints.shutdown.enabled=true
[Note]Note

The prefix ‟endpoints + . + name” is used to uniquely identify the endpoint that is being configured.

37.2 Custom health information

The default information exposed by the health endpoint is a simple ‘OK’ message. It is often useful to perform some additional health checks, for example you might check that a database connection works, or that a remote REST endpoint is functioning.

To provide custom health information you can register a Spring bean that implements the HealthIndicator interface.

import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealth implements HealthIndicator {

    @Override
    public Health health() {
        // perform some specific health check
        return ...
    }

}

Spring Boot provides a DataSourceHealthIndicator implementation that attempts a simple database test (reusing the validation query set on the data source, if any) as well as implementations for Redis, MongoDB and RabbitMQ. Spring Boot adds the HealthIndicator instances automatically if beans of type DataSource, MongoTemplate, RedisConnectionFactory, and RabbitTemplate respectively are present in the ApplicationContext. A health indicator that checks free disk space is also provided.

Besides implementing custom a HealthIndicator type and using out-of-box Status types, it is also possible to introduce custom Status types for different or more complex system states. In that case a custom implementation of the HealthAggregator interface needs to be provided or the default implementation has to be configured using the management.health.status.order configuration property.

Assuming a new Status with code FATAL is being used in one of your HealthIndicator implementations. To configure the severity or order add the following to your application properties: management.health.status.order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP.

37.3 Custom application info information

You can customize the data exposed by the info endpoint by setting info.* Spring properties. All Environment properties under the info key will be automatically exposed. For example, you could add the following to your application.properties:

info.app.name=MyService
info.app.description=My awesome service
info.app.version=1.0.0

37.3.1 Automatically expand info properties at build time

Rather than hardcoding some properties that are also specified in your project’s build configuration, you can automatically expand info properties using the existing build configuration instead. This is possible in both Maven and Gradle.

Automatic property expansion using Maven

You can automatically expand info properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders, e.g.

project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
info.build.artifact=${project.artifactId}
info.build.name[email protected]@
info.build.description[email protected]@
info.build.version[email protected]@
[Note]Note

In the above example we used project.* to set some values to be used as fallbacks if the Maven resource filtering has not been switched on for some reason.

[Note]Note

If you don’t use the starter parent, in your pom.xml you need (inside the <build/> element):

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

and (inside <plugins/>):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
    </configuration>
</plugin>

Automatic property expansion using Gradle

You can automatically expand info properties from the Gradle project by configuring the Java plugin’s processResources task to do so:

processResources {
    expand(project.properties)
}

You can then refer to your Gradle project’s properties via placeholders, e.g.

info.build.name=${name}
info.build.description=${description}
info.build.version=${version}

37.3.2 Git commit information

Another useful feature of the info endpoint is its ability to publish information about the state of your git source code repository when the project was built. If a git.properties file is contained in your jar the git.branch and git.commit properties will be loaded.

For Maven users the spring-boot-starter-parent POM includes a pre-configured plugin to generate a git.properties file. Simply add the following declaration to your POM:

<build>
    <plugins>
        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
        </plugin>
    </plugins>
</build>

A similar gradle-git plugin is also available for Gradle users, although a little more work is required to generate the properties file.