added log of exception on failing

This commit is contained in:
Francesco Mangiacrapa 2024-10-22 16:38:40 +02:00
parent a4611b887a
commit a341824dc6
2 changed files with 12 additions and 5 deletions

View File

@ -6,8 +6,10 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponse.State;
import org.gcube.application.geoportal.service.rest.health.GeoportalHealthCheck;
import org.gcube.application.geoportal.service.rest.health.HealthCheckResponseSerializer;
import org.gcube.application.geoportal.service.rest.health.MongoHealthCheck;
@ -47,7 +49,7 @@ public class GeoportalHealth {
public Response databaseCheck(@QueryParam("context") String context) throws JsonProcessingException {
log.debug("databaseCheck called in the context {}", context);
if (context == null) {
HealthCheckResponse response = HealthCheckResponse.named("databaseCheck")
HealthCheckResponse response = HealthCheckResponse.named(MongoHealthCheck.SERVICE_NAME)
.withData("context", "is required parameter (e.g. context=/gcube/devsec/devVRE)").down().build();
String json = healthCheckSerializer(response);
log.info("databaseCheck error response is {}", json);
@ -56,9 +58,13 @@ public class GeoportalHealth {
}
HealthCheckResponse response = new MongoHealthCheck(context).call();
ResponseBuilder responseBuilder = Response.ok();
if (response.getState().equals(State.DOWN)) {
responseBuilder = responseBuilder.status(503);
}
String json = healthCheckSerializer(response);
log.info("databaseCheck response is {}", json);
return Response.ok().entity(json).build();
return responseBuilder.entity(json).build();
}
private String healthCheckSerializer(HealthCheckResponse response) throws JsonProcessingException {

View File

@ -20,7 +20,7 @@ import lombok.extern.slf4j.Slf4j;
public class MongoHealthCheck implements HealthCheck {
private String context;
private static final String SERVICE_NAME = "mongo";
public static final String SERVICE_NAME = "mongo";
@Override
public HealthCheckResponse call() {
@ -40,7 +40,6 @@ public class MongoHealthCheck implements HealthCheck {
mongo = ImplementationProvider.get().getProvidedObjectByClass(Mongo.class);
buildHCRBuilder = appendMongoInfo(buildHCRBuilder, mongo.getConnection());
buildHCRBuilder.state(true);
log.info("checkMongo is OK in the context: {}", context);
MongoIterable<String> collections = mongo.getTheClient().getDatabase(mongo.getConnection().getDatabase())
.listCollectionNames();
log.info("listCollectionNames is null: {}", collections == null);
@ -50,9 +49,11 @@ public class MongoHealthCheck implements HealthCheck {
buildHCRBuilder.withData("collection_" + i, coll);
i++;
}
log.info("checkMongo is OK in the context: {}", context);
return buildHCRBuilder.build();
} catch (Exception e) {
log.info("checkMongo is KO in the context: {}", context);
log.error("Error on checkMongo: ", e);
log.warn("checkMongo is KO in the context: {}", context);
buildHCRBuilder.state(false);
if (mongo != null) {
MongoConnection connection = null;