Search in sources :

Example 1 with HealthMetaData

use of org.wildfly.swarm.monitor.HealthMetaData in project wildfly-swarm by wildfly-swarm.

the class HealthAnnotationProcessor method process.

@Override
public void process() throws NamingException {
    // first pass: jboss-web context root
    Optional<String> jbossWebContext = Optional.empty();
    // if (archive instanceof JBossWebContainer) {
    if (archive.getName().endsWith(".war")) {
        JBossWebContainer war = archive.as(WARArchive.class);
        if (war.getContextRoot() != null) {
            jbossWebContext = Optional.of(war.getContextRoot());
        }
    }
    // second pass: JAX-RS applications
    Optional<String> appPath = Optional.empty();
    Collection<AnnotationInstance> appPathAnnotations = index.getAnnotations(APP_PATH);
    for (AnnotationInstance annotation : appPathAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            appPath = Optional.of(annotation.value().asString());
        }
    }
    // third pass: JAX-RS resources
    Collection<AnnotationInstance> pathAnnotations = index.getAnnotations(PATH);
    for (AnnotationInstance annotation : pathAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            ClassInfo classInfo = annotation.target().asClass();
            for (MethodInfo methodInfo : classInfo.methods()) {
                if (methodInfo.hasAnnotation(HEALTH)) {
                    StringBuilder sb = new StringBuilder();
                    boolean isSecure = false;
                    // prepend the jboss-web cntext if given
                    if (jbossWebContext.isPresent() && !jbossWebContext.get().equals("/")) {
                        safeAppend(sb, jbossWebContext.get());
                    }
                    // prepend the appPath if given
                    if (appPath.isPresent() && !appPath.get().equals("/")) {
                        safeAppend(sb, appPath.get());
                    }
                    // the class level @Path
                    for (AnnotationInstance classAnnotation : classInfo.classAnnotations()) {
                        if (classAnnotation.name().equals(PATH)) {
                            String methodPathValue = classAnnotation.value().asString();
                            if (!methodPathValue.equals("/")) {
                                safeAppend(sb, methodPathValue);
                            }
                        }
                    }
                    if (methodInfo.hasAnnotation(PATH)) {
                        // the method level @Path
                        safeAppend(sb, methodInfo.annotation(PATH).value().asString());
                        // the method level @Health
                        AnnotationInstance healthAnnotation = methodInfo.annotation(HEALTH);
                        isSecure = healthAnnotation.value("inheritSecurity") != null ? healthAnnotation.value("inheritSecurity").asBoolean() : true;
                    } else {
                        throw new RuntimeException("@Health requires an explicit @Path annotation");
                    }
                    HealthMetaData metaData = new HealthMetaData(sb.toString(), isSecure);
                    Monitor.lookup().registerHealth(metaData);
                }
            }
        }
    }
}
Also used : HealthMetaData(org.wildfly.swarm.monitor.HealthMetaData) JBossWebContainer(org.wildfly.swarm.undertow.descriptors.JBossWebContainer) MethodInfo(org.jboss.jandex.MethodInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with HealthMetaData

use of org.wildfly.swarm.monitor.HealthMetaData in project wildfly-swarm by wildfly-swarm.

the class HttpContexts method proxyRequests.

private void proxyRequests(HttpServerExchange exchange) {
    if (monitor.getHealthURIs().isEmpty()) {
        noHealthEndpoints(exchange);
    } else {
        try {
            final List<InVMResponse> responses = new CopyOnWriteArrayList<>();
            CountDownLatch latch = new CountDownLatch(monitor.getHealthURIs().size());
            dispatched.set(latch);
            for (HealthMetaData healthCheck : monitor.getHealthURIs()) {
                invokeHealthInVM(exchange, healthCheck, responses, latch);
            }
            latch.await(10, TimeUnit.SECONDS);
            if (latch.getCount() > 0) {
                throw new Exception("Probe timed out");
            }
            boolean failed = false;
            if (!responses.isEmpty()) {
                if (responses.size() != monitor.getHealthURIs().size()) {
                    throw new RuntimeException("The number of responses does not match!");
                }
                StringBuffer sb = new StringBuffer("{");
                sb.append("\"checks\": [\n");
                int i = 0;
                for (InVMResponse resp : responses) {
                    sb.append(resp.getPayload());
                    if (!failed) {
                        failed = resp.getStatus() != 200;
                    }
                    if (i < responses.size() - 1) {
                        sb.append(",\n");
                    }
                    i++;
                }
                sb.append("],\n");
                // we don't have policies yet, so keep it simple
                String outcome = failed ? "DOWN" : "UP";
                sb.append("\"outcome\": \"" + outcome + "\"\n");
                sb.append("}\n");
                // send a response
                if (failed) {
                    exchange.setStatusCode(503);
                }
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
                exchange.getResponseSender().send(sb.toString());
            } else {
                new RuntimeException("Responses should not be empty").printStackTrace();
                exchange.setStatusCode(500);
            }
            exchange.endExchange();
        } catch (Throwable t) {
            LOG.error("Health check failed", t);
            if (!exchange.isResponseStarted()) {
                exchange.setStatusCode(500);
            }
            exchange.endExchange();
        }
    }
}
Also used : HealthMetaData(org.wildfly.swarm.monitor.HealthMetaData) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) NamingException(javax.naming.NamingException) IOException(java.io.IOException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

HealthMetaData (org.wildfly.swarm.monitor.HealthMetaData)2 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 NamingException (javax.naming.NamingException)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 ClassInfo (org.jboss.jandex.ClassInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1 JBossWebContainer (org.wildfly.swarm.undertow.descriptors.JBossWebContainer)1