Skip to content

Commit 2f167ff

Browse files
author
Boris Petkov
authored
feat: Discovery service health check (#2312)
* Discovery service health check Signed-off-by: Boris Petkov <boris.petkov@broadcom.com> * Discovery service health check with Partial status Signed-off-by: Boris Petkov <boris.petkov@broadcom.com>
1 parent 5b5880c commit 2f167ff

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
package org.zowe.apiml.discovery.health;
11+
12+
import lombok.RequiredArgsConstructor;
13+
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
14+
import org.springframework.boot.actuate.health.Health;
15+
import org.springframework.boot.actuate.health.Status;
16+
import org.springframework.cloud.client.discovery.DiscoveryClient;
17+
import org.springframework.stereotype.Component;
18+
import org.zowe.apiml.product.constants.CoreService;
19+
20+
/**
21+
* Discovery service health information (/application/health)
22+
*/
23+
@Component
24+
@RequiredArgsConstructor
25+
public class DiscoveryServiceHealthIndicator extends AbstractHealthIndicator {
26+
27+
private final DiscoveryClient discoveryClient;
28+
29+
@Override
30+
protected void doHealthCheck(Health.Builder builder) {
31+
String gatewayServiceId = CoreService.GATEWAY.getServiceId();
32+
boolean gatewayDown = this.discoveryClient.getInstances(gatewayServiceId).isEmpty();
33+
builder
34+
.status(gatewayDown ? new Status("PARTIAL", "Authenticated endpoints not available.") : Status.UP)
35+
.withDetail(gatewayServiceId, gatewayDown ? Status.DOWN : Status.UP);
36+
}
37+
}

discovery-service/src/main/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ management:
8787
endpoint:
8888
shutdown:
8989
enabled: true
90+
health:
91+
show-details: always
9092

9193
hystrix.command.default.execution.timeout.enabled: false
9294
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
package org.zowe.apiml.discovery.health;
11+
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.boot.actuate.health.Health;
15+
import org.springframework.boot.actuate.health.Status;
16+
import org.springframework.cloud.client.DefaultServiceInstance;
17+
import org.springframework.cloud.client.discovery.DiscoveryClient;
18+
import org.zowe.apiml.product.constants.CoreService;
19+
20+
import java.util.Collections;
21+
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.when;
24+
25+
class DiscoveryServiceHealthIndicatorTest {
26+
27+
private final DiscoveryClient discoveryClient = mock(DiscoveryClient.class);
28+
private final DiscoveryServiceHealthIndicator discoverServiceHealthIndicator = new DiscoveryServiceHealthIndicator(discoveryClient);
29+
private final Health.Builder builder = new Health.Builder();
30+
31+
@Test
32+
void testStatusIsUpWhenGatewayIsAvailable() {
33+
when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(
34+
Collections.singletonList(
35+
new DefaultServiceInstance(null, CoreService.GATEWAY.getServiceId(), "host", 10010, true)));
36+
37+
discoverServiceHealthIndicator.doHealthCheck(builder);
38+
39+
Assertions.assertEquals(Status.UP, builder.build().getStatus());
40+
Assertions.assertEquals(Status.UP, builder.build().getDetails().get("gateway"));
41+
}
42+
43+
@Test
44+
void testStatusIsPartialWhenGatewayIsNotAvailable() {
45+
when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(Collections.emptyList());
46+
47+
discoverServiceHealthIndicator.doHealthCheck(builder);
48+
49+
Assertions.assertEquals(new Status("PARTIAL"), builder.build().getStatus());
50+
Assertions.assertEquals("Authenticated endpoints not available.", builder.build().getStatus().getDescription());
51+
Assertions.assertEquals(Status.DOWN, builder.build().getDetails().get("gateway"));
52+
}
53+
}

discovery-service/src/test/resources/application.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ management:
4747
health:
4848
defaults:
4949
enabled: false
50+
endpoint:
51+
health:
52+
status:
53+
order: "DOWN,PARTIAL,UP"
54+
http-mapping:
55+
DOWN: 503
56+
PARTIAL: 200
57+
show-details: always
5058
---
5159
spring:
5260
profiles: https

0 commit comments

Comments
 (0)