-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Discovery service health check (#2312)
* Discovery service health check Signed-off-by: Boris Petkov <[email protected]> * Discovery service health check with Partial status Signed-off-by: Boris Petkov <[email protected]>
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...ervice/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
boolean gatewayDown = this.discoveryClient.getInstances(gatewayServiceId).isEmpty(); | ||
builder | ||
.status(gatewayDown ? new Status("PARTIAL", "Authenticated endpoints not available.") : Status.UP) | ||
.withDetail(gatewayServiceId, gatewayDown ? Status.DOWN : Status.UP); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ce/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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(Status.UP, builder.build().getDetails().get("gateway")); | ||
} | ||
|
||
@Test | ||
void testStatusIsPartialWhenGatewayIsNotAvailable() { | ||
when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(Collections.emptyList()); | ||
|
||
discoverServiceHealthIndicator.doHealthCheck(builder); | ||
|
||
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters