concept Actuator in category spring

appears as: Actuator, The Actuator, Actuator
Spring Boot in Action

This is an excerpt from Manning's book Spring Boot in Action.

  • The Actuator— Gives you insight into what’s going on inside of a running Spring Boot application.
  • In this chapter, we’re going to explore Spring Boot’s Actuator. The Actuator offers production-ready features such as monitoring and metrics to Spring Boot applications. The Actuator’s features are provided by way of several REST endpoints, a remote shell, and Java Management Extensions (JMX). We’ll start by looking at the Actuator’s REST endpoints, which offer the most complete and well-known way of working with the Actuator.

    Now the /shutdown endpoint is locked down for everyone except users with ADMIN access. But what about the Actuator’s other endpoints? Assuming you want to lock them down with ADMIN access as for /shutdown, you can list each of them in the call to antMatchers(). For example, to lock down /metrics and /configprops as well as /shutdown, call antMatchers() like this:

    .antMatchers("/shutdown", "/metrics", "/configprops")
                              .access("hasRole('ADMIN')")

    Although this approach will work, it’s only suitable if you want to secure a small subset of the Actuator endpoints. It becomes unwieldy if you use it to lock down all of the Actuator’s endpoints.

    Rather than explicitly list all of the Actuator endpoints when calling antMatchers(), it’s much easier to use wildcards to match them all with a simple Ant-style expression. This is challenging, however, because there’s not a lot in common between the endpoint paths. And we can’t apply ADMIN access to “/**” because then everything except for the root path (/) would require ADMIN access.

    Instead, consider setting the endpoint’s context path by setting the management.context-path property. By default, this property is empty, which is why all of the Actuator’s endpoint paths are relative to the root path. But the following entry in application.yaml will prefix them all with /mgmt.

    7.1. Exploring the Actuator’s endpoints

    The key feature of Spring Boot’s Actuator is that it provides several web endpoints in your application through which you can view the internals of your running application. Through the Actuator, you can find out how beans are wired together in the Spring application context, determine what environment properties are available to your application, get a snapshot of runtime metrics, and more.

    The Actuator offers a baker’s dozen of endpoints, as described in table 7.1.

    Table 7.1. Actuator endpoints

    HTTP method

    Path

    Description

    GET /autoconfig Provides an auto-configuration report describing what auto-configuration conditions passed and failed.
    GET /configprops Describes how beans have been injected with configuration properties (including default values).
    GET /beans Describes all beans in the application context and their relationship to each other.
    GET /dump Retrieves a snapshot dump of thread activity.
    GET /env Retrieves all environment properties.
    GET /env/{name} Retrieves a specific environment value by name.
    GET /health Reports health metrics for the application, as provided by HealthIndicator implementations.
    GET /info Retrieves custom information about the application, as provided by any properties prefixed with info.
    GET /mappings Describes all URI paths and how they’re mapped to controllers (including Actuator endpoints).
    GET /metrics Reports various application metrics such as memory usage and HTTP request counters.
    GET /metrics/{name} Reports an individual application metric by name.
    POST /shutdown Shuts down the application; requires that endpoints.shutdown.enabled be set to true.
    GET /trace Provides basic trace information (timestamp, headers, and so on) for HTTP requests.

    To enable the Actuator endpoints, all you must do is add the Actuator starter to your build. In a Gradle build specification, that dependency looks like this: