log status into database spring boot admin

151 views Asked by At

Has anyone encountered the need to log a response from springboot acutator? I want to use a springboot admin server that will query applications and log the response of each /health request to the application and write it in database.

I googled my problem, but I didn't find anything.

Admin server have url to application enter image description here can i get this url from admin server to make rest request?

2

There are 2 answers

1
Anh Tran On

It is possible to log the response from Spring Boot Actuator endpoints, including the /health endpoint, and then write it to a database using a Spring Boot Admin Server. Spring Boot Actuator provides several endpoints that allow you to monitor and manage your application, and the /health endpoint is one of them, used to check the health status of your application.

// Step 1: Set up Spring Boot Admin Server
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }
}

In the above example, you have set up the Spring Boot Admin Server. Now you need to configure the client applications to be monitored.

// Step 2: Configure Actuator for Remote Applications
@SpringBootApplication
public class MonitoredApplication {
    public static void main(String[] args) {
        SpringApplication.run(MonitoredApplication.class, args);
    }
}

Now, customize the /health response by implementing a custom health indicator:

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // Implement your custom health checks here
        // You can include application-specific information in the Health status
        return Health.up().withDetail("custom", "Custom health check passed").build();
    }
}

Finally, in the Spring Boot Admin Server application, implement the logic to query the /health endpoint of the monitored applications and log the responses:

@Service
public class HealthLoggingService {
    private RestTemplate restTemplate = new RestTemplate();

    public void logHealthFromApplication(String applicationUrl) {
        ResponseEntity<Map> response = restTemplate.exchange(
            applicationUrl + "/actuator/health", HttpMethod.GET, null, Map.class);
        
        // Extract relevant information from the response and store it in the database
        // For example, you can log the response status, details, timestamp, etc.
        // You can also parse the JSON response to extract specific data you want to log.
        Map<String, Object> healthDetails = response.getBody();
        // Store the details in the database
    }
}
0
Erik P On

If you are only interested in events where the health status changed, persistent storage is indirectly supported by using hazelcast with persistent storage, see https://github.com/codecentric/spring-boot-admin/issues/541

If you want to persist status change events on your own, you could provide a bean which subscribes on InstanceEventStore which implements Publisher<InstanceEvent>.

If you really want to get all health calls, then you could probably do this via a small "hack" by extending EventsourcingInstanceRepository. computeIfPresent is called on every health endpoint invocation (but not only there, also in other cases!), so you could override it:

public class CustomEventsourcingInstanceRepository extends EventsourcingInstanceRepository {
        @Override
        public Mono<Instance> computeIfPresent(InstanceId id, BiFunction<InstanceId, Instance, Mono<Instance>> remappingFunction) {
            Mono<Instance> instanceMono = super.computeIfPresent(id, remappingFunction);
            instanceMono.doOnNext(instance -> persistToDb(instance.getStatusInfo().getStatus()));
            return instanceMono;
        }
    }

Maybe a new extension point could be added to SBA itself, therefore please request it here: https://github.com/codecentric/spring-boot-admin/issues/new/choose and describe in detail when it should be called and with which information.