From 8ccc2e06d95c2f567ac466ef8d2063b97071e655 Mon Sep 17 00:00:00 2001 From: Boris Petkov Date: Thu, 21 Apr 2022 16:40:16 +0200 Subject: [PATCH 1/2] Discovery service health check Signed-off-by: Boris Petkov --- .../DiscoveryServiceHealthIndicator.java | 37 +++++++++++++ .../src/main/resources/application.yml | 2 + .../DiscoveryServiceHealthIndicatorTest.java | 52 +++++++++++++++++++ .../src/test/resources/application.yml | 3 ++ 4 files changed, 94 insertions(+) create mode 100644 discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java create mode 100644 discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java diff --git a/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java b/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java new file mode 100644 index 0000000000..1221bbc883 --- /dev/null +++ b/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java @@ -0,0 +1,37 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package org.zowe.apiml.discovery.health; + +import lombok.RequiredArgsConstructor; +import org.springframework.boot.actuate.health.AbstractHealthIndicator; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.stereotype.Component; +import org.zowe.apiml.product.constants.CoreService; + +/** + * Discovery service health information (/application/health) + */ +@Component +@RequiredArgsConstructor +public class DiscoveryServiceHealthIndicator extends AbstractHealthIndicator { + + private final DiscoveryClient discoveryClient; + + @Override + protected void doHealthCheck(Health.Builder builder) { + String gatewayServiceId = CoreService.GATEWAY.getServiceId(); + Status gatewayStatus = this.discoveryClient.getInstances(gatewayServiceId).isEmpty() ? Status.DOWN : Status.UP; + builder + .up() + .withDetail(gatewayServiceId, gatewayStatus.getCode()); + } +} diff --git a/discovery-service/src/main/resources/application.yml b/discovery-service/src/main/resources/application.yml index 4705355165..f1948d9a7b 100644 --- a/discovery-service/src/main/resources/application.yml +++ b/discovery-service/src/main/resources/application.yml @@ -87,6 +87,8 @@ management: endpoint: shutdown: enabled: true + health: + show-details: always hystrix.command.default.execution.timeout.enabled: false --- diff --git a/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java b/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java new file mode 100644 index 0000000000..ddde222bea --- /dev/null +++ b/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java @@ -0,0 +1,52 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package org.zowe.apiml.discovery.health; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.zowe.apiml.product.constants.CoreService; + +import java.util.Collections; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DiscoveryServiceHealthIndicatorTest { + + private final DiscoveryClient discoveryClient = mock(DiscoveryClient.class); + private final DiscoveryServiceHealthIndicator discoverServiceHealthIndicator = new DiscoveryServiceHealthIndicator(discoveryClient); + private final Health.Builder builder = new Health.Builder(); + + @Test + void testStatusIsUpWhenGatewayIsAvailable() { + when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn( + Collections.singletonList( + new DefaultServiceInstance(null, CoreService.GATEWAY.getServiceId(), "host", 10010, true))); + + discoverServiceHealthIndicator.doHealthCheck(builder); + + Assertions.assertEquals(Status.UP, builder.build().getStatus()); + Assertions.assertEquals("UP", builder.build().getDetails().get("gateway")); + } + + @Test + void testStatusIsUpWhenGatewayIsNotAvailable() { + when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(Collections.emptyList()); + + discoverServiceHealthIndicator.doHealthCheck(builder); + + Assertions.assertEquals(Status.UP, builder.build().getStatus()); + Assertions.assertEquals("DOWN", builder.build().getDetails().get("gateway")); + } +} diff --git a/discovery-service/src/test/resources/application.yml b/discovery-service/src/test/resources/application.yml index 0855b45e01..2b7e41af3d 100644 --- a/discovery-service/src/test/resources/application.yml +++ b/discovery-service/src/test/resources/application.yml @@ -47,6 +47,9 @@ management: health: defaults: enabled: false + endpoint: + health: + show-details: always --- spring: profiles: https From 55c4650447d90eeb568cd7c3406421a1a8091a7f Mon Sep 17 00:00:00 2001 From: Boris Petkov Date: Fri, 22 Apr 2022 10:20:38 +0200 Subject: [PATCH 2/2] Discovery service health check with Partial status Signed-off-by: Boris Petkov --- .../health/DiscoveryServiceHealthIndicator.java | 6 +++--- .../health/DiscoveryServiceHealthIndicatorTest.java | 9 +++++---- discovery-service/src/test/resources/application.yml | 5 +++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java b/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java index 1221bbc883..343bc35b1c 100644 --- a/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java +++ b/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java @@ -29,9 +29,9 @@ public class DiscoveryServiceHealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Health.Builder builder) { String gatewayServiceId = CoreService.GATEWAY.getServiceId(); - Status gatewayStatus = this.discoveryClient.getInstances(gatewayServiceId).isEmpty() ? Status.DOWN : Status.UP; + boolean gatewayDown = this.discoveryClient.getInstances(gatewayServiceId).isEmpty(); builder - .up() - .withDetail(gatewayServiceId, gatewayStatus.getCode()); + .status(gatewayDown ? new Status("PARTIAL", "Authenticated endpoints not available.") : Status.UP) + .withDetail(gatewayServiceId, gatewayDown ? Status.DOWN : Status.UP); } } diff --git a/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java b/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java index ddde222bea..fc790b38a5 100644 --- a/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java +++ b/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java @@ -37,16 +37,17 @@ void testStatusIsUpWhenGatewayIsAvailable() { discoverServiceHealthIndicator.doHealthCheck(builder); Assertions.assertEquals(Status.UP, builder.build().getStatus()); - Assertions.assertEquals("UP", builder.build().getDetails().get("gateway")); + Assertions.assertEquals(Status.UP, builder.build().getDetails().get("gateway")); } @Test - void testStatusIsUpWhenGatewayIsNotAvailable() { + void testStatusIsPartialWhenGatewayIsNotAvailable() { when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(Collections.emptyList()); discoverServiceHealthIndicator.doHealthCheck(builder); - Assertions.assertEquals(Status.UP, builder.build().getStatus()); - Assertions.assertEquals("DOWN", builder.build().getDetails().get("gateway")); + Assertions.assertEquals(new Status("PARTIAL"), builder.build().getStatus()); + Assertions.assertEquals("Authenticated endpoints not available.", builder.build().getStatus().getDescription()); + Assertions.assertEquals(Status.DOWN, builder.build().getDetails().get("gateway")); } } diff --git a/discovery-service/src/test/resources/application.yml b/discovery-service/src/test/resources/application.yml index 2b7e41af3d..36acf0dd37 100644 --- a/discovery-service/src/test/resources/application.yml +++ b/discovery-service/src/test/resources/application.yml @@ -49,6 +49,11 @@ management: enabled: false endpoint: health: + status: + order: "DOWN,PARTIAL,UP" + http-mapping: + DOWN: 503 + PARTIAL: 200 show-details: always --- spring: