49. Endpoints

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

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

The following technology-agnostic endpoints are available:

IDDescription

auditevents

Exposes audit events information for the current application.

autoconfig

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

beans

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

configprops

Displays a collated list of all @ConfigurationProperties.

env

Exposes properties from Spring’s ConfigurableEnvironment.

flyway

Shows any Flyway database migrations that have been applied.

health

Shows application health information.

info

Displays arbitrary application info.

loggers

Shows and modifies the configuration of loggers in the application.

liquibase

Shows any Liquibase database migrations that have been applied.

metrics

Shows ‘metrics’ information for the current application.

mappings

Displays a collated list of all @RequestMapping paths.

sessions

Allows retrieval and deletion of user sessions from a Spring Session-backed session store.

shutdown

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

status

Shows application status information (that is, health status with no additional details).

threaddump

Performs a thread dump.

trace

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

If your application is a web application (Spring MVC, Spring WebFlux, or Jersey), you can use the following additional endpoints:

IDDescription

heapdump

Returns a GZip compressed hprof heap dump file.

logfile

Returns the contents of the logfile (if logging.file or logging.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.

prometheus

Exposes metrics in a format that can be scraped by a Prometheus server.

49.1 Securing Endpoints

By default, all HTTP endpoints are secured such that only users that have an ACTUATOR role may access them. Security is enforced by using the standard HttpServletRequest.isUserInRole method.

[Tip]Tip

If you want to use something other than ACTUATOR as the role, set the management.security.roles property to the value you want to use.

If you deploy applications behind a firewall, you may prefer that all your actuator endpoints can be accessed without requiring authentication. You can do so by changing the management.security.enabled property, as follows:

application.properties. 

management.security.enabled=false

[Caution]Caution

By default, actuator endpoints are exposed on the same port that serves regular HTTP traffic. Take care not to accidentally expose sensitive information if you change the management.security.enabled property.

If you deploy applications publicly, you may want to add ‘Spring Security’ to handle user authentication. When ‘Spring Security’ is added, by default, ‘basic’ authentication is used. The username is`user` and the password is a random generated password (which is printed on the console when the application starts).

[Tip]Tip

Generated passwords are logged as the application starts. To find the password in the console, search for ‘Using default security password’.

You can use Spring properties to change the username and password and to change the security role(s) required to access the endpoints. For example, you might set the following properties in your application.properties:

security.user.name=admin
security.user.password=secret
management.security.roles=SUPERUSER

If your application has custom security configuration and you want all your actuator endpoints to be accessible without authentication, you need to explicitly configure that in your security configuration. Also, you need to change the management.security.enabled property to false.

If your custom security configuration secures your actuator endpoints, you also need to ensure that the authenticated user has the roles specified under management.security.roles.

[Tip]Tip

If you do not have a use case for exposing basic health information to unauthenticated users and you have secured the actuator endpoints with custom security, you can set management.security.enabled to false. This tells Spring Boot to skip the additional role check.

49.2 Customizing Endpoints

Endpoints can be customized by using Spring properties. You can change whether an endpoint is enabled and its id.

For example, the following application.properties changes the id of the beans endpoint and also enables shutdown:

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

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

By default, all endpoints except for shutdown are enabled. If you prefer to specifically “opt-in” endpoint enablement, you can use the endpoints.default.enabled property. For example, the following settings disables all endpoints except for info:

endpoints.default.enabled=false
endpoints.info.enabled=true

49.3 Hypermedia for Actuator Web Endpoints

A “discovery page” is added with links to all the endpoints. The “discovery page” is available on /application by default.

When a custom management context path is configured, the “discovery page” automatically moves from /application to the root of the management context. For example, if the management context path is /management, then the discovery page is available from /management. When the management context path is set to /, the discovery page is disabled to prevent the possibility of a clash with other mappings.

49.4 CORS Support

Cross-origin resource sharing (CORS) is a W3C specification that allows you to specify in a flexible way what kind of cross domain requests are authorized. If you use Spring MVC or Spring WebFlux, Actuator’s web endpoints can be configured to support such scenarios.

CORS support is disabled by default and is only enabled once the management.endpoints.cors.allowed-origins property has been set. The following configuration permits GET and POST calls from the example.com domain:

management.endpoints.cors.allowed-origins=http://example.com
management.endpoints.cors.allowed-methods=GET,POST
[Tip]Tip

See CorsEndpointProperties for a complete list of options.

49.5 Adding Custom Endpoints

If you add a @Bean annotated with @Endpoint, any methods annotated with @ReadOperation or @WriteOperation are automatically exposed over JMX and, in a web application, over HTTP as well.

[Tip]Tip

If you do this as a library feature, consider adding a configuration class annotated with @ManagementContextConfiguration to /META-INF/spring.factories under the key, org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration. If you do so and if your users ask for a separate management port or address, the endpoint moves to a child context with all the other web endpoints.

49.6 Health Information

You can use health information to check the status of your running application. It is often used by monitoring software to alert someone when a production system goes down. The default information exposed by the health endpoint depends on how it is accessed. For an unauthenticated connection in a secure application, a simple ‘status’ message is returned. For an authenticated connection, additional details are also displayed. (See Section 50.6, “HTTP Health Endpoint Format and Access Restrictions” for HTTP details.)

Health information is collected from all HealthIndicator beans defined in your ApplicationContext. Spring Boot includes a number of auto-configured HealthIndicators, and you can also write your own. By default, the final system state is derived by the HealthAggregator, which sorts the statuses from each HealthIndicator based on an ordered list of statuses. The first status in the sorted list is used as the overall health status. If no HealthIndicator returns a status that is known to the HealthAggregator, an UNKNOWN status is used.

49.6.1 Auto-configured HealthIndicators

The following HealthIndicators are auto-configured by Spring Boot when appropriate:

NameDescription

CassandraHealthIndicator

Checks that a Cassandra database is up.

DiskSpaceHealthIndicator

Checks for low disk space.

DataSourceHealthIndicator

Checks that a connection to DataSource can be obtained.

ElasticsearchHealthIndicator

Checks that an Elasticsearch cluster is up.

JmsHealthIndicator

Checks that a JMS broker is up.

MailHealthIndicator

Checks that a mail server is up.

MongoHealthIndicator

Checks that a Mongo database is up.

RabbitHealthIndicator

Checks that a Rabbit server is up.

RedisHealthIndicator

Checks that a Redis server is up.

SolrHealthIndicator

Checks that a Solr server is up.

[Tip]Tip

It is possible to disable them all using the management.health.defaults.enabled property.

49.6.2 Writing Custom HealthIndicators

To provide custom health information, you can register Spring beans that implement the HealthIndicator interface. You need to provide an implementation of the health() method and return a Health response. The Health response should include a status and can optionally include additional details to be displayed. The following code shows a sample HealthIndicator implementation:

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

@Component
public class MyHealthIndicator implements HealthIndicator {

	@Override
	public Health health() {
		int errorCode = check(); // perform some specific health check
		if (errorCode != 0) {
			return Health.down().withDetail("Error Code", errorCode).build();
		}
		return Health.up().build();
	}

}
[Note]Note

The identifier for a given HealthIndicator is the name of the bean without the HealthIndicator suffix, if it exists. In the preceding example, the health information is available in an entry named my.

In addition to Spring Boot’s predefined Status types, it is also possible for Health to return a custom Status that represents a new system state. In such cases, a custom implementation of the HealthAggregator interface also needs to be provided, or the default implementation has to be configured by using the management.health.status.order configuration property.

For example, assume a new Status with code FATAL is being used in one of your HealthIndicator implementations. To configure the severity order, add the following to your application properties:

management.health.status.order=FATAL, DOWN, OUT_OF_SERVICE, UNKNOWN, UP

The HTTP status code in the response reflects the overall health status (for example, UP maps to 200, while OUT_OF_SERVICE and DOWN map to 503). You might also want to register custom status mappings if you access the health endpoint over HTTP. For example, the following property maps FATAL to 503 (service unavailable):

management.health.status.http-mapping.FATAL=503
[Tip]Tip

If you need more control, you can define your own HealthStatusHttpMapper bean.

The following table shows the default status mappings for the built-in statuses:

StatusMapping

DOWN

SERVICE_UNAVAILABLE (503)

OUT_OF_SERVICE

SERVICE_UNAVAILABLE (503)

UP

No mapping by default, so http status is 200

UNKNOWN

No mapping by default, so http status is 200

49.7 Application Information

Application information exposes various information collected from all InfoContributor beans defined in your ApplicationContext. Spring Boot includes a number of auto-configured InfoContributors, and you can write your own.

49.7.1 Auto-configured InfoContributors

The following InfoContributors are auto-configured by Spring Boot, when appropriate:

NameDescription

EnvironmentInfoContributor

Expose any key from the Environment under the info key.

GitInfoContributor

Expose git information if a git.properties file is available.

BuildInfoContributor

Expose build information if a META-INF/build-info.properties file is available.

[Tip]Tip

It is possible to disable them all using the management.info.defaults.enabled property.

49.7.2 Custom Application Information

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

info.app.encoding=UTF-8
info.app.java.source=1.8
info.app.java.target=1.8
[Tip]Tip

Rather than hardcoding those values, you could also expand info properties at build time.

Assuming you use Maven, you could rewrite the preceding example as follows:

info.app.encoding[email protected]@
info.app.java.source[email protected]@
info.app.java.target[email protected]@

49.7.3 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 GitProperties bean is available, the git.branch, git.commit.id and git.commit.time properties are exposed.

[Tip]Tip

A GitProperties bean is auto-configured if a git.properties file is available at the root of the classpath. See "Generate git information" for more details.

If you want to display the full git information (that is, the full content of git.properties), use the management.info.git.mode property, as follows:

management.info.git.mode=full

49.7.4 Build Information

If a BuildProperties bean is available, the info endpoint can also publish information about your build. This happens if a META-INF/build-info.properties file is available in the classpath.

[Tip]Tip

The Maven and Gradle plugins can both generate that file. See "Generate build information" for more details.

49.7.5 Writing Custom InfoContributors

To provide custom application information, you can register Spring beans that implement the InfoContributor interface.

The following example contributes an example entry with a single value:

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class ExampleInfoContributor implements InfoContributor {

	@Override
	public void contribute(Info.Builder builder) {
		builder.withDetail("example",
				Collections.singletonMap("key", "value"));
	}

}

If you reach the info endpoint, you should see a response that contains the following additional entry:

{
	"example": {
		"key" : "value"
	}
}