Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HealthCheckServlet object mapper and status indicator #1871

Merged
merged 4 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.codahale.metrics.servlets;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.ExecutorService;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckFilter;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.json.HealthCheckModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
Expand All @@ -14,13 +15,11 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckFilter;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.json.HealthCheckModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.ExecutorService;

public class HealthCheckServlet extends HttpServlet {
public static abstract class ContextListener implements ServletContextListener {
Expand All @@ -46,11 +45,21 @@ protected HealthCheckFilter getHealthCheckFilter() {
return HealthCheckFilter.ALL;
}

/**
* @return the {@link ObjectMapper} that shall be used to render health checks,
* or {@code null} if the default object mapper should be used.
*/
protected ObjectMapper getObjectMapper() {
// don't use an object mapper by default
return null;
}

@Override
public void contextInitialized(ServletContextEvent event) {
final ServletContext context = event.getServletContext();
context.setAttribute(HEALTH_CHECK_REGISTRY, getHealthCheckRegistry());
context.setAttribute(HEALTH_CHECK_EXECUTOR, getExecutorService());
context.setAttribute(HEALTH_CHECK_MAPPER, getObjectMapper());
}

@Override
Expand All @@ -62,14 +71,18 @@ public void contextDestroyed(ServletContextEvent event) {
public static final String HEALTH_CHECK_REGISTRY = HealthCheckServlet.class.getCanonicalName() + ".registry";
public static final String HEALTH_CHECK_EXECUTOR = HealthCheckServlet.class.getCanonicalName() + ".executor";
public static final String HEALTH_CHECK_FILTER = HealthCheckServlet.class.getCanonicalName() + ".healthCheckFilter";
public static final String HEALTH_CHECK_MAPPER = HealthCheckServlet.class.getCanonicalName() + ".mapper";
public static final String HEALTH_CHECK_HTTP_STATUS_INDICATOR = HealthCheckServlet.class.getCanonicalName() + ".httpStatusIndicator";

private static final long serialVersionUID = -8432996484889177321L;
private static final String CONTENT_TYPE = "application/json";
private static final String HTTP_STATUS_INDICATOR_PARAM = "httpStatusIndicator";

private transient HealthCheckRegistry registry;
private transient ExecutorService executorService;
private transient HealthCheckFilter filter;
private transient ObjectMapper mapper;
private transient boolean httpStatusIndicator;

public HealthCheckServlet() {
}
Expand Down Expand Up @@ -97,7 +110,6 @@ public void init(ServletConfig config) throws ServletException {
this.executorService = (ExecutorService) executorAttr;
}


final Object filterAttr = context.getAttribute(HEALTH_CHECK_FILTER);
if (filterAttr instanceof HealthCheckFilter) {
filter = (HealthCheckFilter) filterAttr;
Expand All @@ -106,7 +118,20 @@ public void init(ServletConfig config) throws ServletException {
filter = HealthCheckFilter.ALL;
}

this.mapper = new ObjectMapper().registerModule(new HealthCheckModule());
final Object mapperAttr = context.getAttribute(HEALTH_CHECK_MAPPER);
if (mapperAttr instanceof ObjectMapper) {
this.mapper = (ObjectMapper) mapperAttr;
} else {
this.mapper = new ObjectMapper();
}
this.mapper.registerModule(new HealthCheckModule());

final Object httpStatusIndicatorAttr = context.getAttribute(HEALTH_CHECK_HTTP_STATUS_INDICATOR);
if (httpStatusIndicatorAttr instanceof Boolean) {
this.httpStatusIndicator = (Boolean) httpStatusIndicatorAttr;
} else {
this.httpStatusIndicator = true;
}
}

@Override
Expand All @@ -124,7 +149,10 @@ protected void doGet(HttpServletRequest req,
if (results.isEmpty()) {
resp.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
} else {
if (isAllHealthy(results)) {
final String reqParameter = req.getParameter(HTTP_STATUS_INDICATOR_PARAM);
final boolean httpStatusIndicatorParam = Boolean.parseBoolean(reqParameter);
final boolean useHttpStatusForHealthCheck = reqParameter == null ? httpStatusIndicator : httpStatusIndicatorParam;
if (!useHttpStatusForHealthCheck || isAllHealthy(results)) {
resp.setStatus(HttpServletResponse.SC_OK);
} else {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -159,4 +187,9 @@ private static boolean isAllHealthy(Map<String, HealthCheck.Result> results) {
}
return true;
}

// visible for testing
ObjectMapper getMapper() {
return mapper;
}
}
Loading