use of org.eclipse.microprofile.health.HealthCheckResponse in project Payara by payara.
the class HealthCheckService method constructResponse.
private void constructResponse(HttpServletResponse httpResponse, Set<HealthCheckResponse> healthCheckResponses) throws IOException {
httpResponse.setContentType("application/json");
// For each HealthCheckResponse we got from executing the health checks...
JsonArrayBuilder checksArray = Json.createArrayBuilder();
for (HealthCheckResponse healthCheckResponse : healthCheckResponses) {
JsonObjectBuilder healthCheckObject = Json.createObjectBuilder();
// Add the name and state
healthCheckObject.add("name", healthCheckResponse.getName());
healthCheckObject.add("state", healthCheckResponse.getState().toString());
// Add data if present
JsonObjectBuilder healthCheckData = Json.createObjectBuilder();
if (healthCheckResponse.getData().isPresent() && !healthCheckResponse.getData().get().isEmpty()) {
for (Map.Entry<String, Object> dataMapEntry : healthCheckResponse.getData().get().entrySet()) {
healthCheckData.add(dataMapEntry.getKey(), dataMapEntry.getValue().toString());
}
}
healthCheckObject.add("data", healthCheckData);
// Add finished Object to checks array
checksArray.add(healthCheckObject);
// Check if we need to set the response as 503. Check against status 200 so we don't repeatedly set it
if (httpResponse.getStatus() == 200 && healthCheckResponse.getState().equals(HealthCheckResponse.State.DOWN)) {
httpResponse.setStatus(503);
}
}
// Create the final aggregate object
JsonObjectBuilder responseObject = Json.createObjectBuilder();
// Set the aggregate outcome
if (httpResponse.getStatus() == 200) {
responseObject.add("outcome", "UP");
} else {
responseObject.add("outcome", "DOWN");
}
// Add all of the checks
responseObject.add("checks", checksArray);
// Print the outcome
httpResponse.getOutputStream().print(responseObject.build().toString());
}
use of org.eclipse.microprofile.health.HealthCheckResponse in project Payara by payara.
the class HealthCheckService method collectJointType.
private static List<HealthCheckResponse> collectJointType(MonitoringDataCollector collector, String type, Map<String, List<HealthCheckResponse>> healthResponsesByAppName) {
List<HealthCheckResponse> allForType = new ArrayList<>();
for (Entry<String, List<HealthCheckResponse>> e : healthResponsesByAppName.entrySet()) {
HealthCheckResponse joint = computeJointState(type, e.getValue());
collectUpDown(collector.group(e.getKey()), joint);
allForType.addAll(e.getValue());
}
collectUpDown(collector, computeJointState(type, allForType));
return allForType;
}
use of org.eclipse.microprofile.health.HealthCheckResponse in project Payara by payara.
the class HealthCheckService method constructResponse.
private void constructResponse(HttpServletResponse httpResponse, Set<HealthCheckResponse> healthCheckResponses, HealthCheckType type, String enablePrettyPrint) throws IOException {
httpResponse.setContentType("application/json");
// For each HealthCheckResponse we got from executing the health checks...
JsonArrayBuilder checksArray = Json.createArrayBuilder();
for (HealthCheckResponse healthCheckResponse : healthCheckResponses) {
JsonObjectBuilder healthCheckObject = Json.createObjectBuilder();
// Add the name and status
healthCheckObject.add("name", healthCheckResponse.getName());
healthCheckObject.add("status", healthCheckResponse.getStatus().toString());
// Add data if present
JsonObjectBuilder healthCheckData = Json.createObjectBuilder();
if (healthCheckResponse.getData().isPresent() && !healthCheckResponse.getData().get().isEmpty()) {
for (Map.Entry<String, Object> dataMapEntry : healthCheckResponse.getData().get().entrySet()) {
healthCheckData.add(dataMapEntry.getKey(), dataMapEntry.getValue().toString());
}
}
healthCheckObject.add("data", healthCheckData);
// Add finished Object to checks array
checksArray.add(healthCheckObject);
// Check if we need to set the response as 503. Check against status 200 so we don't repeatedly set it
if (httpResponse.getStatus() == 200 && healthCheckResponse.getStatus().equals(HealthCheckResponse.Status.DOWN)) {
httpResponse.setStatus(503);
}
}
// Create the final aggregate object
JsonObjectBuilder responseObject = Json.createObjectBuilder();
// Set the aggregate status
responseObject.add("status", httpResponse.getStatus() == 200 ? "UP" : "DOWN");
// Add all of the checks
responseObject.add("checks", checksArray);
String prettyPrinting = enablePrettyPrint == null || enablePrettyPrint.equals("false") ? "" : JsonGenerator.PRETTY_PRINTING;
JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(singletonMap(prettyPrinting, ""));
StringWriter stringWriter = new StringWriter();
try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) {
jsonWriter.writeObject(responseObject.build());
}
// Print the outcome
httpResponse.getOutputStream().print(stringWriter.toString());
}
use of org.eclipse.microprofile.health.HealthCheckResponse in project Payara by payara.
the class HealthCheckResponseBuilderImpl method build.
@Override
public HealthCheckResponse build() {
validate();
// Just use the basic HealthCheckResponse implementation
HealthCheckResponse healthCheckResponse = new HealthCheckResponseImpl(name, status, data);
return healthCheckResponse;
}
use of org.eclipse.microprofile.health.HealthCheckResponse in project Payara by payara.
the class HealthCheckService method collectChecks.
private Map<String, List<HealthCheckResponse>> collectChecks(MonitoringDataCollector collector, Map<String, Set<HealthCheck>> checks, Map<String, Set<String>> collected) {
Map<String, List<HealthCheckResponse>> statusByApp = new HashMap<>();
for (Entry<String, Set<HealthCheck>> entry : checks.entrySet()) {
String appName = entry.getKey();
MonitoringDataCollector appCollector = collector.group(appName);
for (HealthCheck check : entry.getValue()) {
HealthCheckResponse response = performHealthCheckInApplicationContext(appName, check);
String metric = response.getName();
Set<String> appCollected = collected.get(appName);
// prevent adding same check more then once, unfortunately we have to run it to find that out
if (appCollected == null || !appCollected.contains(metric)) {
statusByApp.computeIfAbsent(appName, key -> new ArrayList<>()).add(response);
collectUpDown(appCollector, response);
if (response.getStatus() == Status.DOWN && response.getData().isPresent()) {
appCollector.annotate(metric, 0L, createAnnotation(response.getData().get()));
}
collected.computeIfAbsent(appName, key -> new HashSet<>()).add(metric);
}
}
}
return statusByApp;
}
Aggregations