Skip to content

Commit

Permalink
Fix health info parsing bug (#770)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yinan-symphony authored May 24, 2023
1 parent 4eb4abc commit 6341138
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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<String, V3HealthComponent> services = health.getServices();
Map<String, V3HealthComponent> users = health.getUsers();
V3HealthStatus podStatus = services.get(POD).getStatus();
Expand All @@ -60,12 +73,22 @@ 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))
.withDetail(AGT, users.get(AGT))
.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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html>\n"
+ "<head><title>502 Bad Gateway</title></head>\n"
+ "<body>\n"
+ "<center><h1>502 Bad Gateway</h1></center>\n"
+ "</body>\n"
+ "</html>";


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");
}
}

0 comments on commit 6341138

Please sign in to comment.