commented the javadoc

This commit is contained in:
Francesco Mangiacrapa 2024-10-22 17:32:54 +02:00
parent ba3cb8eddc
commit ed7d1e36da
4 changed files with 92 additions and 12 deletions

View File

@ -2,7 +2,7 @@
## [v1.2.2-SNAPSHOT] - 2024-10-16
- Included the health check at https://{geoporta_endpoint}/health via `microprofile-health-api` (see https://microprofile.io/specifications/microprofile-health/)
- Included the health check at https://{geoporta_endpoint}/health via `smallrye-health` (that implements the https://microprofile.io/specifications/microprofile-health/) [#28301]
## [v1.2.1] - 2024-10-02

View File

@ -20,18 +20,34 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.extern.slf4j.Slf4j;
/**
* The Class GeoportalHealth.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Oct 22, 2024
*/
@Path("/health")
@Slf4j
public class GeoportalHealth {
private ObjectMapper mapper = new ObjectMapper();
/**
* Instantiates a new geoportal health.
*/
public GeoportalHealth() {
SimpleModule module = new SimpleModule();
module.addSerializer(HealthCheckResponse.class, new HealthCheckResponseSerializer());
mapper.registerModule(module);
}
/**
* Service check.
*
* @return the response compliant to `microprofile-health` specification. 200 if is OK. Otherwise it fails.
* @throws JsonProcessingException the json processing exception
*/
@GET
@Path("")
@Produces({ MediaType.APPLICATION_JSON })
@ -43,10 +59,18 @@ public class GeoportalHealth {
return Response.ok().entity(json).build();
}
/**
* Database check.
*
* @param context the gcube context
* @param include_collections if the check has to include the mongo collections in the response
* @return the response compliant to `microprofile-health` specification
* @throws JsonProcessingException the json processing exception
*/
@GET
@Path("/mongo")
@Produces({ MediaType.APPLICATION_JSON })
public Response databaseCheck(@QueryParam("context") String context) throws JsonProcessingException {
public Response databaseCheck(@QueryParam("context") String context, @QueryParam("include_collections") Boolean includeCollections) throws JsonProcessingException {
log.debug("databaseCheck called in the context {}", context);
if (context == null) {
HealthCheckResponse response = HealthCheckResponse.named(MongoHealthCheck.SERVICE_NAME)
@ -57,7 +81,7 @@ public class GeoportalHealth {
return Response.status(400).entity(json).build();
}
HealthCheckResponse response = new MongoHealthCheck(context).call();
HealthCheckResponse response = new MongoHealthCheck(context, includeCollections).call();
ResponseBuilder responseBuilder = Response.ok();
if (response.getState().equals(State.DOWN)) {
responseBuilder = responseBuilder.status(503);
@ -67,6 +91,13 @@ public class GeoportalHealth {
return responseBuilder.entity(json).build();
}
/**
* Health check serializer.
*
* @param response the response
* @return the string
* @throws JsonProcessingException the json processing exception
*/
private String healthCheckSerializer(HealthCheckResponse response) throws JsonProcessingException {
// Serializes HealthCheckResponse in JSON with custom
// HealthCheckResponseSerializer

View File

@ -7,6 +7,14 @@ import org.eclipse.microprofile.health.Readiness;
import lombok.extern.slf4j.Slf4j;
/**
* The Class GeoportalHealthCheck.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Oct 22, 2024
*/
@Readiness
@Liveness
@Slf4j
@ -14,6 +22,11 @@ public class GeoportalHealthCheck implements HealthCheck {
private static final String SERVICE_NAME = "geooportal-service";
/**
* Call.
*
* @return the health check response
*/
@Override
public HealthCheckResponse call() {
log.info(GeoportalHealthCheck.class.getSimpleName() + " call");

View File

@ -14,23 +14,50 @@ import com.mongodb.client.MongoIterable;
import lombok.extern.slf4j.Slf4j;
/**
* The Class MongoHealthCheck.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Oct 22, 2024
*/
@Readiness
@Liveness
@Slf4j
public class MongoHealthCheck implements HealthCheck {
private String context;
private Boolean includeCollections;
public static final String SERVICE_NAME = "mongo";
/**
* Call.
*
* @return the health check response
*/
@Override
public HealthCheckResponse call() {
return checkMongo(context);
}
public MongoHealthCheck(String context) {
/**
* Instantiates a new mongo health check.
*
* @param context the context
* @param includeCollections the include collections
*/
public MongoHealthCheck(String context, Boolean includeCollections) {
this.context = context;
this.includeCollections = includeCollections;
}
/**
* Check mongo.
*
* @param context the context
* @return the health check response
*/
private HealthCheckResponse checkMongo(String context) {
log.debug("checkMongo in the context: {}", context);
HealthCheckResponseBuilder buildHCRBuilder = HealthCheckResponse.named(SERVICE_NAME);
@ -40,14 +67,16 @@ public class MongoHealthCheck implements HealthCheck {
mongo = ImplementationProvider.get().getProvidedObjectByClass(Mongo.class);
buildHCRBuilder = appendMongoInfo(buildHCRBuilder, mongo.getConnection());
buildHCRBuilder.state(true);
MongoIterable<String> collections = mongo.getTheClient().getDatabase(mongo.getConnection().getDatabase())
.listCollectionNames();
log.info("listCollectionNames is null: {}", collections == null);
int i = 1;
for (String coll : collections) {
log.info("adding collection: {}", coll);
buildHCRBuilder.withData("collection_" + i, coll);
i++;
if (includeCollections) {
MongoIterable<String> collections = mongo.getTheClient()
.getDatabase(mongo.getConnection().getDatabase()).listCollectionNames();
log.info("listCollectionNames is null: {}", collections == null);
int i = 1;
for (String coll : collections) {
log.debug("adding collection: {}", coll);
buildHCRBuilder.withData("collection_" + i, coll);
i++;
}
}
log.info("checkMongo is OK in the context: {}", context);
return buildHCRBuilder.build();
@ -71,6 +100,13 @@ public class MongoHealthCheck implements HealthCheck {
}
}
/**
* Append mongo info.
*
* @param buildHCRBuilder the build HCR builder
* @param connection the connection
* @return the health check response builder
*/
private HealthCheckResponseBuilder appendMongoInfo(HealthCheckResponseBuilder buildHCRBuilder,
MongoConnection connection) {
buildHCRBuilder.withData("hosts", connection.getHosts() + "");