generated from 2martens/template-service
69 lines
1.9 KiB
Java
69 lines
1.9 KiB
Java
package de.twomartens.oparlservice.actuator;
|
|
|
|
import de.twomartens.oparlservice.interceptors.RequestTypeInterceptor;
|
|
import de.twomartens.oparlservice.interceptors.TraceIdInterceptor;
|
|
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();
|
|
|
|
}
|