Merge branch '2.7.x'
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.boot.actuate.elasticsearch;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.elasticsearch.client.Request;
|
||||
import org.elasticsearch.client.Response;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.json.JsonParser;
|
||||
import org.springframework.boot.json.JsonParserFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
|
||||
*
|
||||
* @author Artsiom Yudovin
|
||||
* @author Brian Clozel
|
||||
* @author Filip Hrisafov
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public class ElasticsearchRestClientHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private static final String RED_STATUS = "red";
|
||||
|
||||
private final RestClient client;
|
||||
|
||||
private final JsonParser jsonParser;
|
||||
|
||||
public ElasticsearchRestClientHealthIndicator(RestClient client) {
|
||||
super("Elasticsearch health check failed");
|
||||
this.client = client;
|
||||
this.jsonParser = JsonParserFactory.getJsonParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
|
||||
builder.down();
|
||||
builder.withDetail("statusCode", statusLine.getStatusCode());
|
||||
builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
|
||||
return;
|
||||
}
|
||||
try (InputStream inputStream = response.getEntity().getContent()) {
|
||||
doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private void doHealthCheck(Health.Builder builder, String json) {
|
||||
Map<String, Object> response = this.jsonParser.parseMap(json);
|
||||
String status = (String) response.get("status");
|
||||
if (RED_STATUS.equals(status)) {
|
||||
builder.outOfService();
|
||||
}
|
||||
else {
|
||||
builder.up();
|
||||
}
|
||||
builder.withDetails(response);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,22 +16,9 @@
|
||||
|
||||
package org.springframework.boot.actuate.elasticsearch;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.elasticsearch.client.Request;
|
||||
import org.elasticsearch.client.Response;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.json.JsonParser;
|
||||
import org.springframework.boot.json.JsonParserFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
|
||||
@@ -40,58 +27,18 @@ import org.springframework.util.StreamUtils;
|
||||
* @author Brian Clozel
|
||||
* @author Filip Hrisafov
|
||||
* @since 2.1.1
|
||||
* @deprecated since 2.7.0 for removal in 2.9.0 in favor of
|
||||
* {@link ElasticsearchRestClientHealthIndicator}
|
||||
*/
|
||||
public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
|
||||
@Deprecated
|
||||
public class ElasticsearchRestHealthIndicator extends ElasticsearchRestClientHealthIndicator {
|
||||
|
||||
private static final String RED_STATUS = "red";
|
||||
|
||||
private final RestClient client;
|
||||
|
||||
private final JsonParser jsonParser;
|
||||
|
||||
/**
|
||||
* Create a new {@code ElasticsearchRestHealthIndicator} using the {@link RestClient}
|
||||
* obtained from the given high-level client.
|
||||
* @param client the high-level client
|
||||
* @deprecated since 2.7.0 for removal in 2.9.0 in favor of
|
||||
* {@link #ElasticsearchRestHealthIndicator(RestClient)}
|
||||
*/
|
||||
@Deprecated
|
||||
public ElasticsearchRestHealthIndicator(org.elasticsearch.client.RestHighLevelClient client) {
|
||||
this(client.getLowLevelClient());
|
||||
}
|
||||
|
||||
public ElasticsearchRestHealthIndicator(RestClient client) {
|
||||
super("Elasticsearch health check failed");
|
||||
this.client = client;
|
||||
this.jsonParser = JsonParserFactory.getJsonParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
|
||||
builder.down();
|
||||
builder.withDetail("statusCode", statusLine.getStatusCode());
|
||||
builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
|
||||
return;
|
||||
}
|
||||
try (InputStream inputStream = response.getEntity().getContent()) {
|
||||
doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private void doHealthCheck(Health.Builder builder, String json) {
|
||||
Map<String, Object> response = this.jsonParser.parseMap(json);
|
||||
String status = (String) response.get("status");
|
||||
if (RED_STATUS.equals(status)) {
|
||||
builder.outOfService();
|
||||
}
|
||||
else {
|
||||
builder.up();
|
||||
}
|
||||
builder.withDetails(response);
|
||||
super(client);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,16 +37,16 @@ import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ElasticsearchRestHealthIndicator}.
|
||||
* Tests for {@link ElasticsearchRestClientHealthIndicator}.
|
||||
*
|
||||
* @author Artsiom Yudovin
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
class ElasticsearchRestHealthIndicatorTests {
|
||||
class ElasticsearchRestClientHealthIndicatorTests {
|
||||
|
||||
private final RestClient restClient = mock(RestClient.class);
|
||||
|
||||
private final ElasticsearchRestHealthIndicator elasticsearchRestHealthIndicator = new ElasticsearchRestHealthIndicator(
|
||||
private final ElasticsearchRestClientHealthIndicator elasticsearchRestClientHealthIndicator = new ElasticsearchRestClientHealthIndicator(
|
||||
this.restClient);
|
||||
|
||||
@Test
|
||||
@@ -59,7 +59,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(response.getEntity()).willReturn(httpEntity);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "green");
|
||||
}
|
||||
@@ -74,7 +74,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(response.getEntity()).willReturn(httpEntity);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
|
||||
}
|
||||
@@ -82,7 +82,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
@Test
|
||||
void elasticsearchIsDown() throws IOException {
|
||||
given(this.restClient.performRequest(any(Request.class))).willThrow(new IOException("Couldn't connect"));
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).contains(entry("error", "java.io.IOException: Couldn't connect"));
|
||||
}
|
||||
@@ -95,7 +95,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(statusLine.getReasonPhrase()).willReturn("Internal server error");
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).contains(entry("statusCode", 500),
|
||||
entry("reasonPhrase", "Internal server error"));
|
||||
@@ -111,7 +111,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(response.getEntity()).willReturn(httpEntity);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "red");
|
||||
}
|
||||
Reference in New Issue
Block a user