Search in sources :

Example 1 with HealthCheckResponse

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());
}
Also used : HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 2 with HealthCheckResponse

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;
}
Also used : HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 3 with HealthCheckResponse

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());
}
Also used : HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) JsonWriter(javax.json.JsonWriter) JsonWriterFactory(javax.json.JsonWriterFactory)

Example 4 with HealthCheckResponse

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;
}
Also used : HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse)

Example 5 with 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;
}
Also used : MonitoringDataCollector(fish.payara.monitoring.collect.MonitoringDataCollector) JsonWriterFactory(javax.json.JsonWriterFactory) Events(org.glassfish.api.event.Events) MonitoringWatchCollector(fish.payara.monitoring.collect.MonitoringWatchCollector) Map(java.util.Map) WebApplication(com.sun.enterprise.web.WebApplication) LIVENESS(fish.payara.microprofile.healthcheck.HealthCheckType.LIVENESS) HealthCheck(org.eclipse.microprofile.health.HealthCheck) PayaraHealthCheckServiceEvents(fish.payara.nucleus.healthcheck.events.PayaraHealthCheckServiceEvents) MonitoringDataCollector(fish.payara.monitoring.collect.MonitoringDataCollector) Collections.emptyList(java.util.Collections.emptyList) UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WARNING(java.util.logging.Level.WARNING) Set(java.util.Set) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) MonitoringDataSource(fish.payara.monitoring.collect.MonitoringDataSource) List(java.util.List) StartupRunLevel(org.glassfish.api.StartupRunLevel) Service(org.jvnet.hk2.annotations.Service) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) MicroprofileHealthCheckConfiguration(fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration) Optional(java.util.Optional) UnprocessedChangeEvent(org.jvnet.hk2.config.UnprocessedChangeEvent) JsonObjectBuilder(javax.json.JsonObjectBuilder) Deployment(org.glassfish.internal.deployment.Deployment) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) READINESS(fish.payara.microprofile.healthcheck.HealthCheckType.READINESS) JsonGenerator(javax.json.stream.JsonGenerator) InvocationManager(org.glassfish.api.invocation.InvocationManager) JsonArrayBuilder(javax.json.JsonArrayBuilder) Globals(org.glassfish.internal.api.Globals) MonitoringData(fish.payara.monitoring.collect.MonitoringData) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) InvocationException(org.glassfish.api.invocation.InvocationException) HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) BiConsumer(java.util.function.BiConsumer) Json(javax.json.Json) Collections.singletonMap(java.util.Collections.singletonMap) EventListener(org.glassfish.api.event.EventListener) PropertyChangeEvent(java.beans.PropertyChangeEvent) WebComponentInvocation(com.sun.enterprise.web.WebComponentInvocation) RunLevel(org.glassfish.hk2.runlevel.RunLevel) StringWriter(java.io.StringWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebContainer(com.sun.enterprise.web.WebContainer) IOException(java.io.IOException) Checker(fish.payara.nucleus.healthcheck.configuration.Checker) STARTUP(fish.payara.microprofile.healthcheck.HealthCheckType.STARTUP) Status(org.eclipse.microprofile.health.HealthCheckResponse.Status) MonitoringWatchSource(fish.payara.monitoring.collect.MonitoringWatchSource) JsonWriter(javax.json.JsonWriter) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ApplicationRegistry(org.glassfish.internal.data.ApplicationRegistry) ConfigListener(org.jvnet.hk2.config.ConfigListener) PayaraHealthCheck(fish.payara.microprofile.healthcheck.checks.PayaraHealthCheck) Set(java.util.Set) HashSet(java.util.HashSet) HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HealthCheck(org.eclipse.microprofile.health.HealthCheck) PayaraHealthCheck(fish.payara.microprofile.healthcheck.checks.PayaraHealthCheck) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

HealthCheckResponse (org.eclipse.microprofile.health.HealthCheckResponse)4 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 WebApplication (com.sun.enterprise.web.WebApplication)1 WebComponentInvocation (com.sun.enterprise.web.WebComponentInvocation)1 WebContainer (com.sun.enterprise.web.WebContainer)1 LIVENESS (fish.payara.microprofile.healthcheck.HealthCheckType.LIVENESS)1 READINESS (fish.payara.microprofile.healthcheck.HealthCheckType.READINESS)1 STARTUP (fish.payara.microprofile.healthcheck.HealthCheckType.STARTUP)1 PayaraHealthCheck (fish.payara.microprofile.healthcheck.checks.PayaraHealthCheck)1 MicroprofileHealthCheckConfiguration (fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration)1