added custom HealthCheckResponseSerializer

This commit is contained in:
Francesco Mangiacrapa 2024-10-18 16:35:22 +02:00
parent af16e3b200
commit e7d6a2d707
4 changed files with 101 additions and 12 deletions

View File

@ -9,10 +9,12 @@ import javax.ws.rs.core.Response;
import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.HealthCheckResponse;
import org.gcube.application.geoportal.service.rest.health.GeoportalHealthCheck; 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; import org.gcube.application.geoportal.service.rest.health.MongoHealthCheck;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -20,32 +22,47 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class GeoportalHealth { public class GeoportalHealth {
private ObjectMapper mapper = new ObjectMapper();
public GeoportalHealth() {
SimpleModule module = new SimpleModule();
module.addSerializer(HealthCheckResponse.class, new HealthCheckResponseSerializer());
mapper.registerModule(module);
}
@GET @GET
@Path("") @Path("")
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public String serviceCheck() throws JsonProcessingException { public Response serviceCheck() throws JsonProcessingException {
log.info("serviceCheck called"); log.debug("serviceCheck called");
HealthCheckResponse response = new GeoportalHealthCheck().call(); HealthCheckResponse response = new GeoportalHealthCheck().call();
String json = healthCheckSerializer(response); String json = healthCheckSerializer(response);
log.info("serviceCheck response is {}", json); log.info("serviceCheck response is {}", json);
return json; return Response.ok().entity(json).build();
} }
@GET @GET
@Path("/mongo") @Path("/mongo")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
public HealthCheckResponse databaseCheck(@QueryParam("context") String context) { public Response databaseCheck(@QueryParam("context") String context) throws JsonProcessingException {
log.info("databaseCheck called in the context {}", context); log.debug("databaseCheck called in the context {}", context);
if (context == null) if (context == null) {
return HealthCheckResponse.named("databaseCheck") HealthCheckResponse response = HealthCheckResponse.named("databaseCheck")
.withData("context", "is required parameter (e.g. context=/gcube/devsec/devVRE)").down().build(); .withData("context", "is required parameter (e.g. context=/gcube/devsec/devVRE)").down().build();
String json = healthCheckSerializer(response);
log.info("databaseCheck error response is {}", json);
// Bad request
return Response.status(400).entity(json).build();
}
return new MongoHealthCheck(context).call(); HealthCheckResponse response = new MongoHealthCheck(context).call();
String json = healthCheckSerializer(response);
log.info("databaseCheck response is {}", json);
return Response.ok().entity(json).build();
} }
private String healthCheckSerializer(HealthCheckResponse response) throws JsonProcessingException { private String healthCheckSerializer(HealthCheckResponse response) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper(); // Serializes HealthCheckResponse in JSON with custom HealthCheckResponseSerializer
// Serializes HealthCheckResponse in JSON
return mapper.writeValueAsString(response); return mapper.writeValueAsString(response);
} }

View File

@ -7,7 +7,6 @@ import org.eclipse.microprofile.health.Readiness;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Readiness @Readiness
@Liveness @Liveness
@Slf4j @Slf4j
@ -18,7 +17,8 @@ public class GeoportalHealthCheck implements HealthCheck {
@Override @Override
public HealthCheckResponse call() { public HealthCheckResponse call() {
log.info(GeoportalHealthCheck.class.getSimpleName() + " call"); log.info(GeoportalHealthCheck.class.getSimpleName() + " call");
HealthCheckResponse response = HealthCheckResponse.named(SERVICE_NAME).state(true).withData("serviceStatus", "healthy").build(); HealthCheckResponse response = HealthCheckResponse.named(SERVICE_NAME).state(true)
.withData("serviceStatus", "healthy").build();
return response; return response;
} }

View File

@ -0,0 +1,40 @@
package org.gcube.application.geoportal.service.rest.health;
import java.io.IOException;
import java.util.Map;
import org.eclipse.microprofile.health.HealthCheckResponse;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HealthCheckResponseSerializer extends JsonSerializer<HealthCheckResponse> {
@Override
public void serialize(HealthCheckResponse response, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
if (response.getName() != null)
gen.writeStringField("name", response.getName());
if (response.getState() != null)
gen.writeStringField("state", response.getState().toString());
response.getData().ifPresent(data -> {
try {
gen.writeObjectFieldStart("data");
for (Map.Entry<String, Object> entry : data.entrySet()) {
gen.writeObjectField(entry.getKey(), entry.getValue());
}
gen.writeEndObject();
} catch (IOException e) {
log.warn("Error on serializing the data field", e);
}
});
gen.writeEndObject();
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.application.geoportal.service.health;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.gcube.application.geoportal.service.rest.health.HealthCheckResponseSerializer;
public class HealthCheckSerializer {
public static void main(String[] args) throws Exception {
HealthCheckResponse response = HealthCheckResponse.named("geooportal-service")
.state(true)
.withData("status", "healthy")
.build();
// Configura ObjectMapper con il serializer personalizzato
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(HealthCheckResponse.class, new HealthCheckResponseSerializer());
mapper.registerModule(module);
// Serializza l'oggetto HealthCheckResponse in JSON
String json = mapper.writeValueAsString(response);
// Stampa il JSON serializzato
System.out.println(json);
}
}