Moment For Technology

Spring Boot 2.X Actuator Monitoring/Health check/Audit/Statistics

Posted on Aug. 8, 2022, 7:07 p.m. by Shane Stewart
Category: The back-end Tag: The back-end Spring Boot

"This is the 13th day of my participation in the August Text Challenge.

Spring Boot 2.x log4j2

Modify based on the above code

Source: github.com/langyastudi...

IO /spring-boot...

Spring Boot provides a Actuator that helps you monitor and manage Spring Boot applications, such as health checks, auditing, statistics, and HTTP tracking. All of these features are available via JMX or HTTP endPoints.

Endpoints can be divided into three broad categories:

  • Application Configuration Class

    Gets information about configuration classes that are closely related to Spring Boot applications, such as application configurations, environment variables, and automated configuration reports loaded in the application

  • Metric class

    Gets metrics to monitor as the application runs, such as memory information, thread pool information, HTTP request statistics, and so on

  • Operation control class

    It provides operation functions such as shutting down applications

The Actuator can also be integrated with external application monitoring systems such as Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic, etc. These systems provide excellent dashboards, ICONS, analytics, and alerts, making it easy to monitor and manage your applications through a unified interface.

Access point

Pom. XML is introduced into the physical

dependency
    groupIdorg.springframework.boot/groupId
    artifactIdspring-boot-starter-actuator/artifactId
/dependency
Copy the code

Configuring access Points

By default, all access points are exposed to JMX, but only Health and INFO are exposed to the Web for security reasons. If you need to expose more access points to the Web, you need to add a configuration in application.yml (not recommended), for example:

management:
  endpoints:
    web:
      exposure:
        include: health, info, beans, env, conditions, configprops, loggers, metrics
Copy the code

Pay special attention to the security of exposed urls. For example, /actuator/env can obtain all environment variables of the current machine and cannot be exposed to the public network

Common access points

Endpoint ID Description
health Application health status
info Basic application information
shutdown Closing applications (On and off can be passedManagement.endpoint. endpoint name. enabledTo set up)
env Application environment information, including Profile, system environment variables, and application properties information
beans All beans applied
conditions Automatic configuration reports for applications, including positiveMatches (matching), negativeMatches (mismatching), and unconditionalClasses (matching)
httptrace Displays HTTP footprints, the last 100 HTTP Requests /repsponse
configprops All beans annotated @ConfigurationProperties
threaddump Java VIRTUAL machine thread information
loggers All loggers in the application
mappings All of the @ RequestMapping
metrics Application indicator information, including memory usage
scheduledtasks Scheduling tasks in applications

Web access

/ physical is the prefix all endpoints, visit http://localhost:8080/actuator, it can display all have been exposed and function has been to the end of open access to information

Some of the access points are described here

health

Many gateways acting as reverse proxies require a URL to detect whether the back-end cluster application is alive and this URL can be provided to the gateway

Physical can through the URL/physical / + access point for a visit, http://localhost:8080/actuator/health, for example, can view the application current status:

{
    "status": "UP"
}
Copy the code

If you want to see the details of the health information, configure it in application.yml:

management:
  endpoint:
    health:
      show-details: always
Copy the code

After the application is restarted, detailed health information is displayed:

{
    "status": "UP"."components": {
        "diskSpace": {
            "status": "UP"."details": {
                "total": 64424505344."free": 23276142592."threshold": 10485760."exists": true}},"ping": {
            "status": "UP"}}}Copy the code

info

This can be configured in application.yml, and any information can be set using info.*, for example:

info:
  name: spring boot app
  version: 1.01.
  maintainer: langyastudio
Copy the code

At this point to http://localhost:8080/actuator/info can see application information is as follows:

{
    "name": "spring boot app"."version": "1.0.1"."maintainer": "langyastudio"
}
Copy the code

httptrace

Used to display trace information for requests, but starting with Spring 2.2.x, we had to customize the Bean that implements HttpTraceRepository to store trace and query information in order to enable this endpoint.

The custom

Modifying an Access Address

This can be configured in application.yml

endpoints:
  web:
    exposure:
      #health, info
      include: health, info
    base-path: /monitor
    path-mapping:
      health: check-health
Copy the code
  • Access address fromactuatorInstead ofmonitor
  • Change the address of the health endpoint to check-health, for examplehttp://localhost:8080/monitor/ check-health

Custom health indicators

Define a HealthIndicator using a HealthIndicator, using the state data of the previous endpoint as a health basis. If the state is not empty, it is Healthy (UP). If the state is empty, it is unhealthy (DOWN).

@Component
public class MonitorHealthIndicator implements HealthIndicator
{
    @Override
    public Health health(a)
    {
        //Health.down();
       return  Health.up()
                .withDetail("error"."no") .build(); }}Copy the code

Accessing the Health endpoint at this point results in the following:

Custom endpoints

Annotating @endpoint, @webendpoint, or @EndPointwebextension on a Bean exposes the Bean as an Endpoint over HTTP.

Custom endpoints support three operations and receive arguments via @selector:

  • @readOperation: GET (query request)
  • @writeOperation: POST (Save request)
  • @deleteOperation: DELETE (DELETE request)

I won't go into details here

User-defined Indicators

Spring Boot Actuator also provides basic metrics and automatic configuration based on Micrometer.

I won't go into details here

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.