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

add option to configure Consul check status considered as healthy #599

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -17,11 +17,15 @@
package org.springframework.cloud.consul.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ecwid.consul.v1.ConsistencyMode;
import com.ecwid.consul.v1.health.model.Check;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down Expand Up @@ -160,6 +164,12 @@ public class ConsulDiscoveryProperties {
*/
private boolean queryPassing = false;

/**
* List of status considered as healthy. Contains "passing" by default.
*/
private Set<Check.CheckStatus> statusConsideredAsHealthy = new HashSet(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document the new property

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add the explanation of this feature to docs/src/main/asciidoc/spring-cloud-consul.adoc.
docs/src/main/asciidoc/_configprops.adoc will be automatically generated for you so you don't need to make any changes here.

Collections.singleton(Check.CheckStatus.PASSING));

/** Register as a service in consul. */
private boolean register = true;

Expand Down Expand Up @@ -470,10 +480,19 @@ public boolean isQueryPassing() {
return this.queryPassing;
}

public Set<Check.CheckStatus> getStatusConsideredAsHealthy() {
return statusConsideredAsHealthy;
}

public void setQueryPassing(boolean queryPassing) {
this.queryPassing = queryPassing;
}

public void setStatusConsideredAsHealthy(
Set<Check.CheckStatus> statusConsideredAsHealthy) {
this.statusConsideredAsHealthy = statusConsideredAsHealthy;
}

public boolean isRegister() {
return this.register;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.consul.discovery;

import java.util.Map;
import java.util.Set;

import com.ecwid.consul.v1.health.model.Check;
import com.ecwid.consul.v1.health.model.HealthService;
Expand All @@ -33,11 +34,15 @@ public class ConsulServer extends Server {

private final HealthService service;

private final Set<Check.CheckStatus> statusConsideredAsHealthy;

private final Map<String, String> metadata;

public ConsulServer(final HealthService healthService) {
public ConsulServer(final HealthService healthService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to maintain backwards compatibility so we need to leave this constructor as is (perhaps mark it as deprecated) and add a new one.

Set<Check.CheckStatus> statusConsideredAsHealthy) {
super(findHost(healthService), healthService.getService().getPort());
this.service = healthService;
this.statusConsideredAsHealthy = statusConsideredAsHealthy;
this.metadata = ConsulServerUtils.getMetadata(this.service);
this.metaInfo = new MetaInfo() {
@Override
Expand Down Expand Up @@ -79,7 +84,7 @@ public Map<String, String> getMetadata() {

public boolean isPassingChecks() {
for (Check check : this.service.getChecks()) {
if (check.getStatus() != Check.CheckStatus.PASSING) {
if (!statusConsideredAsHealthy.contains(check.getStatus())) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.health.model.Check;
import com.ecwid.consul.v1.health.model.HealthService;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList;
Expand Down Expand Up @@ -83,20 +85,24 @@ private List<ConsulServer> getServers() {
if (response.getValue() == null || response.getValue().isEmpty()) {
return Collections.emptyList();
}
return transformResponse(response.getValue());
return transformResponse(response.getValue(),
this.properties.getStatusConsideredAsHealthy());
}

/**
* Transforms the response from Consul in to a list of usable {@link ConsulServer}s.
* @param healthServices the initial list of servers from Consul. Guaranteed to be
* non-empty list
* @param statusConsideredAsHealthy list of status that are considered as
* healthy/passing
* @return ConsulServer instances
* @see ConsulServer#ConsulServer(HealthService)
* @see ConsulServer#ConsulServer(HealthService, java.util.Set)
*/
protected List<ConsulServer> transformResponse(List<HealthService> healthServices) {
protected List<ConsulServer> transformResponse(List<HealthService> healthServices,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid changing the method signature I would use this.properties in the method rather than pass in the Set. You can add an overloaded method if you want to give the option to past in the Set

Set<Check.CheckStatus> statusConsideredAsHealthy) {
List<ConsulServer> servers = new ArrayList<>();
for (HealthService service : healthServices) {
ConsulServer server = new ConsulServer(service);
ConsulServer server = new ConsulServer(service, statusConsideredAsHealthy);
if (server.getMetadata()
.containsKey(this.properties.getDefaultZoneMetadataName())) {
server.setZone(server.getMetadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.Map;

import com.ecwid.consul.v1.health.model.Check;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -95,4 +96,17 @@ public void testAddManagementTag() {
.containsOnly(ConsulDiscoveryProperties.MANAGEMENT, "newTag");
}

@Test
public void testConsiderStatusAsHealthyContainsPassingByDefault() {
assertThat(this.properties.getStatusConsideredAsHealthy())
.containsExactly(Check.CheckStatus.PASSING);
}

@Test
public void testConsiderWarningStatusAsHealthy() {
this.properties.getStatusConsideredAsHealthy().add(Check.CheckStatus.WARNING);
assertThat(this.properties.getStatusConsideredAsHealthy())
.containsExactly(Check.CheckStatus.WARNING, Check.CheckStatus.PASSING);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.consul.discovery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.ecwid.consul.v1.health.model.Check;
import com.ecwid.consul.v1.health.model.HealthService;
import org.junit.Test;

import static com.ecwid.consul.v1.health.model.Check.CheckStatus.CRITICAL;
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.PASSING;
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.WARNING;
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Christian Lorenz
*/
public class ConsulServerTest {

@Test
public void testIsPassingChecks() {
Set<Check.CheckStatus> acceptedStatus = new HashSet<>(
Arrays.asList(PASSING, WARNING));
assertThat(newServer(PASSING, acceptedStatus).isPassingChecks()).isTrue();
assertThat(newServer(WARNING, acceptedStatus).isPassingChecks()).isTrue();
assertThat(newServer(CRITICAL, acceptedStatus).isPassingChecks()).isFalse();
}

static ConsulServer newServer(Check.CheckStatus checkStatus,
Set<Check.CheckStatus> acceptedStatus) {
HealthService healthService = new HealthService();
HealthService.Node node = new HealthService.Node();
node.setAddress("nodeaddr" + checkStatus.name());
node.setNode("nodenode" + checkStatus.name());
healthService.setNode(node);
HealthService.Service service = new HealthService.Service();
service.setAddress("serviceaddr" + checkStatus.name());
service.setId("serviceid" + checkStatus.name());
service.setPort(8080);
service.setService("serviceservice" + checkStatus.name());
healthService.setService(service);
ArrayList<Check> checks = new ArrayList<>();
Check check = new Check();
check.setStatus(checkStatus);
checks.add(check);
healthService.setChecks(checks);
return new ConsulServer(healthService, acceptedStatus);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
package org.springframework.cloud.consul.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ecwid.consul.v1.health.model.Check;
import com.ecwid.consul.v1.health.model.HealthService;
import com.netflix.loadbalancer.Server;
import org.junit.Test;

import static com.ecwid.consul.v1.health.model.Check.CheckStatus.CRITICAL;
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.PASSING;
import static com.ecwid.consul.v1.health.model.Check.CheckStatus.WARNING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.consul.discovery.ConsulServerTest.newServer;

/**
* @author Spencer Gibb
Expand All @@ -39,33 +39,13 @@ public void testGetFilteredListOfServers() {
HealthServiceServerListFilter filter = new HealthServiceServerListFilter();

ArrayList<Server> servers = new ArrayList<>();
servers.add(newServer(PASSING));
servers.add(newServer(PASSING));
servers.add(newServer(CRITICAL));
servers.add(newServer(WARNING));
servers.add(newServer(PASSING, Collections.singleton(PASSING)));
servers.add(newServer(PASSING, Collections.singleton(PASSING)));
servers.add(newServer(CRITICAL, Collections.singleton(PASSING)));
servers.add(newServer(WARNING, Collections.singleton(PASSING)));

List<Server> filtered = filter.getFilteredListOfServers(servers);
assertThat(filtered).as("wrong # of filtered servers").hasSize(2);
}

private ConsulServer newServer(Check.CheckStatus checkStatus) {
HealthService healthService = new HealthService();
HealthService.Node node = new HealthService.Node();
node.setAddress("nodeaddr" + checkStatus.name());
node.setNode("nodenode" + checkStatus.name());
healthService.setNode(node);
HealthService.Service service = new HealthService.Service();
service.setAddress("serviceaddr" + checkStatus.name());
service.setId("serviceid" + checkStatus.name());
service.setPort(8080);
service.setService("serviceservice" + checkStatus.name());
healthService.setService(service);
ArrayList<Check> checks = new ArrayList<>();
Check check = new Check();
check.setStatus(checkStatus);
checks.add(check);
healthService.setChecks(checks);
return new ConsulServer(healthService);
}

}