oparl-service/oparl-server/src/main/java/de/twomartens/oparlservice/actuator/AbstractHealthCheck.java

69 lines
1.9 KiB
Java
Raw Normal View History

2020-07-07 23:00:19 +02:00
package de.twomartens.oparlservice.actuator;
2020-07-07 22:43:29 +02:00
2020-07-07 23:00:19 +02:00
import de.twomartens.oparlservice.interceptors.RequestTypeInterceptor;
import de.twomartens.oparlservice.interceptors.TraceIdInterceptor;
2020-07-07 22:43:29 +02:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
@Slf4j
public abstract class AbstractHealthCheck implements HealthIndicator {
private static final String DEFAULT_HOST = "localhost";
private static final String HEALTH_KEY_ENDPOINT = "endpoint";
private final String endpoint;
private final TraceIdInterceptor traceIdInterceptor;
private final RequestTypeInterceptor requestTypeInterceptor;
private final int port;
public AbstractHealthCheck(int port,
TraceIdInterceptor traceIdInterceptor,
RequestTypeInterceptor requestTypeInterceptor) {
this.traceIdInterceptor = traceIdInterceptor;
this.requestTypeInterceptor = requestTypeInterceptor;
this.port = port;
this.endpoint = mkEndpoint();
}
@Override
public Health health() {
requestTypeInterceptor.markAsHealthCheck();
traceIdInterceptor.createNewTraceId();
Health result;
try {
if (isEndpointAvailable()) {
result = Health.up().withDetail(HEALTH_KEY_ENDPOINT, getEndpoint()).build();
} else {
result = Health.down().withDetail(HEALTH_KEY_ENDPOINT, getEndpoint()).build();
}
} catch (Exception e) {
result = Health.down().withDetail(HEALTH_KEY_ENDPOINT, getEndpoint()).withException(e).build();
}
log.info("health check invoked for '{}' with status '{}'", getMethodName(), result.getStatus());
return result;
}
String getHost() {
return DEFAULT_HOST;
}
int getPort() {
return this.port;
}
String getEndpoint() {
return this.endpoint;
}
abstract boolean isEndpointAvailable();
abstract String getMethodName();
abstract String mkEndpoint();
}