Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.metrics.reporting;
  
  
 import java.util.Map;

An HTTP servlet which runs the health checks registered with a given com.yammer.metrics.core.HealthCheckRegistry and prints the results as a text/plain entity. Only responds to GET requests.

If the servlet context has an attribute named com.yammer.metrics.reporting.HealthCheckServlet.registry which is a com.yammer.metrics.core.HealthCheckRegistry instance, HealthCheckServlet will use it instead of com.yammer.metrics.HealthChecks.

 
 public class HealthCheckServlet extends HttpServlet {
    
The attribute name of the com.yammer.metrics.core.HealthCheckRegistry instance in the servlet context.
 
     public static final String REGISTRY_ATTRIBUTE = HealthCheckServlet.class.getName() + ".registry";
     private static final String CONTENT_TYPE = "text/plain";
 
     private HealthCheckRegistry registry;

    
 
     public HealthCheckServlet(HealthCheckRegistry registry) {
         this. = registry;
     }

    
 
     public HealthCheckServlet() {
         this(HealthChecks.defaultRegistry());
     }
 
     @Override
     public void init(ServletConfig configthrows ServletException {
         final Object o = config.getServletContext().getAttribute();
         if (o instanceof HealthCheckRegistry) {
             this. = (HealthCheckRegistryo;
         }
     }
 
     @Override
     protected void doGet(HttpServletRequest req,
                          HttpServletResponse respthrows ServletExceptionIOException {
         final Map<StringHealthCheck.Resultresults = .runHealthChecks();
         resp.setContentType();
         resp.setHeader("Cache-Control""must-revalidate,no-cache,no-store");
         final PrintWriter writer = resp.getWriter();
         if (results.isEmpty()) {
             resp.setStatus(.);
             writer.println("! No health checks registered.");
         } else {
             if (isAllHealthy(results)) {
                 resp.setStatus(.);
             } else {
                 resp.setStatus(.);
             }
             for (Map.Entry<StringHealthCheck.Resultentry : results.entrySet()) {
                 final HealthCheck.Result result = entry.getValue();
                 if (result.isHealthy()) {
                     if (result.getMessage() != null) {
                         writer.format("* %s: OK\n  %s\n"entry.getKey(), result.getMessage());
                     } else {
                         writer.format("* %s: OK\n"entry.getKey());
                     }
                 } else {
                     if (result.getMessage() != null) {
                         writer.format("! %s: ERROR\n!  %s\n"entry.getKey(), result.getMessage());
                     }
 
                     @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
                     final Throwable error = result.getError();
                     if (error != null) {
                         writer.println();
                         error.printStackTrace(writer);
                         writer.println();
                     }
                 }
             }
         }
         writer.close();
     }
 
    private static boolean isAllHealthy(Map<StringHealthCheck.Resultresults) {
        for (HealthCheck.Result result : results.values()) {
            if (!result.isHealthy()) {
                return false;
            }
        }
        return true;
    }
New to GrepCode? Check out our FAQ X