Production-ready Features

1. Enabling Production-ready Features

The spring-boot-actuator module provides all of Spring Boot’s production-ready features. The recommended way to enable the features is to add a dependency on the spring-boot-starter-actuator “Starter”.spring-doc.cn

Definition of Actuator

An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.spring-doc.cn

To add the actuator to a Maven-based project, add the following “Starter” dependency:spring-doc.cn

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

For Gradle, use the following declaration:spring-doc.cn

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}

2. 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.spring-doc.cn

You can enable or disable each individual endpoint and expose them (make them remotely accessible) over HTTP or JMX. An endpoint is considered to be available when it is both enabled and exposed. The built-in endpoints are auto-configured only when they are available. Most applications choose exposure over HTTP, where the ID of the endpoint and a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.spring-doc.cn

To learn more about the Actuator’s endpoints and their request and response formats, see the separate API documentation (HTML or PDF).

The following technology-agnostic endpoints are available:spring-doc.cn

ID Description

auditeventsspring-doc.cn

Exposes audit events information for the current application. Requires an AuditEventRepository bean.spring-doc.cn

beansspring-doc.cn

Displays a complete list of all the Spring beans in your application.spring-doc.cn

cachesspring-doc.cn

Exposes available caches.spring-doc.cn

conditionsspring-doc.cn

Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.spring-doc.cn

configpropsspring-doc.cn

Displays a collated list of all @ConfigurationProperties. Subject to sanitization.spring-doc.cn

envspring-doc.cn

Exposes properties from Spring’s ConfigurableEnvironment. Subject to sanitization.spring-doc.cn

flywayspring-doc.cn

Shows any Flyway database migrations that have been applied. Requires one or more Flyway beans.spring-doc.cn

healthspring-doc.cn

Shows application health information.spring-doc.cn

httpexchangesspring-doc.cn

Displays HTTP exchange information (by default, the last 100 HTTP request-response exchanges). Requires an HttpExchangeRepository bean.spring-doc.cn

infospring-doc.cn

Displays arbitrary application info.spring-doc.cn

integrationgraphspring-doc.cn

Shows the Spring Integration graph. Requires a dependency on spring-integration-core.spring-doc.cn

loggersspring-doc.cn

Shows and modifies the configuration of loggers in the application.spring-doc.cn

liquibasespring-doc.cn

Shows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.spring-doc.cn

metricsspring-doc.cn

Shows “metrics” information for the current application.spring-doc.cn

mappingsspring-doc.cn

Displays a collated list of all @RequestMapping paths.spring-doc.cn

quartzspring-doc.cn

Shows information about Quartz Scheduler jobs. Subject to sanitization.spring-doc.cn

scheduledtasksspring-doc.cn

Displays the scheduled tasks in your application.spring-doc.cn

sessionsspring-doc.cn

Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a servlet-based web application that uses Spring Session.spring-doc.cn

shutdownspring-doc.cn

Lets the application be gracefully shutdown. Only works when using jar packaging. Disabled by default.spring-doc.cn

startupspring-doc.cn

Shows the startup steps data collected by the ApplicationStartup. Requires the SpringApplication to be configured with a BufferingApplicationStartup.spring-doc.cn

threaddumpspring-doc.cn

Performs a thread dump.spring-doc.cn

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

ID Description

heapdumpspring-doc.cn

Returns a heap dump file. On a HotSpot JVM, an HPROF-format file is returned. On an OpenJ9 JVM, a PHD-format file is returned.spring-doc.cn

logfilespring-doc.cn

Returns the contents of the logfile (if the logging.file.name or the logging.file.path property has been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.spring-doc.cn

prometheusspring-doc.cn

Exposes metrics in a format that can be scraped by a Prometheus server. Requires a dependency on micrometer-registry-prometheus.spring-doc.cn

2.1. Enabling Endpoints

By default, all endpoints except for shutdown are enabled. To configure the enablement of an endpoint, use its management.endpoint.<id>.enabled property. The following example enables the shutdown endpoint:spring-doc.cn

Properties
management.endpoint.shutdown.enabled=true
Yaml
management:
  endpoint:
    shutdown:
      enabled: true

If you prefer endpoint enablement to be opt-in rather than opt-out, set the management.endpoints.enabled-by-default property to false and use individual endpoint enabled properties to opt back in. The following example enables the info endpoint and disables all other endpoints:spring-doc.cn

Properties
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
Yaml
management:
  endpoints:
    enabled-by-default: false
  endpoint:
    info:
      enabled: true
Disabled endpoints are removed entirely from the application context. If you want to change only the technologies over which an endpoint is exposed, use the include and exclude properties instead.

2.2. Exposing Endpoints

By default, only the health endpoint is exposed over HTTP and JMX. Since Endpoints may contain sensitive information, you should carefully consider when to expose them.spring-doc.cn

To change which endpoints are exposed, use the following technology-specific include and exclude properties:spring-doc.cn

Property Default

management.endpoints.jmx.exposure.excludespring-doc.cn

management.endpoints.jmx.exposure.includespring-doc.cn

healthspring-doc.cn

management.endpoints.web.exposure.excludespring-doc.cn

management.endpoints.web.exposure.includespring-doc.cn

healthspring-doc.cn

The include property lists the IDs of the endpoints that are exposed. The exclude property lists the IDs of the endpoints that should not be exposed. The exclude property takes precedence over the include property. You can configure both the include and the exclude properties with a list of endpoint IDs.spring-doc.cn

For example, to only expose the health and info endpoints over JMX, use the following property:spring-doc.cn

Properties
management.endpoints.jmx.exposure.include=health,info
Yaml
management:
  endpoints:
    jmx:
      exposure:
        include: "health,info"

* can be used to select all endpoints. For example, to expose everything over HTTP except the env and beans endpoints, use the following properties:spring-doc.cn

Properties
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
Yaml
management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: "env,beans"
* has a special meaning in YAML, so be sure to add quotation marks if you want to include (or exclude) all endpoints.
If your application is exposed publicly, we strongly recommend that you also secure your endpoints.
If you want to implement your own strategy for when endpoints are exposed, you can register an EndpointFilter bean.

2.3. Security

For security purposes, only the /health endpoint is exposed over HTTP by default. You can use the management.endpoints.web.exposure.include property to configure the endpoints that are exposed.spring-doc.cn

Before setting the management.endpoints.web.exposure.include, ensure that the exposed actuators do not contain sensitive information, are secured by placing them behind a firewall, or are secured by something like Spring Security.

If Spring Security is on the classpath and no other SecurityFilterChain bean is present, all actuators other than /health are secured by Spring Boot auto-configuration. If you define a custom SecurityFilterChain bean, Spring Boot auto-configuration backs off and lets you fully control the actuator access rules.spring-doc.cn

If you wish to configure custom security for HTTP endpoints (for example, to allow only users with a certain role to access them), Spring Boot provides some convenient RequestMatcher objects that you can use in combination with Spring Security.spring-doc.cn

A typical Spring Security configuration might look something like the following example:spring-doc.cn

Java
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.securityMatcher(EndpointRequest.toAnyEndpoint());
        http.authorizeHttpRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
        http.httpBasic(withDefaults());
        return http.build();
    }

}
Kotlin
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.web.SecurityFilterChain

@Configuration(proxyBeanMethods = false)
class MySecurityConfiguration {

    @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
        http.securityMatcher(EndpointRequest.toAnyEndpoint()).authorizeHttpRequests { requests ->
            requests.anyRequest().hasRole("ENDPOINT_ADMIN")
        }
        http.httpBasic()
        return http.build()
    }

}

The preceding example uses EndpointRequest.toAnyEndpoint() to match a request to any endpoint and then ensures that all have the ENDPOINT_ADMIN role. Several other matcher methods are also available on EndpointRequest. See the API documentation (HTML or PDF) for details.spring-doc.cn

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.endpoints.web.exposure.include property, as follows:spring-doc.cn

Properties
management.endpoints.web.exposure.include=*
Yaml
management:
  endpoints:
    web:
      exposure:
        include: "*"

Additionally, if Spring Security is present, you would need to add custom security configuration that allows unauthenticated access to the endpoints, as the following example shows:spring-doc.cn

Java
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.securityMatcher(EndpointRequest.toAnyEndpoint());
        http.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll());
        return http.build();
    }

}
Kotlin
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.web.SecurityFilterChain

@Configuration(proxyBeanMethods = false)
class MySecurityConfiguration {

    @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
        http.securityMatcher(EndpointRequest.toAnyEndpoint()).authorizeHttpRequests { requests ->
            requests.anyRequest().permitAll()
        }
        return http.build()
    }

}
In both of the preceding examples, the configuration applies only to the actuator endpoints. Since Spring Boot’s security configuration backs off completely in the presence of any SecurityFilterChain bean, you need to configure an additional SecurityFilterChain bean with rules that apply to the rest of the application.

2.3.1. Cross Site Request Forgery Protection

Since Spring Boot relies on Spring Security’s defaults, CSRF protection is turned on by default. This means that the actuator endpoints that require a POST (shutdown and loggers endpoints), a PUT, or a DELETE get a 403 (forbidden) error when the default security configuration is in use.spring-doc.cn

We recommend disabling CSRF protection completely only if you are creating a service that is used by non-browser clients.

You can find additional information about CSRF protection in the Spring Security Reference Guide.spring-doc.cn

2.4. Configuring Endpoints

Endpoints automatically cache responses to read operations that do not take any parameters. To configure the amount of time for which an endpoint caches a response, use its cache.time-to-live property. The following example sets the time-to-live of the beans endpoint’s cache to 10 seconds:spring-doc.cn

Properties
management.endpoint.beans.cache.time-to-live=10s
Yaml
management:
  endpoint:
    beans:
      cache:
        time-to-live: "10s"
The management.endpoint.<name> prefix uniquely identifies the endpoint that is being configured.

2.5. Sanitize Sensitive Values

Information returned by the /env, /configprops and /quartz endpoints can be somewhat sensitive. All values are sanitized by default (that is replaced by ******). Viewing original values in the unsanitized form can be configured per endpoint using the showValues property for that endpoint. This property can be configured to have the following values:spring-doc.cn

  • ALWAYS - all values are shown in their unsanitized form to all usersspring-doc.cn

  • NEVER - all values are always sanitized (that is replaced by ******)spring-doc.cn

  • WHEN_AUTHORIZED - all values are shown in their unsanitized form to authorized usersspring-doc.cn

For HTTP endpoints, a user is considered to be authorized if they have authenticated and have the roles configured by the endpoint’s roles property. By default, any authenticated user is authorized. For JMX endpoints, all users are always authorized.spring-doc.cn

Properties
management.endpoint.env.show-values=WHEN_AUTHORIZED
management.endpoint.env.roles=admin
Yaml
management:
  endpoint:
    env:
      show-values: WHEN_AUTHORIZED
      roles: "admin"

The configuration above enables the ability for all users with the admin role to view all values in their original form from the /env endpoint.spring-doc.cn

When show-values is set to ALWAYS or WHEN_AUTHORIZED any sanitization applied by a SanitizingFunction will still be applied.

2.6. Hypermedia for Actuator Web Endpoints

A “discovery page” is added with links to all the endpoints. The “discovery page” is available on /actuator by default.spring-doc.cn

To disable the “discovery page”, add the following property to your application properties:spring-doc.cn

Properties
management.endpoints.web.discovery.enabled=false
Yaml
management:
  endpoints:
    web:
      discovery:
        enabled: false

When a custom management context path is configured, the “discovery page” automatically moves from /actuator to the root of the management context. For example, if the management context path is /management, 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.spring-doc.cn

2.7. CORS Support

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

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

Properties
management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST
Yaml
management:
  endpoints:
    web:
      cors:
        allowed-origins: "https://example.com"
        allowed-methods: "GET,POST"
See CorsEndpointProperties for a complete list of options.

2.8. Implementing Custom Endpoints

If you add a @Bean annotated with @Endpoint, any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and, in a web application, over HTTP as well. Endpoints can be exposed over HTTP by using Jersey, Spring MVC, or Spring WebFlux. If both Jersey and Spring MVC are available, Spring MVC is used.spring-doc.cn

The following example exposes a read operation that returns a custom object:spring-doc.cn

Java
@ReadOperation
public CustomData getData() {
    return new CustomData("test", 5);
}
Kotlin
@ReadOperation
fun getData(): CustomData {
    return CustomData("test", 5)
}

You can also write technology-specific endpoints by using @JmxEndpoint or @WebEndpoint. These endpoints are restricted to their respective technologies. For example, @WebEndpoint is exposed only over HTTP and not over JMX.spring-doc.cn

You can write technology-specific extensions by using @EndpointWebExtension and @EndpointJmxExtension. These annotations let you provide technology-specific operations to augment an existing endpoint.spring-doc.cn

Finally, if you need access to web-framework-specific functionality, you can implement servlet or Spring @Controller and @RestController endpoints at the cost of them not being available over JMX or when using a different web framework.spring-doc.cn

2.8.1. Receiving Input

Operations on an endpoint receive input through their parameters. When exposed over the web, the values for these parameters are taken from the URL’s query parameters and from the JSON request body. When exposed over JMX, the parameters are mapped to the parameters of the MBean’s operations. Parameters are required by default. They can be made optional by annotating them with either @javax.annotation.Nullable or @org.springframework.lang.Nullable.spring-doc.cn

You can map each root property in the JSON request body to a parameter of the endpoint. Consider the following JSON request body:spring-doc.cn

{
    "name": "test",
    "counter": 42
}

You can use this to invoke a write operation that takes String name and int counter parameters, as the following example shows:spring-doc.cn

Java
@WriteOperation
public void updateData(String name, int counter) {
    // injects "test" and 42
}
Kotlin
@WriteOperation
fun updateData(name: String?, counter: Int) {
    // injects "test" and 42
}
Because endpoints are technology agnostic, only simple types can be specified in the method signature. In particular, declaring a single parameter with a CustomData type that defines a name and counter properties is not supported.
To let the input be mapped to the operation method’s parameters, Java code that implements an endpoint should be compiled with -parameters, and Kotlin code that implements an endpoint should be compiled with -java-parameters. This will happen automatically if you use Spring Boot’s Gradle plugin or if you use Maven and spring-boot-starter-parent.
Input Type Conversion

The parameters passed to endpoint operation methods are, if necessary, automatically converted to the required type. Before calling an operation method, the input received over JMX or HTTP is converted to the required types by using an instance of ApplicationConversionService as well as any Converter or GenericConverter beans qualified with @EndpointConverter.spring-doc.cn

2.8.2. Custom Web Endpoints

Operations on an @Endpoint, @WebEndpoint, or @EndpointWebExtension are automatically exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux. If both Jersey and Spring MVC are available, Spring MVC is used.spring-doc.cn

Web Endpoint Request Predicates

A request predicate is automatically generated for each operation on a web-exposed endpoint.spring-doc.cn

Path

The path of the predicate is determined by the ID of the endpoint and the base path of the web-exposed endpoints. The default base path is /actuator. For example, an endpoint with an ID of sessions uses /actuator/sessions as its path in the predicate.spring-doc.cn

You can further customize the path by annotating one or more parameters of the operation method with @Selector. Such a parameter is added to the path predicate as a path variable. The variable’s value is passed into the operation method when the endpoint operation is invoked. If you want to capture all remaining path elements, you can add @Selector(Match=ALL_REMAINING) to the last parameter and make it a type that is conversion-compatible with a String[].spring-doc.cn

HTTP method

The HTTP method of the predicate is determined by the operation type, as shown in the following table:spring-doc.cn

Operation HTTP method

@ReadOperationspring-doc.cn

GETspring-doc.cn

@WriteOperationspring-doc.cn

POSTspring-doc.cn

@DeleteOperationspring-doc.cn

DELETEspring-doc.cn

Consumes

For a @WriteOperation (HTTP POST) that uses the request body, the consumes clause of the predicate is application/vnd.spring-boot.actuator.v2+json, application/json. For all other operations, the consumes clause is empty.spring-doc.cn

Produces

The produces clause of the predicate can be determined by the produces attribute of the @DeleteOperation, @ReadOperation, and @WriteOperation annotations. The attribute is optional. If it is not used, the produces clause is determined automatically.spring-doc.cn

If the operation method returns void or Void, the produces clause is empty. If the operation method returns a org.springframework.core.io.Resource, the produces clause is application/octet-stream. For all other operations, the produces clause is application/vnd.spring-boot.actuator.v2+json, application/json.spring-doc.cn

Web Endpoint Response Status

The default response status for an endpoint operation depends on the operation type (read, write, or delete) and what, if anything, the operation returns.spring-doc.cn

If a @ReadOperation returns a value, the response status will be 200 (OK). If it does not return a value, the response status will be 404 (Not Found).spring-doc.cn

If a @WriteOperation or @DeleteOperation returns a value, the response status will be 200 (OK). If it does not return a value, the response status will be 204 (No Content).spring-doc.cn

If an operation is invoked without a required parameter or with a parameter that cannot be converted to the required type, the operation method is not called, and the response status will be 400 (Bad Request).spring-doc.cn

Web Endpoint Range Requests

You can use an HTTP range request to request part of an HTTP resource. When using Spring MVC or Spring Web Flux, operations that return a org.springframework.core.io.Resource automatically support range requests.spring-doc.cn

Range requests are not supported when using Jersey.
Web Endpoint Security

An operation on a web endpoint or a web-specific endpoint extension can receive the current java.security.Principal or org.springframework.boot.actuate.endpoint.SecurityContext as a method parameter. The former is typically used in conjunction with @Nullable to provide different behavior for authenticated and unauthenticated users. The latter is typically used to perform authorization checks by using its isUserInRole(String) method.spring-doc.cn

2.8.3. Servlet Endpoints

A servlet can be exposed as an endpoint by implementing a class annotated with @ServletEndpoint that also implements Supplier<EndpointServlet>. Servlet endpoints provide deeper integration with the servlet container but at the expense of portability. They are intended to be used to expose an existing servlet as an endpoint. For new endpoints, the @Endpoint and @WebEndpoint annotations should be preferred whenever possible.spring-doc.cn

2.8.4. Controller Endpoints

You can use @ControllerEndpoint and @RestControllerEndpoint to implement an endpoint that is exposed only by Spring MVC or Spring WebFlux. Methods are mapped by using the standard annotations for Spring MVC and Spring WebFlux, such as @RequestMapping and @GetMapping, with the endpoint’s ID being used as a prefix for the path. Controller endpoints provide deeper integration with Spring’s web frameworks but at the expense of portability. The @Endpoint and @WebEndpoint annotations should be preferred whenever possible.spring-doc.cn

2.9. 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 information exposed by the health endpoint depends on the management.endpoint.health.show-details and management.endpoint.health.show-components properties, which can be configured with one of the following values:spring-doc.cn

Name Description

neverspring-doc.cn

Details are never shown.spring-doc.cn

when-authorizedspring-doc.cn

Details are shown only to authorized users. Authorized roles can be configured by using management.endpoint.health.roles.spring-doc.cn

alwaysspring-doc.cn

Details are shown to all users.spring-doc.cn

The default value is never. A user is considered to be authorized when they are in one or more of the endpoint’s roles. If the endpoint has no configured roles (the default), all authenticated users are considered to be authorized. You can configure the roles by using the management.endpoint.health.roles property.spring-doc.cn

If you have secured your application and wish to use always, your security configuration must permit access to the health endpoint for both authenticated and unauthenticated users.

Health information is collected from the content of a HealthContributorRegistry (by default, all HealthContributor instances defined in your ApplicationContext). Spring Boot includes a number of auto-configured HealthContributors, and you can also write your own.spring-doc.cn

A HealthContributor can be either a HealthIndicator or a CompositeHealthContributor. A HealthIndicator provides actual health information, including a Status. A CompositeHealthContributor provides a composite of other HealthContributors. Taken together, contributors form a tree structure to represent the overall system health.spring-doc.cn

By default, the final system health is derived by a StatusAggregator, 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 StatusAggregator, an UNKNOWN status is used.spring-doc.cn

You can use the HealthContributorRegistry to register and unregister health indicators at runtime.

2.9.1. Auto-configured HealthIndicators

When appropriate, Spring Boot auto-configures the HealthIndicators listed in the following table. You can also enable or disable selected indicators by configuring management.health.key.enabled, with the key listed in the following table:spring-doc.cn

Key Name Description

cassandraspring-doc.cn

CassandraDriverHealthIndicatorspring-doc.cn

Checks that a Cassandra database is up.spring-doc.cn

couchbasespring-doc.cn

CouchbaseHealthIndicatorspring-doc.cn

Checks that a Couchbase cluster is up.spring-doc.cn

dbspring-doc.cn

DataSourceHealthIndicatorspring-doc.cn

Checks that a connection to DataSource can be obtained.spring-doc.cn

diskspacespring-doc.cn

DiskSpaceHealthIndicatorspring-doc.cn

Checks for low disk space.spring-doc.cn

elasticsearchspring-doc.cn

ElasticsearchRestClientHealthIndicatorspring-doc.cn

Checks that an Elasticsearch cluster is up.spring-doc.cn

hazelcastspring-doc.cn

HazelcastHealthIndicatorspring-doc.cn

Checks that a Hazelcast server is up.spring-doc.cn

influxdbspring-doc.cn

InfluxDbHealthIndicatorspring-doc.cn

Checks that an InfluxDB server is up.spring-doc.cn

jmsspring-doc.cn

JmsHealthIndicatorspring-doc.cn

Checks that a JMS broker is up.spring-doc.cn

ldapspring-doc.cn

LdapHealthIndicatorspring-doc.cn

Checks that an LDAP server is up.spring-doc.cn

mailspring-doc.cn

MailHealthIndicatorspring-doc.cn

Checks that a mail server is up.spring-doc.cn

mongospring-doc.cn

MongoHealthIndicatorspring-doc.cn

Checks that a Mongo database is up.spring-doc.cn

neo4jspring-doc.cn

Neo4jHealthIndicatorspring-doc.cn

Checks that a Neo4j database is up.spring-doc.cn

pingspring-doc.cn

PingHealthIndicatorspring-doc.cn

Always responds with UP.spring-doc.cn

rabbitspring-doc.cn

RabbitHealthIndicatorspring-doc.cn

Checks that a Rabbit server is up.spring-doc.cn

redisspring-doc.cn

RedisHealthIndicatorspring-doc.cn

Checks that a Redis server is up.spring-doc.cn

You can disable them all by setting the management.health.defaults.enabled property.

Additional HealthIndicators are available but are not enabled by default:spring-doc.cn

Key Name Description

livenessstatespring-doc.cn

LivenessStateHealthIndicatorspring-doc.cn

Exposes the “Liveness” application availability state.spring-doc.cn

readinessstatespring-doc.cn

ReadinessStateHealthIndicatorspring-doc.cn

Exposes the “Readiness” application availability state.spring-doc.cn

2.9.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:spring-doc.cn

Java
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();
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    private int check() {
        // perform some specific health check
        return ...
    }

}
Kotlin
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.HealthIndicator
import org.springframework.stereotype.Component

@Component
class MyHealthIndicator : HealthIndicator {

    override fun health(): Health {
        val errorCode = check()
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build()
        }
        return Health.up().build()
    }

    private fun check(): Int {
        // perform some specific health check
        return  ...
    }

}
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.
Health indicators are usually called over HTTP and need to respond before any connection timeouts. Spring Boot will log a warning message for any health indicator that takes longer than 10 seconds to respond. If you want to configure this threshold, you can use the management.endpoint.health.logging.slow-indicator-threshold property.

In addition to Spring Boot’s predefined Status types, Health can return a custom Status that represents a new system state. In such cases, you also need to provide a custom implementation of the StatusAggregator interface, or you must configure the default implementation by using the management.endpoint.health.status.order configuration property.spring-doc.cn

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

Properties
management.endpoint.health.status.order=fatal,down,out-of-service,unknown,up
Yaml
management:
  endpoint:
    health:
      status:
        order: "fatal,down,out-of-service,unknown,up"

The HTTP status code in the response reflects the overall health status. By default, OUT_OF_SERVICE and DOWN map to 503. Any unmapped health statuses, including UP, map to 200. You might also want to register custom status mappings if you access the health endpoint over HTTP. Configuring a custom mapping disables the defaults mappings for DOWN and OUT_OF_SERVICE. If you want to retain the default mappings, you must explicitly configure them, alongside any custom mappings. For example, the following property maps FATAL to 503 (service unavailable) and retains the default mappings for DOWN and OUT_OF_SERVICE:spring-doc.cn

Properties
management.endpoint.health.status.http-mapping.down=503
management.endpoint.health.status.http-mapping.fatal=503
management.endpoint.health.status.http-mapping.out-of-service=503
Yaml
management:
  endpoint:
    health:
      status:
        http-mapping:
          down: 503
          fatal: 503
          out-of-service: 503
If you need more control, you can define your own HttpCodeStatusMapper bean.

The following table shows the default status mappings for the built-in statuses:spring-doc.cn

Status Mapping

DOWNspring-doc.cn

SERVICE_UNAVAILABLE (503)spring-doc.cn

OUT_OF_SERVICEspring-doc.cn

SERVICE_UNAVAILABLE (503)spring-doc.cn

UPspring-doc.cn

No mapping by default, so HTTP status is 200spring-doc.cn

UNKNOWNspring-doc.cn

No mapping by default, so HTTP status is 200spring-doc.cn

2.9.3. Reactive Health Indicators

For reactive applications, such as those that use Spring WebFlux, ReactiveHealthContributor provides a non-blocking contract for getting application health. Similar to a traditional HealthContributor, health information is collected from the content of a ReactiveHealthContributorRegistry (by default, all HealthContributor and ReactiveHealthContributor instances defined in your ApplicationContext). Regular HealthContributors that do not check against a reactive API are executed on the elastic scheduler.spring-doc.cn

In a reactive application, you should use the ReactiveHealthContributorRegistry to register and unregister health indicators at runtime. If you need to register a regular HealthContributor, you should wrap it with ReactiveHealthContributor#adapt.

To provide custom health information from a reactive API, you can register Spring beans that implement the ReactiveHealthIndicator interface. The following code shows a sample ReactiveHealthIndicator implementation:spring-doc.cn

Java
import reactor.core.publisher.Mono;

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

@Component
public class MyReactiveHealthIndicator implements ReactiveHealthIndicator {

    @Override
    public Mono<Health> health() {
        return doHealthCheck().onErrorResume((exception) ->
            Mono.just(new Health.Builder().down(exception).build()));
    }

    private Mono<Health> doHealthCheck() {
        // perform some specific health check
        return ...
    }

}
Kotlin
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.ReactiveHealthIndicator
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono

@Component
class MyReactiveHealthIndicator : ReactiveHealthIndicator {

    override fun health(): Mono<Health> {
        return doHealthCheck()!!.onErrorResume { exception: Throwable? ->
            Mono.just(Health.Builder().down(exception).build())
        }
    }

    private fun doHealthCheck(): Mono<Health>? {
        // perform some specific health check
        return  ...
    }

}
To handle the error automatically, consider extending from AbstractReactiveHealthIndicator.

2.9.4. Auto-configured ReactiveHealthIndicators

When appropriate, Spring Boot auto-configures the following ReactiveHealthIndicators:spring-doc.cn

Key Name Description

cassandraspring-doc.cn

CassandraDriverReactiveHealthIndicatorspring-doc.cn

Checks that a Cassandra database is up.spring-doc.cn

couchbasespring-doc.cn

CouchbaseReactiveHealthIndicatorspring-doc.cn

Checks that a Couchbase cluster is up.spring-doc.cn

elasticsearchspring-doc.cn

ElasticsearchReactiveHealthIndicatorspring-doc.cn

Checks that an Elasticsearch cluster is up.spring-doc.cn

mongospring-doc.cn

MongoReactiveHealthIndicatorspring-doc.cn

Checks that a Mongo database is up.spring-doc.cn

neo4jspring-doc.cn

Neo4jReactiveHealthIndicatorspring-doc.cn

Checks that a Neo4j database is up.spring-doc.cn

redisspring-doc.cn

RedisReactiveHealthIndicatorspring-doc.cn

Checks that a Redis server is up.spring-doc.cn

If necessary, reactive indicators replace the regular ones. Also, any HealthIndicator that is not handled explicitly is wrapped automatically.

2.9.5. Health Groups

It is sometimes useful to organize health indicators into groups that you can use for different purposes.spring-doc.cn

To create a health indicator group, you can use the management.endpoint.health.group.<name> property and specify a list of health indicator IDs to include or exclude. For example, to create a group that includes only database indicators you can define the following:spring-doc.cn

Properties
management.endpoint.health.group.custom.include=db
Yaml
management:
  endpoint:
    health:
      group:
        custom:
          include: "db"

You can then check the result by hitting localhost:8080/actuator/health/custom.spring-doc.cn

Similarly, to create a group that excludes the database indicators from the group and includes all the other indicators, you can define the following:spring-doc.cn

Properties
management.endpoint.health.group.custom.exclude=db
Yaml
management:
  endpoint:
    health:
      group:
        custom:
          exclude: "db"

By default, groups inherit the same StatusAggregator and HttpCodeStatusMapper settings as the system health. However, you can also define these on a per-group basis. You can also override the show-details and roles properties if required:spring-doc.cn

Properties
management.endpoint.health.group.custom.show-details=when-authorized
management.endpoint.health.group.custom.roles=admin
management.endpoint.health.group.custom.status.order=fatal,up
management.endpoint.health.group.custom.status.http-mapping.fatal=500
management.endpoint.health.group.custom.status.http-mapping.out-of-service=500
Yaml
management:
  endpoint:
    health:
      group:
        custom:
          show-details: "when-authorized"
          roles: "admin"
          status:
            order: "fatal,up"
            http-mapping:
              fatal: 500
              out-of-service: 500
You can use @Qualifier("groupname") if you need to register custom StatusAggregator or HttpCodeStatusMapper beans for use with the group.

A health group can also include/exclude a CompositeHealthContributor. You can also include/exclude only a certain component of a CompositeHealthContributor. This can be done using the fully qualified name of the component as follows:spring-doc.cn

management.endpoint.health.group.custom.include="test/primary"
management.endpoint.health.group.custom.exclude="test/primary/b"

In the example above, the custom group will include the HealthContributor with the name primary which is a component of the composite test. Here, primary itself is a composite and the HealthContributor with the name b will be excluded from the custom group.spring-doc.cn

Health groups can be made available at an additional path on either the main or management port. This is useful in cloud environments such as Kubernetes, where it is quite common to use a separate management port for the actuator endpoints for security purposes. Having a separate port could lead to unreliable health checks because the main application might not work properly even if the health check is successful. The health group can be configured with an additional path as follows:spring-doc.cn

management.endpoint.health.group.live.additional-path="server:/healthz"

This would make the live health group available on the main server port at /healthz. The prefix is mandatory and must be either server: (represents the main server port) or management: (represents the management port, if configured.) The path must be a single path segment.spring-doc.cn

2.9.6. DataSource Health

The DataSource health indicator shows the health of both standard data sources and routing data source beans. The health of a routing data source includes the health of each of its target data sources. In the health endpoint’s response, each of a routing data source’s targets is named by using its routing key. If you prefer not to include routing data sources in the indicator’s output, set management.health.db.ignore-routing-data-sources to true.spring-doc.cn

2.10. Kubernetes Probes

Applications deployed on Kubernetes can provide information about their internal state with Container Probes. Depending on your Kubernetes configuration, the kubelet calls those probes and reacts to the result.spring-doc.cn

By default, Spring Boot manages your Application Availability State. If deployed in a Kubernetes environment, actuator gathers the “Liveness” and “Readiness” information from the ApplicationAvailability interface and uses that information in dedicated health indicators: LivenessStateHealthIndicator and ReadinessStateHealthIndicator. These indicators are shown on the global health endpoint ("/actuator/health"). They are also exposed as separate HTTP Probes by using health groups: "/actuator/health/liveness" and "/actuator/health/readiness".spring-doc.cn

You can then configure your Kubernetes infrastructure with the following endpoint information:spring-doc.cn

livenessProbe:
  httpGet:
    path: "/actuator/health/liveness"
    port: <actuator-port>
  failureThreshold: ...
  periodSeconds: ...

readinessProbe:
  httpGet:
    path: "/actuator/health/readiness"
    port: <actuator-port>
  failureThreshold: ...
  periodSeconds: ...
<actuator-port> should be set to the port that the actuator endpoints are available on. It could be the main web server port or a separate management port if the "management.server.port" property has been set.

These health groups are automatically enabled only if the application runs in a Kubernetes environment. You can enable them in any environment by using the management.endpoint.health.probes.enabled configuration property.spring-doc.cn

If an application takes longer to start than the configured liveness period, Kubernetes mentions the "startupProbe" as a possible solution. Generally speaking, the "startupProbe" is not necessarily needed here, as the "readinessProbe" fails until all startup tasks are done. This means your application will not receive traffic until it is ready. However, if your application takes a long time to start, consider using a "startupProbe" to make sure that Kubernetes won’t kill your application while it is in the process of starting. See the section that describes how probes behave during the application lifecycle.

If your Actuator endpoints are deployed on a separate management context, the endpoints do not use the same web infrastructure (port, connection pools, framework components) as the main application. In this case, a probe check could be successful even if the main application does not work properly (for example, it cannot accept new connections). For this reason, is it a good idea to make the liveness and readiness health groups available on the main server port. This can be done by setting the following property:spring-doc.cn

management.endpoint.health.probes.add-additional-paths=true

This would make the liveness group available at /livez and the readiness group available at /readyz on the main server port. Paths can be customized using the additional-path property on each group, see health groups for details.spring-doc.cn

2.10.1. Checking External State With Kubernetes Probes

Actuator configures the “liveness” and “readiness” probes as Health Groups. This means that all the health groups features are available for them. You can, for example, configure additional Health Indicators:spring-doc.cn

Properties
management.endpoint.health.group.readiness.include=readinessState,customCheck
Yaml
management:
  endpoint:
    health:
      group:
        readiness:
          include: "readinessState,customCheck"

By default, Spring Boot does not add other health indicators to these groups.spring-doc.cn

The “liveness” probe should not depend on health checks for external systems. If the liveness state of an application is broken, Kubernetes tries to solve that problem by restarting the application instance. This means that if an external system (such as a database, a Web API, or an external cache) fails, Kubernetes might restart all application instances and create cascading failures.spring-doc.cn

As for the “readiness” probe, the choice of checking external systems must be made carefully by the application developers. For this reason, Spring Boot does not include any additional health checks in the readiness probe. If the readiness state of an application instance is unready, Kubernetes does not route traffic to that instance. Some external systems might not be shared by application instances, in which case they could be included in a readiness probe. Other external systems might not be essential to the application (the application could have circuit breakers and fallbacks), in which case they definitely should not be included. Unfortunately, an external system that is shared by all application instances is common, and you have to make a judgement call: Include it in the readiness probe and expect that the application is taken out of service when the external service is down or leave it out and deal with failures higher up the stack, perhaps by using a circuit breaker in the caller.spring-doc.cn

If all instances of an application are unready, a Kubernetes Service with type=ClusterIP or NodePort does not accept any incoming connections. There is no HTTP error response (503 and so on), since there is no connection. A service with type=LoadBalancer might or might not accept connections, depending on the provider. A service that has an explicit ingress also responds in a way that depends on the implementation — the ingress service itself has to decide how to handle the “connection refused” from downstream. HTTP 503 is quite likely in the case of both load balancer and ingress.

Also, if an application uses Kubernetes autoscaling, it may react differently to applications being taken out of the load-balancer, depending on its autoscaler configuration.spring-doc.cn

2.10.2. Application Lifecycle and Probe States

An important aspect of the Kubernetes Probes support is its consistency with the application lifecycle. There is a significant difference between the AvailabilityState (which is the in-memory, internal state of the application) and the actual probe (which exposes that state). Depending on the phase of application lifecycle, the probe might not be available.spring-doc.cn

Spring Boot publishes application events during startup and shutdown, and probes can listen to such events and expose the AvailabilityState information.spring-doc.cn

The following tables show the AvailabilityState and the state of HTTP connectors at different stages.spring-doc.cn

When a Spring Boot application starts:spring-doc.cn

Startup phase LivenessState ReadinessState HTTP server Notes

Startingspring-doc.cn

BROKENspring-doc.cn

REFUSING_TRAFFICspring-doc.cn

Not startedspring-doc.cn

Kubernetes checks the "liveness" Probe and restarts the application if it takes too long.spring-doc.cn

Startedspring-doc.cn

CORRECTspring-doc.cn

REFUSING_TRAFFICspring-doc.cn

Refuses requestsspring-doc.cn

The application context is refreshed. The application performs startup tasks and does not receive traffic yet.spring-doc.cn

Readyspring-doc.cn

CORRECTspring-doc.cn

ACCEPTING_TRAFFICspring-doc.cn

Accepts requestsspring-doc.cn

Startup tasks are finished. The application is receiving traffic.spring-doc.cn

When a Spring Boot application shuts down:spring-doc.cn

Shutdown phase Liveness State Readiness State HTTP server Notes

Runningspring-doc.cn

CORRECTspring-doc.cn

ACCEPTING_TRAFFICspring-doc.cn

Accepts requestsspring-doc.cn

Shutdown has been requested.spring-doc.cn

Graceful shutdownspring-doc.cn

CORRECTspring-doc.cn

REFUSING_TRAFFICspring-doc.cn

New requests are rejectedspring-doc.cn

If enabled, graceful shutdown processes in-flight requests.spring-doc.cn

Shutdown completespring-doc.cn

N/Aspring-doc.cn

N/Aspring-doc.cn

Server is shut downspring-doc.cn

The application context is closed and the application is shut down.spring-doc.cn

See Kubernetes container lifecycle section for more information about Kubernetes deployment.

2.11. Application Information

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

2.11.1. Auto-configured InfoContributors

When appropriate, Spring auto-configures the following InfoContributor beans:spring-doc.cn

ID Name Description Prerequisites

buildspring-doc.cn

BuildInfoContributorspring-doc.cn

Exposes build information.spring-doc.cn

A META-INF/build-info.properties resource.spring-doc.cn

envspring-doc.cn

EnvironmentInfoContributorspring-doc.cn

Exposes any property from the Environment whose name starts with info..spring-doc.cn

None.spring-doc.cn

gitspring-doc.cn

GitInfoContributorspring-doc.cn

Exposes git information.spring-doc.cn

A git.properties resource.spring-doc.cn

javaspring-doc.cn

JavaInfoContributorspring-doc.cn

Exposes Java runtime information.spring-doc.cn

None.spring-doc.cn

osspring-doc.cn

OsInfoContributorspring-doc.cn

Exposes Operating System information.spring-doc.cn

None.spring-doc.cn

Whether an individual contributor is enabled is controlled by its management.info.<id>.enabled property. Different contributors have different defaults for this property, depending on their prerequisites and the nature of the information that they expose.spring-doc.cn

With no prerequisites to indicate that they should be enabled, the env, java, and os contributors are disabled by default. Each can be enabled by setting its management.info.<id>.enabled property to true.spring-doc.cn

The build and git info contributors are enabled by default. Each can be disabled by setting its management.info.<id>.enabled property to false. Alternatively, to disable every contributor that is usually enabled by default, set the management.info.defaults.enabled property to false.spring-doc.cn

2.11.2. Custom Application Information

When the env contributor is enabled, 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:spring-doc.cn

Properties
info.app.encoding=UTF-8
info.app.java.source=17
info.app.java.target=17
Yaml
info:
  app:
    encoding: "UTF-8"
    java:
      source: "17"
      target: "17"

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

Assuming you use Maven, you could rewrite the preceding example as follows:spring-doc.cn

Yaml
info:
  app:
    encoding: "@project.build.sourceEncoding@"
    java:
      source: "@java.version@"
      target: "@java.version@"

2.11.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, you can use the info endpoint to expose these properties.spring-doc.cn

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

By default, the endpoint exposes git.branch, git.commit.id, and git.commit.time properties, if present. If you do not want any of these properties in the endpoint response, they need to be excluded from the git.properties file. 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:spring-doc.cn

Properties
management.info.git.mode=full
Yaml
management:
  info:
    git:
      mode: "full"

To disable the git commit information from the info endpoint completely, set the management.info.git.enabled property to false, as follows:spring-doc.cn

Properties
management.info.git.enabled=false
Yaml
management:
  info:
    git:
      enabled: false

2.11.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.spring-doc.cn

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

2.11.5. Java Information

The info endpoint publishes information about your Java runtime environment, see JavaInfo for more details.spring-doc.cn

2.11.6. OS Information

The info endpoint publishes information about your Operating System, see OsInfo for more details.spring-doc.cn

2.11.7. Writing Custom InfoContributors

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

The following example contributes an example entry with a single value:spring-doc.cn

Java
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 MyInfoContributor implements InfoContributor {

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

}
Kotlin
import org.springframework.boot.actuate.info.Info
import org.springframework.boot.actuate.info.InfoContributor
import org.springframework.stereotype.Component
import java.util.Collections

@Component
class MyInfoContributor : InfoContributor {

    override fun contribute(builder: Info.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:spring-doc.cn

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

3. Monitoring and Management Over HTTP

If you are developing a web application, Spring Boot Actuator auto-configures all enabled endpoints to be exposed over HTTP. The default convention is to use the id of the endpoint with a prefix of /actuator as the URL path. For example, health is exposed as /actuator/health.spring-doc.cn

Actuator is supported natively with Spring MVC, Spring WebFlux, and Jersey. If both Jersey and Spring MVC are available, Spring MVC is used.
Jackson is a required dependency in order to get the correct JSON responses as documented in the API documentation (HTML or PDF).

3.1. Customizing the Management Endpoint Paths

Sometimes, it is useful to customize the prefix for the management endpoints. For example, your application might already use /actuator for another purpose. You can use the management.endpoints.web.base-path property to change the prefix for your management endpoint, as the following example shows:spring-doc.cn

Properties
management.endpoints.web.base-path=/manage
Yaml
management:
  endpoints:
    web:
      base-path: "/manage"

The preceding application.properties example changes the endpoint from /actuator/{id} to /manage/{id} (for example, /manage/info).spring-doc.cn

Unless the management port has been configured to expose endpoints by using a different HTTP port, management.endpoints.web.base-path is relative to server.servlet.context-path (for servlet web applications) or spring.webflux.base-path (for reactive web applications). If management.server.port is configured, management.endpoints.web.base-path is relative to management.server.base-path.

If you want to map endpoints to a different path, you can use the management.endpoints.web.path-mapping property.spring-doc.cn

The following example remaps /actuator/health to /healthcheck:spring-doc.cn

Properties
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck
Yaml
management:
  endpoints:
    web:
      base-path: "/"
      path-mapping:
        health: "healthcheck"

3.2. Customizing the Management Server Port

Exposing management endpoints by using the default HTTP port is a sensible choice for cloud-based deployments. If, however, your application runs inside your own data center, you may prefer to expose endpoints by using a different HTTP port.spring-doc.cn

You can set the management.server.port property to change the HTTP port, as the following example shows:spring-doc.cn

Properties
management.server.port=8081
Yaml
management:
  server:
    port: 8081
On Cloud Foundry, by default, applications receive requests only on port 8080 for both HTTP and TCP routing. If you want to use a custom management port on Cloud Foundry, you need to explicitly set up the application’s routes to forward traffic to the custom port.

3.3. Configuring Management-specific SSL

When configured to use a custom port, you can also configure the management server with its own SSL by using the various management.server.ssl.* properties. For example, doing so lets a management server be available over HTTP while the main application uses HTTPS, as the following property settings show:spring-doc.cn

Properties
server.port=8443
server.ssl.enabled=true
server.ssl.key-store=classpath:store.jks
server.ssl.key-password=secret
management.server.port=8080
management.server.ssl.enabled=false
Yaml
server:
  port: 8443
  ssl:
    enabled: true
    key-store: "classpath:store.jks"
    key-password: "secret"
management:
  server:
    port: 8080
    ssl:
      enabled: false

Alternatively, both the main server and the management server can use SSL but with different key stores, as follows:spring-doc.cn

Properties
server.port=8443
server.ssl.enabled=true
server.ssl.key-store=classpath:main.jks
server.ssl.key-password=secret
management.server.port=8080
management.server.ssl.enabled=true
management.server.ssl.key-store=classpath:management.jks
management.server.ssl.key-password=secret
Yaml
server:
  port: 8443
  ssl:
    enabled: true
    key-store: "classpath:main.jks"
    key-password: "secret"
management:
  server:
    port: 8080
    ssl:
      enabled: true
      key-store: "classpath:management.jks"
      key-password: "secret"

3.4. Customizing the Management Server Address

You can customize the address on which the management endpoints are available by setting the management.server.address property. Doing so can be useful if you want to listen only on an internal or ops-facing network or to listen only for connections from localhost.spring-doc.cn

You can listen on a different address only when the port differs from the main server port.

The following example application.properties does not allow remote management connections:spring-doc.cn

Properties
management.server.port=8081
management.server.address=127.0.0.1
Yaml
management:
  server:
    port: 8081
    address: "127.0.0.1"

3.5. Disabling HTTP Endpoints

If you do not want to expose endpoints over HTTP, you can set the management port to -1, as the following example shows:spring-doc.cn

Properties
management.server.port=-1
Yaml
management:
  server:
    port: -1

You can also achieve this by using the management.endpoints.web.exposure.exclude property, as the following example shows:spring-doc.cn

Properties
management.endpoints.web.exposure.exclude=*
Yaml
management:
  endpoints:
    web:
      exposure:
        exclude: "*"

4. Monitoring and Management over JMX

Java Management Extensions (JMX) provide a standard mechanism to monitor and manage applications. By default, this feature is not enabled. You can turn it on by setting the spring.jmx.enabled configuration property to true. Spring Boot exposes the most suitable MBeanServer as a bean with an ID of mbeanServer. Any of your beans that are annotated with Spring JMX annotations (@ManagedResource, @ManagedAttribute, or @ManagedOperation) are exposed to it.spring-doc.cn

If your platform provides a standard MBeanServer, Spring Boot uses that and defaults to the VM MBeanServer, if necessary. If all that fails, a new MBeanServer is created.spring-doc.cn

See the JmxAutoConfiguration class for more details.spring-doc.cn

By default, Spring Boot also exposes management endpoints as JMX MBeans under the org.springframework.boot domain. To take full control over endpoint registration in the JMX domain, consider registering your own EndpointObjectNameFactory implementation.spring-doc.cn

4.1. Customizing MBean Names

The name of the MBean is usually generated from the id of the endpoint. For example, the health endpoint is exposed as org.springframework.boot:type=Endpoint,name=Health.spring-doc.cn

If your application contains more than one Spring ApplicationContext, you may find that names clash. To solve this problem, you can set the spring.jmx.unique-names property to true so that MBean names are always unique.spring-doc.cn

You can also customize the JMX domain under which endpoints are exposed. The following settings show an example of doing so in application.properties:spring-doc.cn

Properties
spring.jmx.unique-names=true
management.endpoints.jmx.domain=com.example.myapp
Yaml
spring:
  jmx:
    unique-names: true
management:
  endpoints:
    jmx:
      domain: "com.example.myapp"

4.2. Disabling JMX Endpoints

If you do not want to expose endpoints over JMX, you can set the management.endpoints.jmx.exposure.exclude property to *, as the following example shows:spring-doc.cn

Properties
management.endpoints.jmx.exposure.exclude=*
Yaml
management:
  endpoints:
    jmx:
      exposure:
        exclude: "*"

5. Observability

Observability is the ability to observe the internal state of a running system from the outside. It consists of the three pillars logging, metrics and traces.spring-doc.cn

For metrics and traces, Spring Boot uses Micrometer Observation. To create your own observations (which will lead to metrics and traces), you can inject an ObservationRegistry.spring-doc.cn

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;

import org.springframework.stereotype.Component;

@Component
public class MyCustomObservation {

    private final ObservationRegistry observationRegistry;

    public MyCustomObservation(ObservationRegistry observationRegistry) {
        this.observationRegistry = observationRegistry;
    }

    public void doSomething() {
        Observation.createNotStarted("doSomething", this.observationRegistry)
                .lowCardinalityKeyValue("locale", "en-US")
                .highCardinalityKeyValue("userId", "42")
                .observe(() -> {
                    // Execute business logic here
                });
    }

}
Low cardinality tags will be added to metrics and traces, while high cardinality tags will only be added to traces.

Beans of type ObservationPredicate, GlobalObservationConvention and ObservationHandler will be automatically registered on the ObservationRegistry. You can additionally register any number of ObservationRegistryCustomizer beans to further configure the registry.spring-doc.cn

For more details please see the Micrometer Observation documentation.spring-doc.cn

Observability for JDBC and R2DBC can be configured using separate projects. For JDBC, the Datasource Micrometer project provides a Spring Boot starter which automatically creates observations when JDBC operations are invoked. Read more about it in the reference documentation. For R2DBC, the Spring Boot Auto Configuration for R2DBC Observation creates observations for R2DBC query invocations.

The next sections will provide more details about logging, metrics and traces.spring-doc.cn

6. Loggers

Spring Boot Actuator includes the ability to view and configure the log levels of your application at runtime. You can view either the entire list or an individual logger’s configuration, which is made up of both the explicitly configured logging level as well as the effective logging level given to it by the logging framework. These levels can be one of:spring-doc.cn

null indicates that there is no explicit configuration.spring-doc.cn

6.1. Configure a Logger

To configure a given logger, POST a partial entity to the resource’s URI, as the following example shows:spring-doc.cn

{
    "configuredLevel": "DEBUG"
}
To “reset” the specific level of the logger (and use the default configuration instead), you can pass a value of null as the configuredLevel.

7. Metrics

Spring Boot Actuator provides dependency management and auto-configuration for Micrometer, an application metrics facade that supports numerous monitoring systems, including:spring-doc.cn

To learn more about Micrometer’s capabilities, see its reference documentation, in particular the concepts section.

7.1. Getting started

Spring Boot auto-configures a composite MeterRegistry and adds a registry to the composite for each of the supported implementations that it finds on the classpath. Having a dependency on micrometer-registry-{system} in your runtime classpath is enough for Spring Boot to configure the registry.spring-doc.cn

Most registries share common features. For instance, you can disable a particular registry even if the Micrometer registry implementation is on the classpath. The following example disables Datadog:spring-doc.cn

Properties
management.datadog.metrics.export.enabled=false
Yaml
management:
  datadog:
    metrics:
      export:
        enabled: false

You can also disable all registries unless stated otherwise by the registry-specific property, as the following example shows:spring-doc.cn

Properties
management.defaults.metrics.export.enabled=false
Yaml
management:
  defaults:
    metrics:
      export:
        enabled: false

Spring Boot also adds any auto-configured registries to the global static composite registry on the Metrics class, unless you explicitly tell it not to:spring-doc.cn

Properties
management.metrics.use-global-registry=false
Yaml
management:
  metrics:
    use-global-registry: false

You can register any number of MeterRegistryCustomizer beans to further configure the registry, such as applying common tags, before any meters are registered with the registry:spring-doc.cn

Java
import io.micrometer.core.instrument.MeterRegistry;

import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyMeterRegistryConfiguration {

    @Bean
    public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return (registry) -> registry.config().commonTags("region", "us-east-1");
    }

}
Kotlin
import io.micrometer.core.instrument.MeterRegistry
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyMeterRegistryConfiguration {

    @Bean
    fun metricsCommonTags(): MeterRegistryCustomizer<MeterRegistry> {
        return MeterRegistryCustomizer { registry ->
            registry.config().commonTags("region", "us-east-1")
        }
    }

}

You can apply customizations to particular registry implementations by being more specific about the generic type:spring-doc.cn

Java
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.graphite.GraphiteMeterRegistry;

import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyMeterRegistryConfiguration {

    @Bean
    public MeterRegistryCustomizer<GraphiteMeterRegistry> graphiteMetricsNamingConvention() {
        return (registry) -> registry.config().namingConvention(this::name);
    }

    private String name(String name, Meter.Type type, String baseUnit) {
        return ...
    }

}
Kotlin
import io.micrometer.core.instrument.Meter
import io.micrometer.core.instrument.config.NamingConvention
import io.micrometer.graphite.GraphiteMeterRegistry
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyMeterRegistryConfiguration {

    @Bean
    fun graphiteMetricsNamingConvention(): MeterRegistryCustomizer<GraphiteMeterRegistry> {
        return MeterRegistryCustomizer { registry: GraphiteMeterRegistry ->
            registry.config().namingConvention(this::name)
        }
    }

    private fun name(name: String, type: Meter.Type, baseUnit: String?): String {
        return  ...
    }

}

Spring Boot also configures built-in instrumentation that you can control through configuration or dedicated annotation markers.spring-doc.cn

7.2. Supported Monitoring Systems

This section briefly describes each of the supported monitoring systems.spring-doc.cn

7.2.1. AppOptics

By default, the AppOptics registry periodically pushes metrics to api.appoptics.com/v1/measurements. To export metrics to SaaS AppOptics, your API token must be provided:spring-doc.cn

Properties
management.appoptics.metrics.export.api-token=YOUR_TOKEN
Yaml
management:
  appoptics:
    metrics:
      export:
        api-token: "YOUR_TOKEN"

7.2.2. Atlas

By default, metrics are exported to Atlas running on your local machine. You can provide the location of the Atlas server:spring-doc.cn

Properties
management.atlas.metrics.export.uri=https://atlas.example.com:7101/api/v1/publish
Yaml
management:
  atlas:
    metrics:
      export:
        uri: "https://atlas.example.com:7101/api/v1/publish"

7.2.3. Datadog

A Datadog registry periodically pushes metrics to datadoghq. To export metrics to Datadog, you must provide your API key:spring-doc.cn

Properties
management.datadog.metrics.export.api-key=YOUR_KEY
Yaml
management:
  datadog:
    metrics:
      export:
        api-key: "YOUR_KEY"

If you additionally provide an application key (optional), then metadata such as meter descriptions, types, and base units will also be exported:spring-doc.cn

Properties
management.datadog.metrics.export.api-key=YOUR_API_KEY
management.datadog.metrics.export.application-key=YOUR_APPLICATION_KEY
Yaml
management:
  datadog:
    metrics:
      export:
        api-key: "YOUR_API_KEY"
        application-key: "YOUR_APPLICATION_KEY"

By default, metrics are sent to the Datadog US site (api.datadoghq.com). If your Datadog project is hosted on one of the other sites, or you need to send metrics through a proxy, configure the URI accordingly:spring-doc.cn

Properties
management.datadog.metrics.export.uri=https://api.datadoghq.eu
Yaml
management:
  datadog:
    metrics:
      export:
        uri: "https://api.datadoghq.eu"

You can also change the interval at which metrics are sent to Datadog:spring-doc.cn

Properties
management.datadog.metrics.export.step=30s
Yaml
management:
  datadog:
    metrics:
      export:
        step: "30s"

7.2.4. Dynatrace

Dynatrace offers two metrics ingest APIs, both of which are implemented for Micrometer. You can find the Dynatrace documentation on Micrometer metrics ingest here. Configuration properties in the v1 namespace apply only when exporting to the Timeseries v1 API. Configuration properties in the v2 namespace apply only when exporting to the Metrics v2 API. Note that this integration can export only to either the v1 or v2 version of the API at a time, with v2 being preferred. If the device-id (required for v1 but not used in v2) is set in the v1 namespace, metrics are exported to the v1 endpoint. Otherwise, v2 is assumed.spring-doc.cn

v2 API

You can use the v2 API in two ways.spring-doc.cn

Auto-configuration

Dynatrace auto-configuration is available for hosts that are monitored by the OneAgent or by the Dynatrace Operator for Kubernetes.spring-doc.cn

Local OneAgent: If a OneAgent is running on the host, metrics are automatically exported to the local OneAgent ingest endpoint. The ingest endpoint forwards the metrics to the Dynatrace backend.spring-doc.cn

Dynatrace Kubernetes Operator: When running in Kubernetes with the Dynatrace Operator installed, the registry will automatically pick up your endpoint URI and API token from the operator instead.spring-doc.cn

This is the default behavior and requires no special setup beyond a dependency on io.micrometer:micrometer-registry-dynatrace.spring-doc.cn

Manual configuration

If no auto-configuration is available, the endpoint of the Metrics v2 API and an API token are required. The API token must have the “Ingest metrics” (metrics.ingest) permission set. We recommend limiting the scope of the token to this one permission. You must ensure that the endpoint URI contains the path (for example, /api/v2/metrics/ingest):spring-doc.cn

The URL of the Metrics API v2 ingest endpoint is different according to your deployment option:spring-doc.cn

  • SaaS: https://{your-environment-id}.live.dynatrace.com/api/v2/metrics/ingestspring-doc.cn

  • Managed deployments: https://{your-domain}/e/{your-environment-id}/api/v2/metrics/ingestspring-doc.cn

The example below configures metrics export using the example environment id:spring-doc.cn

Properties
management.dynatrace.metrics.export.uri=https://example.live.dynatrace.com/api/v2/metrics/ingest
management.dynatrace.metrics.export.api-token=YOUR_TOKEN
Yaml
management:
  dynatrace:
    metrics:
      export:
        uri: "https://example.live.dynatrace.com/api/v2/metrics/ingest"
        api-token: "YOUR_TOKEN"

When using the Dynatrace v2 API, the following optional features are available (more details can be found in the Dynatrace documentation):spring-doc.cn

  • Metric key prefix: Sets a prefix that is prepended to all exported metric keys.spring-doc.cn

  • Enrich with Dynatrace metadata: If a OneAgent or Dynatrace operator is running, enrich metrics with additional metadata (for example, about the host, process, or pod).spring-doc.cn

  • Default dimensions: Specify key-value pairs that are added to all exported metrics. If tags with the same key are specified with Micrometer, they overwrite the default dimensions.spring-doc.cn

  • Use Dynatrace Summary instruments: In some cases the Micrometer Dynatrace registry created metrics that were rejected. In Micrometer 1.9.x, this was fixed by introducing Dynatrace-specific summary instruments. Setting this toggle to false forces Micrometer to fall back to the behavior that was the default before 1.9.x. It should only be used when encountering problems while migrating from Micrometer 1.8.x to 1.9.x.spring-doc.cn

It is possible to not specify a URI and API token, as shown in the following example. In this scenario, the automatically configured endpoint is used:spring-doc.cn

Properties
management.dynatrace.metrics.export.v2.metric-key-prefix=your.key.prefix
management.dynatrace.metrics.export.v2.enrich-with-dynatrace-metadata=true
management.dynatrace.metrics.export.v2.default-dimensions.key1=value1
management.dynatrace.metrics.export.v2.default-dimensions.key2=value2
management.dynatrace.metrics.export.v2.use-dynatrace-summary-instruments=true
Yaml
management:
  dynatrace:
    metrics:
      export:
        # Specify uri and api-token here if not using the local OneAgent endpoint.
        v2:
          metric-key-prefix: "your.key.prefix"
          enrich-with-dynatrace-metadata: true
          default-dimensions:
            key1: "value1"
            key2: "value2"
          use-dynatrace-summary-instruments: true # (default: true)
v1 API (Legacy)

The Dynatrace v1 API metrics registry pushes metrics to the configured URI periodically by using the Timeseries v1 API. For backwards-compatibility with existing setups, when device-id is set (required for v1, but not used in v2), metrics are exported to the Timeseries v1 endpoint. To export metrics to Dynatrace, your API token, device ID, and URI must be provided:spring-doc.cn

Properties
management.dynatrace.metrics.export.uri=https://{your-environment-id}.live.dynatrace.com
management.dynatrace.metrics.export.api-token=YOUR_TOKEN
management.dynatrace.metrics.export.v1.device-id=YOUR_DEVICE_ID
Yaml
management:
  dynatrace:
    metrics:
      export:
        uri: "https://{your-environment-id}.live.dynatrace.com"
        api-token: "YOUR_TOKEN"
        v1:
          device-id: "YOUR_DEVICE_ID"

For the v1 API, you must specify the base environment URI without a path, as the v1 endpoint path is added automatically.spring-doc.cn

Version-independent Settings

In addition to the API endpoint and token, you can also change the interval at which metrics are sent to Dynatrace. The default export interval is 60s. The following example sets the export interval to 30 seconds:spring-doc.cn

Properties
management.dynatrace.metrics.export.step=30s
Yaml
management:
  dynatrace:
    metrics:
      export:
        step: "30s"

You can find more information on how to set up the Dynatrace exporter for Micrometer in the Micrometer documentation and the Dynatrace documentation.spring-doc.cn

7.2.5. Elastic

By default, metrics are exported to Elastic running on your local machine. You can provide the location of the Elastic server to use by using the following property:spring-doc.cn

Properties
management.elastic.metrics.export.host=https://elastic.example.com:8086
Yaml
management:
  elastic:
    metrics:
      export:
        host: "https://elastic.example.com:8086"

7.2.6. Ganglia

By default, metrics are exported to Ganglia running on your local machine. You can provide the Ganglia server host and port, as the following example shows:spring-doc.cn

Properties
management.ganglia.metrics.export.host=ganglia.example.com
management.ganglia.metrics.export.port=9649
Yaml
management:
  ganglia:
    metrics:
      export:
        host: "ganglia.example.com"
        port: 9649

7.2.7. Graphite

By default, metrics are exported to Graphite running on your local machine. You can provide the Graphite server host and port, as the following example shows:spring-doc.cn

Properties
management.graphite.metrics.export.host=graphite.example.com
management.graphite.metrics.export.port=9004
Yaml
management:
  graphite:
    metrics:
      export:
         host: "graphite.example.com"
         port: 9004

Micrometer provides a default HierarchicalNameMapper that governs how a dimensional meter ID is mapped to flat hierarchical names.spring-doc.cn

To take control over this behavior, define your GraphiteMeterRegistry and supply your own HierarchicalNameMapper. An auto-configured GraphiteConfig and Clock beans are provided unless you define your own:spring-doc.cn

Java
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.graphite.GraphiteConfig;
import io.micrometer.graphite.GraphiteMeterRegistry;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyGraphiteConfiguration {

    @Bean
    public GraphiteMeterRegistry graphiteMeterRegistry(GraphiteConfig config, Clock clock) {
        return new GraphiteMeterRegistry(config, clock, this::toHierarchicalName);
    }

    private String toHierarchicalName(Meter.Id id, NamingConvention convention) {
        return ...
    }

}
Kotlin
import io.micrometer.core.instrument.Clock
import io.micrometer.core.instrument.Meter
import io.micrometer.core.instrument.config.NamingConvention
import io.micrometer.core.instrument.util.HierarchicalNameMapper
import io.micrometer.graphite.GraphiteConfig
import io.micrometer.graphite.GraphiteMeterRegistry
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyGraphiteConfiguration {

    @Bean
    fun graphiteMeterRegistry(config: GraphiteConfig, clock: Clock): GraphiteMeterRegistry {
        return GraphiteMeterRegistry(config, clock, this::toHierarchicalName)
    }
    private fun toHierarchicalName(id: Meter.Id, convention: NamingConvention): String {
        return  ...
    }

}

7.2.8. Humio

By default, the Humio registry periodically pushes metrics to cloud.humio.com. To export metrics to SaaS Humio, you must provide your API token:spring-doc.cn

Properties
management.humio.metrics.export.api-token=YOUR_TOKEN
Yaml
management:
  humio:
    metrics:
      export:
        api-token: "YOUR_TOKEN"

You should also configure one or more tags to identify the data source to which metrics are pushed:spring-doc.cn

Properties
management.humio.metrics.export.tags.alpha=a
management.humio.metrics.export.tags.bravo=b
Yaml
management:
  humio:
    metrics:
      export:
        tags:
          alpha: "a"
          bravo: "b"

7.2.9. Influx

By default, metrics are exported to an Influx v1 instance running on your local machine with the default configuration. To export metrics to InfluxDB v2, configure the org, bucket, and authentication token for writing metrics. You can provide the location of the Influx server to use by using:spring-doc.cn

Properties
management.influx.metrics.export.uri=https://influx.example.com:8086
Yaml
management:
  influx:
    metrics:
      export:
        uri: "https://influx.example.com:8086"

7.2.10. JMX

Micrometer provides a hierarchical mapping to JMX, primarily as a cheap and portable way to view metrics locally. By default, metrics are exported to the metrics JMX domain. You can provide the domain to use by using:spring-doc.cn

Properties
management.jmx.metrics.export.domain=com.example.app.metrics
Yaml
management:
  jmx:
    metrics:
      export:
        domain: "com.example.app.metrics"

Micrometer provides a default HierarchicalNameMapper that governs how a dimensional meter ID is mapped to flat hierarchical names.spring-doc.cn

To take control over this behavior, define your JmxMeterRegistry and supply your own HierarchicalNameMapper. An auto-configured JmxConfig and Clock beans are provided unless you define your own:spring-doc.cn

Java
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyJmxConfiguration {

    @Bean
    public JmxMeterRegistry jmxMeterRegistry(JmxConfig config, Clock clock) {
        return new JmxMeterRegistry(config, clock, this::toHierarchicalName);
    }

    private String toHierarchicalName(Meter.Id id, NamingConvention convention) {
        return ...
    }

}
Kotlin
import io.micrometer.core.instrument.Clock
import io.micrometer.core.instrument.Meter
import io.micrometer.core.instrument.config.NamingConvention
import io.micrometer.core.instrument.util.HierarchicalNameMapper
import io.micrometer.jmx.JmxConfig
import io.micrometer.jmx.JmxMeterRegistry
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyJmxConfiguration {

    @Bean
    fun jmxMeterRegistry(config: JmxConfig, clock: Clock): JmxMeterRegistry {
        return JmxMeterRegistry(config, clock, this::toHierarchicalName)
    }

    private fun toHierarchicalName(id: Meter.Id, convention: NamingConvention): String {
        return  ...
    }

}

7.2.11. KairosDB

By default, metrics are exported to KairosDB running on your local machine. You can provide the location of the KairosDB server to use by using:spring-doc.cn

Properties
management.kairos.metrics.export.uri=https://kairosdb.example.com:8080/api/v1/datapoints
Yaml
management:
  kairos:
    metrics:
      export:
        uri: "https://kairosdb.example.com:8080/api/v1/datapoints"

7.2.12. New Relic

A New Relic registry periodically pushes metrics to New Relic. To export metrics to New Relic, you must provide your API key and account ID:spring-doc.cn

Properties
management.newrelic.metrics.export.api-key=YOUR_KEY
management.newrelic.metrics.export.account-id=YOUR_ACCOUNT_ID
Yaml
management:
  newrelic:
    metrics:
      export:
        api-key: "YOUR_KEY"
        account-id: "YOUR_ACCOUNT_ID"

You can also change the interval at which metrics are sent to New Relic:spring-doc.cn

Properties
management.newrelic.metrics.export.step=30s
Yaml
management:
  newrelic:
    metrics:
      export:
        step: "30s"

By default, metrics are published through REST calls, but you can also use the Java Agent API if you have it on the classpath:spring-doc.cn

Properties
management.newrelic.metrics.export.client-provider-type=insights-agent
Yaml
management:
  newrelic:
    metrics:
      export:
        client-provider-type: "insights-agent"

Finally, you can take full control by defining your own NewRelicClientProvider bean.spring-doc.cn

7.2.13. OpenTelemetry

By default, metrics are exported to OpenTelemetry running on your local machine. You can provide the location of the OpenTelemetry metric endpoint to use by using:spring-doc.cn

Properties
management.otlp.metrics.export.url=https://otlp.example.com:4318/v1/metrics
Yaml
management:
  otlp:
    metrics:
      export:
        url: "https://otlp.example.com:4318/v1/metrics"

7.2.14. Prometheus

Prometheus expects to scrape or poll individual application instances for metrics. Spring Boot provides an actuator endpoint at /actuator/prometheus to present a Prometheus scrape with the appropriate format.spring-doc.cn

By default, the endpoint is not available and must be exposed. See exposing endpoints for more details.

The following example scrape_config adds to prometheus.yml:spring-doc.cn

scrape_configs:
  - job_name: "spring"
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["HOST:PORT"]

Prometheus Exemplars are also supported. To enable this feature, a SpanContextSupplier bean should be present. If you use Micrometer Tracing, this will be auto-configured for you, but you can always create your own if you want. Please check the Prometheus Docs, since this feature needs to be explicitly enabled on Prometheus' side, and it is only supported using the OpenMetrics format.spring-doc.cn

For ephemeral or batch jobs that may not exist long enough to be scraped, you can use Prometheus Pushgateway support to expose the metrics to Prometheus. To enable Prometheus Pushgateway support, add the following dependency to your project:spring-doc.cn

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
</dependency>

When the Prometheus Pushgateway dependency is present on the classpath and the management.prometheus.metrics.export.pushgateway.enabled property is set to true, a PrometheusPushGatewayManager bean is auto-configured. This manages the pushing of metrics to a Prometheus Pushgateway.spring-doc.cn

You can tune the PrometheusPushGatewayManager by using properties under management.prometheus.metrics.export.pushgateway. For advanced configuration, you can also provide your own PrometheusPushGatewayManager bean.spring-doc.cn

7.2.15. SignalFx

SignalFx registry periodically pushes metrics to SignalFx. To export metrics to SignalFx, you must provide your access token:spring-doc.cn

Properties
management.signalfx.metrics.export.access-token=YOUR_ACCESS_TOKEN
Yaml
management:
  signalfx:
    metrics:
      export:
        access-token: "YOUR_ACCESS_TOKEN"

You can also change the interval at which metrics are sent to SignalFx:spring-doc.cn

Properties
management.signalfx.metrics.export.step=30s
Yaml
management:
  signalfx:
    metrics:
      export:
        step: "30s"

7.2.16. Simple

Micrometer ships with a simple, in-memory backend that is automatically used as a fallback if no other registry is configured. This lets you see what metrics are collected in the metrics endpoint.spring-doc.cn

The in-memory backend disables itself as soon as you use any other available backend. You can also disable it explicitly:spring-doc.cn

Properties
management.simple.metrics.export.enabled=false
Yaml
management:
  simple:
    metrics:
      export:
        enabled: false

7.2.17. Stackdriver

The Stackdriver registry periodically pushes metrics to Stackdriver. To export metrics to SaaS Stackdriver, you must provide your Google Cloud project ID:spring-doc.cn

Properties
management.stackdriver.metrics.export.project-id=my-project
Yaml
management:
  stackdriver:
    metrics:
      export:
        project-id: "my-project"

You can also change the interval at which metrics are sent to Stackdriver:spring-doc.cn

Properties
management.stackdriver.metrics.export.step=30s
Yaml
management:
  stackdriver:
    metrics:
      export:
        step: "30s"

7.2.18. StatsD

The StatsD registry eagerly pushes metrics over UDP to a StatsD agent. By default, metrics are exported to a StatsD agent running on your local machine. You can provide the StatsD agent host, port, and protocol to use by using:spring-doc.cn

Properties
management.statsd.metrics.export.host=statsd.example.com
management.statsd.metrics.export.port=9125
management.statsd.metrics.export.protocol=udp
Yaml
management:
  statsd:
    metrics:
      export:
        host: "statsd.example.com"
        port: 9125
        protocol: "udp"

You can also change the StatsD line protocol to use (it defaults to Datadog):spring-doc.cn

Properties
management.statsd.metrics.export.flavor=etsy
Yaml
management:
  statsd:
    metrics:
      export:
        flavor: "etsy"

7.2.19. Wavefront

The Wavefront registry periodically pushes metrics to Wavefront. If you are exporting metrics to Wavefront directly, you must provide your API token:spring-doc.cn

Properties
management.wavefront.api-token=YOUR_API_TOKEN
Yaml
management:
  wavefront:
    api-token: "YOUR_API_TOKEN"

Alternatively, you can use a Wavefront sidecar or an internal proxy in your environment to forward metrics data to the Wavefront API host:spring-doc.cn

Properties
management.wavefront.uri=proxy://localhost:2878
Yaml
management:
  wavefront:
    uri: "proxy://localhost:2878"
If you publish metrics to a Wavefront proxy (as described in the Wavefront documentation), the host must be in the proxy://HOST:PORT format.

You can also change the interval at which metrics are sent to Wavefront:spring-doc.cn

Properties
management.wavefront.metrics.export.step=30s
Yaml
management:
  wavefront:
    metrics:
      export:
        step: "30s"

7.3. Supported Metrics and Meters

Spring Boot provides automatic meter registration for a wide variety of technologies. In most situations, the defaults provide sensible metrics that can be published to any of the supported monitoring systems.spring-doc.cn

7.3.1. JVM Metrics

Auto-configuration enables JVM Metrics by using core Micrometer classes. JVM metrics are published under the jvm. meter name.spring-doc.cn

The following JVM metrics are provided:spring-doc.cn

7.3.2. System Metrics

Auto-configuration enables system metrics by using core Micrometer classes. System metrics are published under the system., process., and disk. meter names.spring-doc.cn

The following system metrics are provided:spring-doc.cn

7.3.3. Application Startup Metrics

Auto-configuration exposes application startup time metrics:spring-doc.cn

  • application.started.time: time taken to start the application.spring-doc.cn

  • application.ready.time: time taken for the application to be ready to service requests.spring-doc.cn

Metrics are tagged by the fully qualified name of the application class.spring-doc.cn

7.3.4. Logger Metrics

Auto-configuration enables the event metrics for both Logback and Log4J2. The details are published under the log4j2.events. or logback.events. meter names.spring-doc.cn

7.3.5. Task Execution and Scheduling Metrics

Auto-configuration enables the instrumentation of all available ThreadPoolTaskExecutor and ThreadPoolTaskScheduler beans, as long as the underling ThreadPoolExecutor is available. Metrics are tagged by the name of the executor, which is derived from the bean name.spring-doc.cn

7.3.6. Spring MVC Metrics

Auto-configuration enables the instrumentation of all requests handled by Spring MVC controllers and functional handlers. By default, metrics are generated with the name, http.server.requests. You can customize the name by setting the management.observations.http.server.requests.name property.spring-doc.cn

To add to the default tags, provide a @Bean that extends DefaultServerRequestObservationConvention from the org.springframework.http.server.observation package. To replace the default tags, provide a @Bean that implements ServerRequestObservationConvention.spring-doc.cn

In some cases, exceptions handled in web controllers are not recorded as request metrics tags. Applications can opt in and record exceptions by setting handled exceptions as request attributes.

By default, all requests are handled. To customize the filter, provide a @Bean that implements FilterRegistrationBean<WebMvcMetricsFilter>.spring-doc.cn

7.3.7. Spring WebFlux Metrics

Auto-configuration enables the instrumentation of all requests handled by Spring WebFlux controllers and functional handlers. By default, metrics are generated with the name, http.server.requests. You can customize the name by setting the management.observations.http.server.requests.name property.spring-doc.cn

To add to the default tags, provide a @Bean that extends DefaultServerRequestObservationConvention from the org.springframework.http.server.reactive.observation package. To replace the default tags, provide a @Bean that implements ServerRequestObservationConvention.spring-doc.cn

In some cases, exceptions handled in controllers and handler functions are not recorded as request metrics tags. Applications can opt in and record exceptions by setting handled exceptions as request attributes.

7.3.8. Jersey Server Metrics

Auto-configuration enables the instrumentation of all requests handled by the Jersey JAX-RS implementation. By default, metrics are generated with the name, http.server.requests. You can customize the name by setting the management.observations.http.server.requests.name property.spring-doc.cn

By default, Jersey server metrics are tagged with the following information:spring-doc.cn

Tag Description

exceptionspring-doc.cn

The simple class name of any exception that was thrown while handling the request.spring-doc.cn

methodspring-doc.cn

The request’s method (for example, GET or POST)spring-doc.cn

outcomespring-doc.cn

The request’s outcome, based on the status code of the response. 1xx is INFORMATIONAL, 2xx is SUCCESS, 3xx is REDIRECTION, 4xx is CLIENT_ERROR, and 5xx is SERVER_ERRORspring-doc.cn

statusspring-doc.cn

The response’s HTTP status code (for example, 200 or 500)spring-doc.cn

urispring-doc.cn

The request’s URI template prior to variable substitution, if possible (for example, /api/person/{id})spring-doc.cn

To customize the tags, provide a @Bean that implements JerseyTagsProvider.spring-doc.cn

7.3.9. HTTP Client Metrics

Spring Boot Actuator manages the instrumentation of both RestTemplate and WebClient. For that, you have to inject the auto-configured builder and use it to create instances:spring-doc.cn

You can also manually apply the customizers responsible for this instrumentation, namely ObservationRestTemplateCustomizer and ObservationWebClientCustomizer.spring-doc.cn

By default, metrics are generated with the name, http.client.requests. You can customize the name by setting the management.observations.http.client.requests.name property.spring-doc.cn

To customize the tags when using RestTemplate, provide a @Bean that implements ClientRequestObservationConvention from the org.springframework.http.client.observation package. To customize the tags when using WebClient, provide a @Bean that implements ClientRequestObservationConvention from the org.springframework.web.reactive.function.client package.spring-doc.cn

7.3.10. Tomcat Metrics

Auto-configuration enables the instrumentation of Tomcat only when an MBeanRegistry is enabled. By default, the MBeanRegistry is disabled, but you can enable it by setting server.tomcat.mbeanregistry.enabled to true.spring-doc.cn

Tomcat metrics are published under the tomcat. meter name.spring-doc.cn

7.3.11. Cache Metrics

Auto-configuration enables the instrumentation of all available Cache instances on startup, with metrics prefixed with cache. Cache instrumentation is standardized for a basic set of metrics. Additional, cache-specific metrics are also available.spring-doc.cn

The following cache libraries are supported:spring-doc.cn

Metrics are tagged by the name of the cache and by the name of the CacheManager, which is derived from the bean name.spring-doc.cn

Only caches that are configured on startup are bound to the registry. For caches not defined in the cache’s configuration, such as caches created on the fly or programmatically after the startup phase, an explicit registration is required. A CacheMetricsRegistrar bean is made available to make that process easier.

7.3.12. Spring Batch Metrics

7.3.13. Spring GraphQL Metrics

7.3.14. DataSource Metrics

Auto-configuration enables the instrumentation of all available DataSource objects with metrics prefixed with jdbc.connections. Data source instrumentation results in gauges that represent the currently active, idle, maximum allowed, and minimum allowed connections in the pool.spring-doc.cn

Metrics are also tagged by the name of the DataSource computed based on the bean name.spring-doc.cn

By default, Spring Boot provides metadata for all supported data sources. You can add additional DataSourcePoolMetadataProvider beans if your favorite data source is not supported. See DataSourcePoolMetadataProvidersConfiguration for examples.

Also, Hikari-specific metrics are exposed with a hikaricp prefix. Each metric is tagged by the name of the pool (you can control it with spring.datasource.name).spring-doc.cn

7.3.15. Hibernate Metrics

If org.hibernate.orm:hibernate-micrometer is on the classpath, all available Hibernate EntityManagerFactory instances that have statistics enabled are instrumented with a metric named hibernate.spring-doc.cn

Metrics are also tagged by the name of the EntityManagerFactory, which is derived from the bean name.spring-doc.cn

To enable statistics, the standard JPA property hibernate.generate_statistics must be set to true. You can enable that on the auto-configured EntityManagerFactory:spring-doc.cn

Properties
spring.jpa.properties[hibernate.generate_statistics]=true
Yaml
spring:
  jpa:
    properties:
      "[hibernate.generate_statistics]": true

7.3.16. Spring Data Repository Metrics

Auto-configuration enables the instrumentation of all Spring Data Repository method invocations. By default, metrics are generated with the name, spring.data.repository.invocations. You can customize the name by setting the management.metrics.data.repository.metric-name property.spring-doc.cn

The @Timed annotation from the io.micrometer.core.annotation package is supported on Repository interfaces and methods. If you do not want to record metrics for all Repository invocations, you can set management.metrics.data.repository.autotime.enabled to false and exclusively use @Timed annotations instead.spring-doc.cn

A @Timed annotation with longTask = true enables a long task timer for the method. Long task timers require a separate metric name and can be stacked with a short task timer.

By default, repository invocation related metrics are tagged with the following information:spring-doc.cn

Tag Description

repositoryspring-doc.cn

The simple class name of the source Repository.spring-doc.cn

methodspring-doc.cn

The name of the Repository method that was invoked.spring-doc.cn

statespring-doc.cn

The result state (SUCCESS, ERROR, CANCELED, or RUNNING).spring-doc.cn

exceptionspring-doc.cn

The simple class name of any exception that was thrown from the invocation.spring-doc.cn

To replace the default tags, provide a @Bean that implements RepositoryTagsProvider.spring-doc.cn

7.3.17. RabbitMQ Metrics

Auto-configuration enables the instrumentation of all available RabbitMQ connection factories with a metric named rabbitmq.spring-doc.cn

7.3.18. Spring Integration Metrics

Spring Integration automatically provides Micrometer support whenever a MeterRegistry bean is available. Metrics are published under the spring.integration. meter name.spring-doc.cn

7.3.19. Kafka Metrics

Auto-configuration registers a MicrometerConsumerListener and MicrometerProducerListener for the auto-configured consumer factory and producer factory, respectively. It also registers a KafkaStreamsMicrometerListener for StreamsBuilderFactoryBean. For more detail, see the Micrometer Native Metrics section of the Spring Kafka documentation.spring-doc.cn

7.3.20. MongoDB Metrics

This section briefly describes the available metrics for MongoDB.spring-doc.cn

MongoDB Command Metrics

Auto-configuration registers a MongoMetricsCommandListener with the auto-configured MongoClient.spring-doc.cn

A timer metric named mongodb.driver.commands is created for each command issued to the underlying MongoDB driver. Each metric is tagged with the following information by default:spring-doc.cn

Tag Description

commandspring-doc.cn

The name of the command issued.spring-doc.cn

cluster.idspring-doc.cn

The identifier of the cluster to which the command was sent.spring-doc.cn

server.addressspring-doc.cn

The address of the server to which the command was sent.spring-doc.cn

statusspring-doc.cn

The outcome of the command (SUCCESS or FAILED).spring-doc.cn

To replace the default metric tags, define a MongoCommandTagsProvider bean, as the following example shows:spring-doc.cn

Java
import io.micrometer.core.instrument.binder.mongodb.MongoCommandTagsProvider;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyCommandTagsProviderConfiguration {

    @Bean
    public MongoCommandTagsProvider customCommandTagsProvider() {
        return new CustomCommandTagsProvider();
    }

}
Kotlin
import io.micrometer.core.instrument.binder.mongodb.MongoCommandTagsProvider
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyCommandTagsProviderConfiguration {

    @Bean
    fun customCommandTagsProvider(): MongoCommandTagsProvider? {
        return CustomCommandTagsProvider()
    }

}

To disable the auto-configured command metrics, set the following property:spring-doc.cn

Properties
management.metrics.mongo.command.enabled=false
Yaml
management:
  metrics:
    mongo:
      command:
        enabled: false
MongoDB Connection Pool Metrics

Auto-configuration registers a MongoMetricsConnectionPoolListener with the auto-configured MongoClient.spring-doc.cn

The following gauge metrics are created for the connection pool:spring-doc.cn

  • mongodb.driver.pool.size reports the current size of the connection pool, including idle and and in-use members.spring-doc.cn

  • mongodb.driver.pool.checkedout reports the count of connections that are currently in use.spring-doc.cn

  • mongodb.driver.pool.waitqueuesize reports the current size of the wait queue for a connection from the pool.spring-doc.cn

Each metric is tagged with the following information by default:spring-doc.cn

Tag Description

cluster.idspring-doc.cn

The identifier of the cluster to which the connection pool corresponds.spring-doc.cn

server.addressspring-doc.cn

The address of the server to which the connection pool corresponds.spring-doc.cn

To replace the default metric tags, define a MongoConnectionPoolTagsProvider bean:spring-doc.cn

Java
import io.micrometer.core.instrument.binder.mongodb.MongoConnectionPoolTagsProvider;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyConnectionPoolTagsProviderConfiguration {

    @Bean
    public MongoConnectionPoolTagsProvider customConnectionPoolTagsProvider() {
        return new CustomConnectionPoolTagsProvider();
    }

}
Kotlin
import io.micrometer.core.instrument.binder.mongodb.MongoConnectionPoolTagsProvider
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyConnectionPoolTagsProviderConfiguration {

    @Bean
    fun customConnectionPoolTagsProvider(): MongoConnectionPoolTagsProvider {
        return CustomConnectionPoolTagsProvider()
    }

}

To disable the auto-configured connection pool metrics, set the following property:spring-doc.cn

Properties
management.metrics.mongo.connectionpool.enabled=false
Yaml
management:
  metrics:
    mongo:
      connectionpool:
        enabled: false

7.3.21. Jetty Metrics

Auto-configuration binds metrics for Jetty’s ThreadPool by using Micrometer’s JettyServerThreadPoolMetrics. Metrics for Jetty’s Connector instances are bound by using Micrometer’s JettyConnectionMetrics and, when server.ssl.enabled is set to true, Micrometer’s JettySslHandshakeMetrics.spring-doc.cn

7.3.22. @Timed Annotation Support

To use @Timed where it is not directly supported by Spring Boot, refer to the Micrometer documentation.spring-doc.cn

7.3.23. Redis Metrics

Auto-configuration registers a MicrometerCommandLatencyRecorder for the auto-configured LettuceConnectionFactory. For more detail, see the Micrometer Metrics section of the Lettuce documentation.spring-doc.cn

7.4. Registering Custom Metrics

To register custom metrics, inject MeterRegistry into your component:spring-doc.cn

Java
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;

import org.springframework.stereotype.Component;

@Component
public class MyBean {

    private final Dictionary dictionary;

    public MyBean(MeterRegistry registry) {
        this.dictionary = Dictionary.load();
        registry.gauge("dictionary.size", Tags.empty(), this.dictionary.getWords().size());
    }

}
Kotlin
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tags
import org.springframework.stereotype.Component

@Component
class MyBean(registry: MeterRegistry) {

    private val dictionary: Dictionary

    init {
        dictionary = Dictionary.load()
        registry.gauge("dictionary.size", Tags.empty(), dictionary.words.size)
    }

}

If your metrics depend on other beans, we recommend that you use a MeterBinder to register them:spring-doc.cn

Java
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.binder.MeterBinder;

import org.springframework.context.annotation.Bean;

public class MyMeterBinderConfiguration {

    @Bean
    public MeterBinder queueSize(Queue queue) {
        return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
    }

}
Kotlin
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.binder.MeterBinder
import org.springframework.context.annotation.Bean

class MyMeterBinderConfiguration {

    @Bean
    fun queueSize(queue: Queue): MeterBinder {
        return MeterBinder { registry ->
            Gauge.builder("queueSize", queue::size).register(registry)
        }
    }

}

Using a MeterBinder ensures that the correct dependency relationships are set up and that the bean is available when the metric’s value is retrieved. A MeterBinder implementation can also be useful if you find that you repeatedly instrument a suite of metrics across components or applications.spring-doc.cn

By default, metrics from all MeterBinder beans are automatically bound to the Spring-managed MeterRegistry.

7.5. Customizing Individual Metrics

If you need to apply customizations to specific Meter instances, you can use the io.micrometer.core.instrument.config.MeterFilter interface.spring-doc.cn

For example, if you want to rename the mytag.region tag to mytag.area for all meter IDs beginning with com.example, you can do the following:spring-doc.cn

Java
import io.micrometer.core.instrument.config.MeterFilter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyMetricsFilterConfiguration {

    @Bean
    public MeterFilter renameRegionTagMeterFilter() {
        return MeterFilter.renameTag("com.example", "mytag.region", "mytag.area");
    }

}
Kotlin
import io.micrometer.core.instrument.config.MeterFilter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyMetricsFilterConfiguration {

    @Bean
    fun renameRegionTagMeterFilter(): MeterFilter {
        return MeterFilter.renameTag("com.example", "mytag.region", "mytag.area")
    }

}
By default, all MeterFilter beans are automatically bound to the Spring-managed MeterRegistry. Make sure to register your metrics by using the Spring-managed MeterRegistry and not any of the static methods on Metrics. These use the global registry that is not Spring-managed.

7.5.1. Common Tags

Common tags are generally used for dimensional drill-down on the operating environment, such as host, instance, region, stack, and others. Commons tags are applied to all meters and can be configured, as the following example shows:spring-doc.cn

Properties
management.metrics.tags.region=us-east-1
management.metrics.tags.stack=prod
Yaml
management:
  metrics:
    tags:
      region: "us-east-1"
      stack: "prod"

The preceding example adds region and stack tags to all meters with a value of us-east-1 and prod, respectively.spring-doc.cn

The order of common tags is important if you use Graphite. As the order of common tags cannot be guaranteed by using this approach, Graphite users are advised to define a custom MeterFilter instead.

7.5.2. Per-meter Properties

In addition to MeterFilter beans, you can apply a limited set of customization on a per-meter basis using properties. Per-meter customizations are applied, using Spring Boot’s PropertiesMeterFilter, to any meter IDs that start with the given name. The following example filters out any meters that have an ID starting with example.remote.spring-doc.cn

Properties
management.metrics.enable.example.remote=false
Yaml
management:
  metrics:
    enable:
      example:
        remote: false

The following properties allow per-meter customization:spring-doc.cn

Table 1. Per-meter customizations
Property Description

management.metrics.enablespring-doc.cn

Whether to accept meters with certain IDs. Meters that are not accepted are filtered from the MeterRegistry.spring-doc.cn

management.metrics.distribution.percentiles-histogramspring-doc.cn

Whether to publish a histogram suitable for computing aggregable (across dimension) percentile approximations.spring-doc.cn

management.metrics.distribution.minimum-expected-value, management.metrics.distribution.maximum-expected-valuespring-doc.cn

Publish fewer histogram buckets by clamping the range of expected values.spring-doc.cn

management.metrics.distribution.percentilesspring-doc.cn

Publish percentile values computed in your applicationspring-doc.cn

management.metrics.distribution.expiry, management.metrics.distribution.buffer-lengthspring-doc.cn

Give greater weight to recent samples by accumulating them in ring buffers which rotate after a configurable expiry, with a configurable buffer length.spring-doc.cn

management.metrics.distribution.slospring-doc.cn

Publish a cumulative histogram with buckets defined by your service-level objectives.spring-doc.cn

For more details on the concepts behind percentiles-histogram, percentiles, and slo, see the “Histograms and percentiles” section of the Micrometer documentation.spring-doc.cn

7.6. Metrics Endpoint

Spring Boot provides a metrics endpoint that you can use diagnostically to examine the metrics collected by an application. The endpoint is not available by default and must be exposed. See exposing endpoints for more details.spring-doc.cn

Navigating to /actuator/metrics displays a list of available meter names. You can drill down to view information about a particular meter by providing its name as a selector — for example, /actuator/metrics/jvm.memory.max.spring-doc.cn

The name you use here should match the name used in the code, not the name after it has been naming-convention normalized for a monitoring system to which it is shipped. In other words, if jvm.memory.max appears as jvm_memory_max in Prometheus because of its snake case naming convention, you should still use jvm.memory.max as the selector when inspecting the meter in the metrics endpoint.spring-doc.cn

You can also add any number of tag=KEY:VALUE query parameters to the end of the URL to dimensionally drill down on a meter — for example, /actuator/metrics/jvm.memory.max?tag=area:nonheap.spring-doc.cn

The reported measurements are the sum of the statistics of all meters that match the meter name and any tags that have been applied. In the preceding example, the returned Value statistic is the sum of the maximum memory footprints of the “Code Cache”, “Compressed Class Space”, and “Metaspace” areas of the heap. If you wanted to see only the maximum size for the “Metaspace”, you could add an additional tag=id:Metaspace — that is, /actuator/metrics/jvm.memory.max?tag=area:nonheap&tag=id:Metaspace.spring-doc.cn

7.7. Integration with Micrometer Observation

A DefaultMeterObservationHandler is automatically registered on the ObservationRegistry, which creates metrics for every completed observation.spring-doc.cn

8. Tracing

Spring Boot Actuator provides dependency management and auto-configuration for Micrometer Tracing, a facade for popular tracer libraries.spring-doc.cn

To learn more about Micrometer Tracing capabilities, see its reference documentation.

8.1. Supported Tracers

Spring Boot ships auto-configuration for the following tracers:spring-doc.cn

8.2. Getting Started

We need an example application that we can use to get started with tracing. For our purposes, the simple “Hello World!” web application that’s covered in the “getting-started.html” section will suffice. We’re going to use the OpenTelemetry tracer with Zipkin as trace backend.spring-doc.cn

To recap, our main application code looks like this:spring-doc.cn

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyApplication {

    private static final Log logger = LogFactory.getLog(MyApplication.class);

    @RequestMapping("/")
    String home() {
        logger.info("home() has been called");
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}
There’s an added logger statement in the home() method, which will be important later.

Now we have to add the following dependencies:spring-doc.cn

  • org.springframework.boot:spring-boot-starter-actuatorspring-doc.cn

  • io.micrometer:micrometer-tracing-bridge-otel - which is needed to bridge the Micrometer Observation API to OpenTelemetry.spring-doc.cn

  • io.opentelemetry:opentelemetry-exporter-zipkin - which is needed to report traces to Zipkin.spring-doc.cn

Add the following application properties:spring-doc.cn

Properties
management.tracing.sampling.probability=1.0
Yaml
management:
  tracing:
    sampling:
      probability: 1.0

By default, Spring Boot samples only 10% of requests to prevent overwhelming the trace backend. This property switches it to 100% so that every request is sent to the trace backend.spring-doc.cn

To collect and visualize the traces, we need a running trace backend. We use Zipkin as our trace backend here. The Zipkin Quickstart guide provides instructions how to start Zipkin locally.spring-doc.cn

After Zipkin is running, you can start your application.spring-doc.cn

If you open a web browser to localhost:8080, you should see the following output:spring-doc.cn

Hello World!

Behind the scenes, an observation has been created for the HTTP request, which in turn gets bridged to OpenTelemetry, which reports a new trace to Zipkin.spring-doc.cn

Now open the Zipkin UI at localhost:9411 and press the "Run Query" button to list all collected traces. You should see one trace. Press the "Show" button to see the details of that trace.spring-doc.cn

You can include the current trace and span id in the logs by setting the logging.pattern.level property to %5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

8.3. Propagating Traces

To automatically propagate traces over the network, use the auto-configured RestTemplateBuilder or WebClient.Builder to construct the client.spring-doc.cn

If you create the WebClient or the RestTemplate without using the auto-configured builders, automatic trace propagation won’t work!

8.4. Tracer Implementations

As Micrometer Tracer supports multiple tracer implementations, there are multiple dependency combinations possible with Spring Boot.spring-doc.cn

All tracer implementations need the org.springframework.boot:spring-boot-starter-actuator dependency.spring-doc.cn

8.4.1. OpenTelemetry With Zipkin

Tracing with OpenTelemetry and reporting to Zipkin requires the following dependencies:spring-doc.cn

  • io.micrometer:micrometer-tracing-bridge-otel - bridges the Micrometer Observation API to OpenTelemetry.spring-doc.cn

  • io.opentelemetry:opentelemetry-exporter-zipkin - reports traces to Zipkin.spring-doc.cn

Use the management.zipkin.tracing.* configuration properties to configure reporting to Zipkin.spring-doc.cn

8.4.2. OpenTelemetry With Wavefront

Tracing with OpenTelemetry and reporting to Wavefront requires the following dependencies:spring-doc.cn

  • io.micrometer:micrometer-tracing-bridge-otel - bridges the Micrometer Observation API to OpenTelemetry.spring-doc.cn

  • io.micrometer:micrometer-tracing-reporter-wavefront - reports traces to Wavefront.spring-doc.cn

Use the management.wavefront.* configuration properties to configure reporting to Wavefront.spring-doc.cn

8.4.3. OpenZipkin Brave With Zipkin

Tracing with OpenZipkin Brave and reporting to Zipkin requires the following dependencies:spring-doc.cn

  • io.micrometer:micrometer-tracing-bridge-brave - bridges the Micrometer Observation API to Brave.spring-doc.cn

  • io.zipkin.reporter2:zipkin-reporter-brave - reports traces to Zipkin.spring-doc.cn

If your project doesn’t use Spring MVC or Spring WebFlux, the io.zipkin.reporter2:zipkin-sender-urlconnection dependency is needed, too.

Use the management.zipkin.tracing.* configuration properties to configure reporting to Zipkin.spring-doc.cn

8.4.4. OpenZipkin Brave With Wavefront

Tracing with OpenZipkin Brave and reporting to Wavefront requires the following dependencies:spring-doc.cn

  • io.micrometer:micrometer-tracing-bridge-brave - which is needed to bridge the Micrometer Observation API to Brave.spring-doc.cn

  • io.micrometer:micrometer-tracing-reporter-wavefront - which is needed to report traces to Wavefront.spring-doc.cn

Use the management.wavefront.* configuration properties to configure reporting to Wavefront.spring-doc.cn

8.5. Integration with Micrometer Observation

A TracingAwareMeterObservationHandler is automatically registered on the ObservationRegistry, which creates spans for every completed observation.spring-doc.cn

8.6. Creating Custom Spans

You can create your own spans by starting an observation. For this, inject ObservationRegistry into your component:spring-doc.cn

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;

import org.springframework.stereotype.Component;

@Component
class CustomObservation {

    private final ObservationRegistry observationRegistry;

    CustomObservation(ObservationRegistry observationRegistry) {
        this.observationRegistry = observationRegistry;
    }

    void someOperation() {
        Observation observation = Observation.createNotStarted("some-operation", this.observationRegistry);
        observation.lowCardinalityKeyValue("some-tag", "some-value");
        observation.observe(() -> {
            // Business logic ...
        });
    }

}

This will create an observation named "some-operation" with the tag "some-tag=some-value".spring-doc.cn

If you want to create a span without creating a metric, you need to use the lower-level Tracer API from Micrometer.

8.7. Baggage

You can create baggage with the Tracer API:spring-doc.cn

import io.micrometer.tracing.BaggageInScope;
import io.micrometer.tracing.Tracer;

import org.springframework.stereotype.Component;

@Component
class CreatingBaggage {

    private final Tracer tracer;

    CreatingBaggage(Tracer tracer) {
        this.tracer = tracer;
    }

    void doSomething() {
        try (BaggageInScope scope = this.tracer.createBaggage("baggage1", "value1").makeCurrent()) {
            // Business logic
        }
    }

}

This example creates baggage named baggage1 with the value value1. The baggage is automatically propagated over the network if you’re using W3C propagation. If you’re using B3 propagation, baggage is not automatically propagated. To manually propagate baggage over the network, use the management.tracing.baggage.remote-fields configuration property (this works for W3C, too). For the example above, setting this property to baggage1 results in an HTTP header baggage1: value1.spring-doc.cn

If you want to propagate the baggage to the MDC, use the management.tracing.baggage.correlation.fields configuration property. For the example above, setting this property to baggage1 results in an MDC entry named baggage1.spring-doc.cn

9. Auditing

Once Spring Security is in play, Spring Boot Actuator has a flexible audit framework that publishes events (by default, “authentication success”, “failure” and “access denied” exceptions). This feature can be very useful for reporting and for implementing a lock-out policy based on authentication failures.spring-doc.cn

You can enable auditing by providing a bean of type AuditEventRepository in your application’s configuration. For convenience, Spring Boot offers an InMemoryAuditEventRepository. InMemoryAuditEventRepository has limited capabilities, and we recommend using it only for development environments. For production environments, consider creating your own alternative AuditEventRepository implementation.spring-doc.cn

9.1. Custom Auditing

To customize published security events, you can provide your own implementations of AbstractAuthenticationAuditListener and AbstractAuthorizationAuditListener.spring-doc.cn

You can also use the audit services for your own business events. To do so, either inject the AuditEventRepository bean into your own components and use that directly or publish an AuditApplicationEvent with the Spring ApplicationEventPublisher (by implementing ApplicationEventPublisherAware).spring-doc.cn

10. Recording HTTP Exchanges

You can enable recording of HTTP exchanges by providing a bean of type HttpExchangeRepository in your application’s configuration. For convenience, Spring Boot offers InMemoryHttpExchangeRepository, which, by default, stores the last 100 request-response exchanges. InMemoryHttpExchangeRepository is limited compared to tracing solutions, and we recommend using it only for development environments. For production environments, we recommend using a production-ready tracing or observability solution, such as Zipkin or OpenTelemetry. Alternatively, you can create your own HttpExchangeRepository.spring-doc.cn

You can use the httpexchanges endpoint to obtain information about the request-response exchanges that are stored in the HttpExchangeRepository.spring-doc.cn

10.1. Custom HTTP Exchange Recording

To customize the items that are included in each recorded exchange, use the management.httpexchanges.recording.include configuration property.spring-doc.cn

To disable recoding entirely, set management.httpexchanges.recording.enabled to false.spring-doc.cn

11. Process Monitoring

In the spring-boot module, you can find two classes to create files that are often useful for process monitoring:spring-doc.cn

  • ApplicationPidFileWriter creates a file that contains the application PID (by default, in the application directory with a file name of application.pid).spring-doc.cn

  • WebServerPortFileWriter creates a file (or files) that contain the ports of the running web server (by default, in the application directory with a file name of application.port).spring-doc.cn

By default, these writers are not activated, but you can enable them:spring-doc.cn

11.1. Extending Configuration

In the META-INF/spring.factories file, you can activate the listener (or listeners) that writes a PID file:spring-doc.cn

org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter

11.2. Programmatically Enabling Process Monitoring

You can also activate a listener by invoking the SpringApplication.addListeners(…​) method and passing the appropriate Writer object. This method also lets you customize the file name and path in the Writer constructor.spring-doc.cn

12. Cloud Foundry Support

Spring Boot’s actuator module includes additional support that is activated when you deploy to a compatible Cloud Foundry instance. The /cloudfoundryapplication path provides an alternative secured route to all @Endpoint beans.spring-doc.cn

The extended support lets Cloud Foundry management UIs (such as the web application that you can use to view deployed applications) be augmented with Spring Boot actuator information. For example, an application status page can include full health information instead of the typical “running” or “stopped” status.spring-doc.cn

The /cloudfoundryapplication path is not directly accessible to regular users. To use the endpoint, you must pass a valid UAA token with the request.

12.1. Disabling Extended Cloud Foundry Actuator Support

If you want to fully disable the /cloudfoundryapplication endpoints, you can add the following setting to your application.properties file:spring-doc.cn

Properties
management.cloudfoundry.enabled=false
Yaml
management:
  cloudfoundry:
    enabled: false

12.2. Cloud Foundry Self-signed Certificates

By default, the security verification for /cloudfoundryapplication endpoints makes SSL calls to various Cloud Foundry services. If your Cloud Foundry UAA or Cloud Controller services use self-signed certificates, you need to set the following property:spring-doc.cn

Properties
management.cloudfoundry.skip-ssl-validation=true
Yaml
management:
  cloudfoundry:
    skip-ssl-validation: true

12.3. Custom Context Path

If the server’s context-path has been configured to anything other than /, the Cloud Foundry endpoints are not available at the root of the application. For example, if server.servlet.context-path=/app, Cloud Foundry endpoints are available at /app/cloudfoundryapplication/*.spring-doc.cn

If you expect the Cloud Foundry endpoints to always be available at /cloudfoundryapplication/*, regardless of the server’s context-path, you need to explicitly configure that in your application. The configuration differs, depending on the web server in use. For Tomcat, you can add the following configuration:spring-doc.cn

Java
import java.io.IOException;
import java.util.Collections;

import jakarta.servlet.GenericServlet;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.apache.catalina.Host;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyCloudFoundryConfiguration {

    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory() {

            @Override
            protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
                super.prepareContext(host, initializers);
                StandardContext child = new StandardContext();
                child.addLifecycleListener(new Tomcat.FixContextListener());
                child.setPath("/cloudfoundryapplication");
                ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
                child.addServletContainerInitializer(initializer, Collections.emptySet());
                child.setCrossContext(true);
                host.addChild(child);
            }

        };
    }

    private ServletContainerInitializer getServletContextInitializer(String contextPath) {
        return (classes, context) -> {
            Servlet servlet = new GenericServlet() {

                @Override
                public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
                    ServletContext context = req.getServletContext().getContext(contextPath);
                    context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res);
                }

            };
            context.addServlet("cloudfoundry", servlet).addMapping("/*");
        };
    }

}
Kotlin
import jakarta.servlet.GenericServlet
import jakarta.servlet.Servlet
import jakarta.servlet.ServletContainerInitializer
import jakarta.servlet.ServletContext
import jakarta.servlet.ServletException
import jakarta.servlet.ServletRequest
import jakarta.servlet.ServletResponse
import org.apache.catalina.Host
import org.apache.catalina.core.StandardContext
import org.apache.catalina.startup.Tomcat.FixContextListener
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.boot.web.servlet.ServletContextInitializer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.io.IOException
import java.util.Collections.emptySet

@Configuration(proxyBeanMethods = false)
class MyCloudFoundryConfiguration {

    @Bean
    fun servletWebServerFactory(): TomcatServletWebServerFactory {
        return object : TomcatServletWebServerFactory() {

            override fun prepareContext(host: Host, initializers: Array<ServletContextInitializer>) {
                super.prepareContext(host, initializers)
                val child = StandardContext()
                child.addLifecycleListener(FixContextListener())
                child.path = "/cloudfoundryapplication"
                val initializer = getServletContextInitializer(contextPath)
                child.addServletContainerInitializer(initializer, emptySet())
                child.crossContext = true
                host.addChild(child)
            }

        }
    }

    private fun getServletContextInitializer(contextPath: String): ServletContainerInitializer {
        return ServletContainerInitializer { classes: Set<Class<*>?>?, context: ServletContext ->
            val servlet: Servlet = object : GenericServlet() {

                @Throws(ServletException::class, IOException::class)
                override fun service(req: ServletRequest, res: ServletResponse) {
                    val servletContext = req.servletContext.getContext(contextPath)
                    servletContext.getRequestDispatcher("/cloudfoundryapplication").forward(req, res)
                }

            }
            context.addServlet("cloudfoundry", servlet).addMapping("/*")
        }
    }
}

13. What to Read Next

You might want to read about graphing tools such as Graphite.spring-doc.cn

Otherwise, you can continue on to read about “deployment options” or jump ahead for some in-depth information about Spring Boot’s build tool plugins.spring-doc.cn