Allow custom AbstractHealthIndicator warning logs
Update `AbstractHealthIndicator` so that the warning message can be customized. Also updated our existing indicators with better messages. Fixes gh-11880
This commit is contained in:
parent
c3ec316890
commit
8af02ce05b
|
@ -34,6 +34,7 @@ public class RabbitHealthIndicator extends AbstractHealthIndicator {
|
|||
private final RabbitTemplate rabbitTemplate;
|
||||
|
||||
public RabbitHealthIndicator(RabbitTemplate rabbitTemplate) {
|
||||
super("Rabbit health check failed");
|
||||
Assert.notNull(rabbitTemplate, "RabbitTemplate must not be null");
|
||||
this.rabbitTemplate = rabbitTemplate;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,10 @@ public class CassandraHealthIndicator extends AbstractHealthIndicator {
|
|||
|
||||
private CassandraOperations cassandraOperations;
|
||||
|
||||
public CassandraHealthIndicator() {
|
||||
super("Cassandra health check failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link CassandraHealthIndicator} instance.
|
||||
* @param cassandraOperations the Cassandra operations
|
||||
|
|
|
@ -37,6 +37,10 @@ public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
|
|||
|
||||
private CouchbaseOperations operations;
|
||||
|
||||
public CouchbaseHealthIndicator() {
|
||||
super("Couchbase health check failed");
|
||||
}
|
||||
|
||||
public CouchbaseHealthIndicator(CouchbaseOperations couchbaseOperations) {
|
||||
Assert.notNull(couchbaseOperations, "CouchbaseOperations must not be null");
|
||||
this.operations = couchbaseOperations;
|
||||
|
|
|
@ -65,6 +65,7 @@ public class ElasticsearchHealthIndicator extends AbstractHealthIndicator {
|
|||
*/
|
||||
public ElasticsearchHealthIndicator(Client client, long responseTimeout,
|
||||
String... indices) {
|
||||
super("Elasticsearch health check failed");
|
||||
this.client = client;
|
||||
this.responseTimeout = responseTimeout;
|
||||
this.indices = indices;
|
||||
|
|
|
@ -40,6 +40,7 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
|
|||
private final JsonParser jsonParser = new JsonParser();
|
||||
|
||||
public ElasticsearchJestHealthIndicator(JestClient jestClient) {
|
||||
super("Elasticsearch health check failed");
|
||||
this.jestClient = jestClient;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,14 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Base {@link HealthIndicator} implementations that encapsulates creation of
|
||||
|
@ -34,8 +38,45 @@ import org.springframework.boot.actuate.health.Health.Builder;
|
|||
*/
|
||||
public abstract class AbstractHealthIndicator implements HealthIndicator {
|
||||
|
||||
private static final String NO_MESSAGE = null;
|
||||
|
||||
private static final String DEFAULT_MESSAGE = "Health check failed";
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final Function<Exception, String> healthCheckFailedMessage;
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractHealthIndicator} instance with a default
|
||||
* {@code healthCheckFailedMessage}.
|
||||
*/
|
||||
public AbstractHealthIndicator() {
|
||||
this(NO_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractHealthIndicator} instance with a specific message to
|
||||
* log when the health check fails.
|
||||
* @param healthCheckFailedMessage the message to log on health check failure
|
||||
* @since 2.0.0
|
||||
*/
|
||||
protected AbstractHealthIndicator(String healthCheckFailedMessage) {
|
||||
this.healthCheckFailedMessage = (ex) -> healthCheckFailedMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractHealthIndicator} instance with a specific message to
|
||||
* log when the health check fails.
|
||||
* @param healthCheckFailedMessage the message to log on health check failure
|
||||
* @since 2.0.0
|
||||
*/
|
||||
protected AbstractHealthIndicator(
|
||||
Function<Exception, String> healthCheckFailedMessage) {
|
||||
Assert.notNull(healthCheckFailedMessage,
|
||||
"HealthCheckFailedMessage must not be null");
|
||||
this.healthCheckFailedMessage = healthCheckFailedMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Health health() {
|
||||
Health.Builder builder = new Health.Builder();
|
||||
|
@ -43,7 +84,11 @@ public abstract class AbstractHealthIndicator implements HealthIndicator {
|
|||
doHealthCheck(builder);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
this.logger.warn("Health check failed", ex);
|
||||
if (this.logger.isWarnEnabled()) {
|
||||
String message = this.healthCheckFailedMessage.apply(ex);
|
||||
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE,
|
||||
ex);
|
||||
}
|
||||
builder.down(ex);
|
||||
}
|
||||
return builder.build();
|
||||
|
|
|
@ -25,6 +25,10 @@ package org.springframework.boot.actuate.health;
|
|||
*/
|
||||
public class ApplicationHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
public ApplicationHealthIndicator() {
|
||||
super("Application health check failed");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
builder.up();
|
||||
|
|
|
@ -35,6 +35,7 @@ public class InfluxDbHealthIndicator extends AbstractHealthIndicator {
|
|||
private final InfluxDB influxDb;
|
||||
|
||||
public InfluxDbHealthIndicator(InfluxDB influxDb) {
|
||||
super("InfluxDB health check failed");
|
||||
Assert.notNull(influxDb, "InfluxDB must not be null");
|
||||
this.influxDb = influxDb;
|
||||
}
|
||||
|
|
|
@ -64,6 +64,7 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator
|
|||
* Create a new {@link DataSourceHealthIndicator} instance.
|
||||
*/
|
||||
public DataSourceHealthIndicator() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,6 +83,7 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator
|
|||
* @param query the validation query to use (can be {@code null})
|
||||
*/
|
||||
public DataSourceHealthIndicator(DataSource dataSource, String query) {
|
||||
super("DataSource health check failed");
|
||||
this.dataSource = dataSource;
|
||||
this.query = query;
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
|
|
|
@ -34,6 +34,7 @@ public class JmsHealthIndicator extends AbstractHealthIndicator {
|
|||
private final ConnectionFactory connectionFactory;
|
||||
|
||||
public JmsHealthIndicator(ConnectionFactory connectionFactory) {
|
||||
super("JMS health check failed");
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@ public class LdapHealthIndicator extends AbstractHealthIndicator {
|
|||
private final LdapOperations ldapOperations;
|
||||
|
||||
public LdapHealthIndicator(LdapOperations ldapOperations) {
|
||||
super("LDAP health check failed");
|
||||
Assert.notNull(ldapOperations, "LdapOperations must not be null");
|
||||
this.ldapOperations = ldapOperations;
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ public class MailHealthIndicator extends AbstractHealthIndicator {
|
|||
private final JavaMailSenderImpl mailSender;
|
||||
|
||||
public MailHealthIndicator(JavaMailSenderImpl mailSender) {
|
||||
super("Mail health check failed");
|
||||
this.mailSender = mailSender;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ public class MongoHealthIndicator extends AbstractHealthIndicator {
|
|||
private final MongoTemplate mongoTemplate;
|
||||
|
||||
public MongoHealthIndicator(MongoTemplate mongoTemplate) {
|
||||
super("MongoDB health check failed");
|
||||
Assert.notNull(mongoTemplate, "MongoTemplate must not be null");
|
||||
this.mongoTemplate = mongoTemplate;
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ public class Neo4jHealthIndicator extends AbstractHealthIndicator {
|
|||
* @param sessionFactory the SessionFactory
|
||||
*/
|
||||
public Neo4jHealthIndicator(SessionFactory sessionFactory) {
|
||||
super("Neo4J health check failed");
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,7 @@ public class RedisHealthIndicator extends AbstractHealthIndicator {
|
|||
private final RedisConnectionFactory redisConnectionFactory;
|
||||
|
||||
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
|
||||
super("Reddis health check failed");
|
||||
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
|
||||
this.redisConnectionFactory = connectionFactory;
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ public class SolrHealthIndicator extends AbstractHealthIndicator {
|
|||
private final SolrClient solrClient;
|
||||
|
||||
public SolrHealthIndicator(SolrClient solrClient) {
|
||||
super("Solr health check failed");
|
||||
this.solrClient = solrClient;
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ public class DiskSpaceHealthIndicator extends AbstractHealthIndicator {
|
|||
* @param threshold the minimum disk space that should be available (in bytes)
|
||||
*/
|
||||
public DiskSpaceHealthIndicator(File path, long threshold) {
|
||||
super("DiskSpace health check failed");
|
||||
this.path = path;
|
||||
this.threshold = threshold;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue