Improve Couchbase health indicator
This commit improve the couchbase health indicator to list the available nodes. Doing so improves the reliability of the indicator as accessing the bucket forces a remote call and better detect the cases where the broker is down. Closes gh-7369
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.couchbase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.bucket.BucketInfo;
|
||||
import com.couchbase.client.java.util.features.Version;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
@@ -31,6 +32,7 @@ import org.springframework.util.StringUtils;
|
||||
* {@link HealthIndicator} for Couchbase.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
|
||||
@@ -46,8 +48,13 @@ public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
List<Version> versions = this.couchbaseOperations.getCouchbaseClusterInfo()
|
||||
.getAllVersions();
|
||||
builder.up().withDetail("versions",
|
||||
StringUtils.collectionToCommaDelimitedString(versions));
|
||||
BucketInfo bucketInfo = this.couchbaseOperations.getCouchbaseBucket()
|
||||
.bucketManager().info();
|
||||
builder.up()
|
||||
.withDetail("versions", StringUtils.collectionToCommaDelimitedString(
|
||||
versions))
|
||||
.withDetail("nodes", StringUtils.collectionToCommaDelimitedString(
|
||||
bucketInfo.nodeList()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,8 +16,13 @@
|
||||
|
||||
package org.springframework.boot.actuate.couchbase;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.bucket.BucketInfo;
|
||||
import com.couchbase.client.java.bucket.BucketManager;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.util.features.Version;
|
||||
import org.junit.Test;
|
||||
@@ -27,6 +32,7 @@ import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.data.couchbase.core.CouchbaseOperations;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -35,22 +41,33 @@ import static org.mockito.Mockito.verify;
|
||||
* Tests for {@link CouchbaseHealthIndicator}
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CouchbaseHealthIndicatorTests {
|
||||
|
||||
@Test
|
||||
public void couchbaseIsUp() {
|
||||
CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class);
|
||||
public void couchbaseIsUp() throws UnknownHostException {
|
||||
BucketInfo bucketInfo = mock(BucketInfo.class);
|
||||
given(bucketInfo.nodeList()).willReturn(Collections.singletonList(
|
||||
InetAddress.getByName("127.0.0.1")));
|
||||
BucketManager bucketManager = mock(BucketManager.class);
|
||||
given(bucketManager.info()).willReturn(bucketInfo);
|
||||
Bucket bucket = mock(Bucket.class);
|
||||
given(bucket.bucketManager()).willReturn(bucketManager);
|
||||
ClusterInfo clusterInfo = mock(ClusterInfo.class);
|
||||
given(clusterInfo.getAllVersions())
|
||||
.willReturn(Arrays.asList(new Version(1, 2, 3)));
|
||||
.willReturn(Collections.singletonList(new Version(1, 2, 3)));
|
||||
CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class);
|
||||
given(couchbaseOperations.getCouchbaseBucket()).willReturn(bucket);
|
||||
given(couchbaseOperations.getCouchbaseClusterInfo()).willReturn(clusterInfo);
|
||||
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator(
|
||||
couchbaseOperations);
|
||||
Health health = healthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails().get("versions")).isEqualTo("1.2.3");
|
||||
assertThat(health.getDetails()).containsOnly(entry("versions", "1.2.3"),
|
||||
entry("nodes", "/127.0.0.1"));
|
||||
verify(clusterInfo).getAllVersions();
|
||||
verify(bucketInfo).nodeList();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user