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

Update to MP Health 2.0-RC1 #35

Merged
merged 2 commits into from
May 5, 2019
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
Expand Up @@ -6,15 +6,19 @@


public class SmallRyeHealth {

private JsonObject payload;

public SmallRyeHealth(JsonObject payload) {
this.payload = payload;
}

public JsonObject getPayload() {
return payload;
}

public boolean isDown() {
return HealthCheckResponse.State.DOWN.toString().equals(payload.getString("outcome"));
return HealthCheckResponse.State.DOWN.toString().equals(payload.getString("status"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.eclipse.microprofile.health.Liveness;
import org.eclipse.microprofile.health.Readiness;
import org.jboss.logging.Logger;


Expand All @@ -43,7 +45,15 @@ public class SmallRyeHealthReporter {
*/
@Inject
@Health
Instance<HealthCheck> checks;
Instance<HealthCheck> healthChecks;

@Inject
@Liveness
Instance<HealthCheck> livenessChecks;

@Inject
@Readiness
Instance<HealthCheck> readinessChecks;

@Inject
@ConfigProperty(name = "io.smallrye.health.uncheckedExceptionDataStyle", defaultValue = ROOT_CAUSE)
Expand Down Expand Up @@ -75,41 +85,63 @@ public void reportHealth(OutputStream out, SmallRyeHealth health) {
writer.writeObject(health.getPayload());
writer.close();
}

public SmallRyeHealth getHealth() {
return getHealth(healthChecks, livenessChecks, readinessChecks);
}

public SmallRyeHealth getLiveness() {
return getHealth(livenessChecks);
}

public SmallRyeHealth getReadiness() {
return getHealth(readinessChecks);
}

@SafeVarargs
private final SmallRyeHealth getHealth(Instance<HealthCheck>... checks) {
JsonArrayBuilder results = Json.createArrayBuilder();
HealthCheckResponse.State outcome = HealthCheckResponse.State.UP;
HealthCheckResponse.State status = HealthCheckResponse.State.UP;

if (checks != null) {
for (HealthCheck check : checks) {
outcome = fillCheck(check, results, outcome);
for (Instance<HealthCheck> instance : checks) {
status = processChecks(instance, results, status);
}
}

if (!additionalChecks.isEmpty()) {
for (HealthCheck check : additionalChecks) {
outcome = fillCheck(check, results, outcome);
}
status = processChecks(additionalChecks, results, status);
}

JsonObjectBuilder builder = Json.createObjectBuilder();

JsonArray checkResults = results.build();

builder.add("outcome", checkResults.isEmpty() ? emptyChecksOutcome : outcome.toString());
builder.add("status", checkResults.isEmpty() ? emptyChecksOutcome : status.toString());
builder.add("checks", checkResults);

return new SmallRyeHealth(builder.build());
}

private HealthCheckResponse.State processChecks(Iterable<HealthCheck> checks, JsonArrayBuilder results, HealthCheckResponse.State status) {
if (checks != null) {
for (HealthCheck check : checks) {
status = fillCheck(check, results, status);
}
}

return status;
}

private HealthCheckResponse.State fillCheck(HealthCheck check, JsonArrayBuilder results, HealthCheckResponse.State globalOutcome) {
if (check == null) {
return globalOutcome;
}
JsonObject each = jsonObject(check);
results.add(each);
if (globalOutcome == HealthCheckResponse.State.UP) {
String state = each.getString("state");
if (state.equals("DOWN")) {
String status = each.getString("status");
if (status.equals("DOWN")) {
return HealthCheckResponse.State.DOWN;
}
}
Expand Down Expand Up @@ -145,7 +177,7 @@ private JsonObject jsonObject(HealthCheck check) {
private JsonObject jsonObject(HealthCheckResponse response) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("name", response.getName());
builder.add("state", response.getState().toString());
builder.add("status", response.getState().toString());
response.getData().ifPresent(d -> {
JsonObjectBuilder data = Json.createObjectBuilder();
for (Map.Entry<String, Object> entry : d.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testDefaultGetHealth() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(false));
assertThat(health.getPayload().getString("outcome"), is("UP"));
assertThat(health.getPayload().getString("status"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks"), is(empty()));
}

Expand All @@ -60,7 +60,7 @@ public void testGetHealthWithEmptyChecksOutcomeDown() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks"), is(empty()));
}

Expand All @@ -71,9 +71,9 @@ public void testGetHealthWithFailingCheckAndStyleDefault() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("name"), is(FailingHealthCheck.class.getName()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data").getString("rootCause"), is("this health check has failed"));
}

Expand All @@ -85,9 +85,9 @@ public void testGetHealthWithFailingCheckAndStyleNone() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("name"), is(FailingHealthCheck.class.getName()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"), is(nullValue()));
}

Expand All @@ -99,9 +99,9 @@ public void testGetHealthWithFailingCheckAndStyleStackTrace() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("name"), is(FailingHealthCheck.class.getName()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data").getString("stackTrace"), is(notNullValue()));
}

Expand All @@ -114,14 +114,14 @@ public void testGetHealthWithMixedChecksAndStyleDefault() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("name"), is("up"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("state"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("status"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("name"), is(FailingHealthCheck.class.getName()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getJsonObject("data").getString("rootCause"), is("this health check has failed"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("name"), is("down"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("status"), is("DOWN"));
}

@Test
Expand All @@ -134,14 +134,14 @@ public void testGetHealthWithMixedChecksAndStyleNone() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("name"), is("up"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("state"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("status"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("name"), is(FailingHealthCheck.class.getName()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getJsonObject("data"), is(nullValue()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("name"), is("down"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("status"), is("DOWN"));
}

@Test
Expand All @@ -154,13 +154,13 @@ public void testGetHealthWithMixedChecksAndStyleStackTrace() {
SmallRyeHealth health = reporter.getHealth();

assertThat(health.isDown(), is(true));
assertThat(health.getPayload().getString("outcome"), is("DOWN"));
assertThat(health.getPayload().getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("name"), is("up"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("state"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(0).getString("status"), is("UP"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("name"), is(FailingHealthCheck.class.getName()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getString("status"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(1).getJsonObject("data").getString("stackTrace"), is(notNullValue()));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("name"), is("down"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("state"), is("DOWN"));
assertThat(health.getPayload().getJsonArray("checks").getJsonObject(2).getString("status"), is("DOWN"));
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<properties>
<version.asciidoctor.plugin>1.6.0</version.asciidoctor.plugin>
<version.eclipse.microprofile.config>1.3</version.eclipse.microprofile.config>
<version.eclipse.microprofile.health>1.0</version.eclipse.microprofile.health>
<version.eclipse.microprofile.health>2.0-RC1</version.eclipse.microprofile.health>
<version.javax.javaee-api>7.0</version.javax.javaee-api>
<version.javax.enterprise.cdi-api>2.0</version.javax.enterprise.cdi-api>
<version.junit>4.11</version.junit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
@WebServlet(name = "SmallRyeHealthServlet", urlPatterns = "/*")
@WebServlet(name = "SmallRyeHealthServlet", urlPatterns = "/health")
public class SmallRyeHealthServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

SmallRyeHealth health = reporter.getHealth();
if (health.isDown()) {
Expand Down
29 changes: 29 additions & 0 deletions tck/src/main/java/io/smallrye/health/SmallRyeLivenessServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.smallrye.health;

import javax.inject.Inject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@SuppressWarnings("serial")
@WebServlet(name = "SmallRyeLivenessServlet", urlPatterns = "/health/live")
public class SmallRyeLivenessServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

SmallRyeHealth health = reporter.getLiveness();
if (health.isDown()) {
resp.setStatus(503);
}
reporter.reportHealth(resp.getOutputStream(), health);
}

@Inject
private SmallRyeHealthReporter reporter;
}


29 changes: 29 additions & 0 deletions tck/src/main/java/io/smallrye/health/SmallRyeReadinessServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.smallrye.health;

import javax.inject.Inject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@SuppressWarnings("serial")
@WebServlet(name = "SmallRyeReadinessServlet", urlPatterns = "/health/ready")
public class SmallRyeReadinessServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

SmallRyeHealth health = reporter.getReadiness();
if (health.isDown()) {
resp.setStatus(503);
}
reporter.reportHealth(resp.getOutputStream(), health);
}

@Inject
private SmallRyeHealthReporter reporter;
}