From 63411389c7d784ce23a82230e4021e7ec1d517fe Mon Sep 17 00:00:00 2001 From: Yinan Liu <60390925+yinan-symphony@users.noreply.github.com> Date: Wed, 24 May 2023 09:13:57 +0200 Subject: [PATCH] Fix health info parsing bug (#770) When extended health info api returns 5xx, the response body cannot be parsed into json model, in this case, we should consider all compoments are down. --- .../service/SymphonyBdkHealthIndicator.java | 33 ++++++++++++++++--- .../SymphonyBdkHealthIndicatorTest.java | 18 ++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/main/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicator.java b/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/main/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicator.java index 0e152bc60..8f3d9dd43 100644 --- a/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/main/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicator.java +++ b/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/main/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicator.java @@ -6,6 +6,7 @@ import com.symphony.bdk.gen.api.model.V3HealthStatus; import com.symphony.bdk.http.api.ApiRuntimeException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apiguardian.api.API; @@ -26,6 +27,8 @@ @API(status = API.Status.INTERNAL) public class SymphonyBdkHealthIndicator extends AbstractHealthIndicator { + public static final String DOWN = "DOWN"; + public static final String WARNING = "WARNING"; private final HealthService healthService; private static final String POD = "pod"; @@ -42,13 +45,23 @@ public SymphonyBdkHealthIndicator(HealthService healthService) { @Override protected void doHealthCheck(Health.Builder builder) throws Exception { - V3Health health = null; try { - health = healthService.healthCheckExtended(); + V3Health health = healthService.healthCheckExtended(); + buildHealthDetail(builder, health); } catch (ApiRuntimeException e) { - log.debug("Health check failed.", e); - health = MAPPER.readValue(e.getResponseBody(), V3Health.class); + log.debug("Health check failed, trying to parse the failure response body.", e); + log.trace("Health check failure response body - {}", e.getResponseBody()); + try { + V3Health health = MAPPER.readValue(e.getResponseBody(), V3Health.class); + buildHealthDetail(builder, health); + } catch (JsonProcessingException exception) { + log.debug("Failed to parse the health check failure response body, assume the global health state is down.", e); + buildHealthDownDetail(builder); + } } + } + + private void buildHealthDetail(Health.Builder builder, V3Health health) { Map services = health.getServices(); Map users = health.getUsers(); V3HealthStatus podStatus = services.get(POD).getStatus(); @@ -60,7 +73,7 @@ protected void doHealthCheck(Health.Builder builder) throws Exception { boolean global = podStatus == V3HealthStatus.UP && dfStatus == V3HealthStatus.UP && kmStatus == V3HealthStatus.UP && agtStatus == V3HealthStatus.UP && datafeedLoop == V3HealthStatus.UP; - builder.status(global ? Status.UP.getCode() : "WARNING") + builder.status(global ? Status.UP.getCode() : WARNING) .withDetail(POD, services.get(POD)) .withDetail(DF, services.get(DF)) .withDetail(KM, services.get(KM)) @@ -68,4 +81,14 @@ protected void doHealthCheck(Health.Builder builder) throws Exception { .withDetail(CE, users.get(CE)) .withDetail(DFL, datafeedLoop); } + + private void buildHealthDownDetail(Health.Builder builder) { + builder.status(DOWN) + .withDetail(POD, DOWN) + .withDetail(DF, DOWN) + .withDetail(KM, DOWN) + .withDetail(AGT, DOWN) + .withDetail(CE, DOWN) + .withDetail(DFL, DOWN); + } } diff --git a/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/test/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicatorTest.java b/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/test/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicatorTest.java index 0190ea6b9..32d3cbc03 100644 --- a/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/test/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicatorTest.java +++ b/symphony-bdk-spring/symphony-bdk-app-spring-boot-starter/src/test/java/com/symphony/bdk/app/spring/service/SymphonyBdkHealthIndicatorTest.java @@ -96,4 +96,22 @@ void doHealthCheck_exception() throws Exception { Health build = builder.build(); assertThat(build.getStatus().getCode()).isEqualTo("WARNING"); } + + @Test + void doHealthCheck_badGw_exception() throws Exception { + final String body = "\n" + + "502 Bad Gateway\n" + + "\n" + + "

502 Bad Gateway

\n" + + "\n" + + ""; + + + doThrow(new ApiRuntimeException(new ApiException(502, "message", Collections.EMPTY_MAP, body))).when(healthService) + .healthCheckExtended(); + Health.Builder builder = new Health.Builder(); + healthIndicator.doHealthCheck(builder); + Health build = builder.build(); + assertThat(build.getStatus().getCode()).isEqualTo("DOWN"); + } }