All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qmetric.spark.metrics.HealthCheckRoute Maven / Gradle / Ivy

Go to download

A library of Route decorators to add metrics to spark based applications and Routes to access the metrics

There is a newer version: 1.19
Show newest version
package com.qmetric.spark.metrics;

import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.json.HealthCheckModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import spark.Request;
import spark.Response;
import spark.Route;

import javax.servlet.http.HttpServletResponse;

import java.io.OutputStream;
import java.util.Map;
import java.util.SortedMap;

public class HealthCheckRoute extends Route
{
    public static final String PATH = "/healthcheck";

    private final HealthCheckRegistry registry;

    private final ObjectMapper objectMapper;

    public HealthCheckRoute(final HealthCheckRegistry registry)
    {
        super(PATH);

        this.registry = registry;
        this.objectMapper = new ObjectMapper().registerModule(new HealthCheckModule());
    }

    @Override public Object handle(final Request request, final Response response)
    {
        final HttpServletResponse resp = response.raw();
        resp.setContentType("application/json");
        resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");

        final SortedMap results = runHealthChecks();
        if (results.isEmpty())
        {
            resp.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
        }
        else
        {
            if (isAllHealthy(results))
            {
                resp.setStatus(HttpServletResponse.SC_OK);
            }
            else
            {
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        }
        try (OutputStream output = resp.getOutputStream())
        {
            objectMapper.writerWithDefaultPrettyPrinter().writeValue(output, results);
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
        return null;
    }

    private SortedMap runHealthChecks()
    {
        return registry.runHealthChecks();
    }

    private static boolean isAllHealthy(Map results)
    {
        for (HealthCheck.Result result : results.values())
        {
            if (!result.isHealthy())
            {
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy