Skip to content

Commit

Permalink
feat: Discovery service health check (#2312)
Browse files Browse the repository at this point in the history
* 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
boris-bc authored Apr 22, 2022
1 parent 5b5880c commit 2f167ff
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
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);
}
}
2 changes: 2 additions & 0 deletions discovery-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ management:
endpoint:
shutdown:
enabled: true
health:
show-details: always

hystrix.command.default.execution.timeout.enabled: false
---
Expand Down
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"));
}
}
8 changes: 8 additions & 0 deletions discovery-service/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ management:
health:
defaults:
enabled: false
endpoint:
health:
status:
order: "DOWN,PARTIAL,UP"
http-mapping:
DOWN: 503
PARTIAL: 200
show-details: always
---
spring:
profiles: https
Expand Down

0 comments on commit 2f167ff

Please sign in to comment.