Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Discovery service health check #2312

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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