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;
|
private final RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
public RabbitHealthIndicator(RabbitTemplate rabbitTemplate) {
|
public RabbitHealthIndicator(RabbitTemplate rabbitTemplate) {
|
||||||
|
super("Rabbit health check failed");
|
||||||
Assert.notNull(rabbitTemplate, "RabbitTemplate must not be null");
|
Assert.notNull(rabbitTemplate, "RabbitTemplate must not be null");
|
||||||
this.rabbitTemplate = rabbitTemplate;
|
this.rabbitTemplate = rabbitTemplate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,10 @@ public class CassandraHealthIndicator extends AbstractHealthIndicator {
|
||||||
|
|
||||||
private CassandraOperations cassandraOperations;
|
private CassandraOperations cassandraOperations;
|
||||||
|
|
||||||
|
public CassandraHealthIndicator() {
|
||||||
|
super("Cassandra health check failed");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link CassandraHealthIndicator} instance.
|
* Create a new {@link CassandraHealthIndicator} instance.
|
||||||
* @param cassandraOperations the Cassandra operations
|
* @param cassandraOperations the Cassandra operations
|
||||||
|
|
|
@ -37,6 +37,10 @@ public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
|
||||||
|
|
||||||
private CouchbaseOperations operations;
|
private CouchbaseOperations operations;
|
||||||
|
|
||||||
|
public CouchbaseHealthIndicator() {
|
||||||
|
super("Couchbase health check failed");
|
||||||
|
}
|
||||||
|
|
||||||
public CouchbaseHealthIndicator(CouchbaseOperations couchbaseOperations) {
|
public CouchbaseHealthIndicator(CouchbaseOperations couchbaseOperations) {
|
||||||
Assert.notNull(couchbaseOperations, "CouchbaseOperations must not be null");
|
Assert.notNull(couchbaseOperations, "CouchbaseOperations must not be null");
|
||||||
this.operations = couchbaseOperations;
|
this.operations = couchbaseOperations;
|
||||||
|
|
|
@ -65,6 +65,7 @@ public class ElasticsearchHealthIndicator extends AbstractHealthIndicator {
|
||||||
*/
|
*/
|
||||||
public ElasticsearchHealthIndicator(Client client, long responseTimeout,
|
public ElasticsearchHealthIndicator(Client client, long responseTimeout,
|
||||||
String... indices) {
|
String... indices) {
|
||||||
|
super("Elasticsearch health check failed");
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.responseTimeout = responseTimeout;
|
this.responseTimeout = responseTimeout;
|
||||||
this.indices = indices;
|
this.indices = indices;
|
||||||
|
|
|
@ -40,6 +40,7 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final JsonParser jsonParser = new JsonParser();
|
private final JsonParser jsonParser = new JsonParser();
|
||||||
|
|
||||||
public ElasticsearchJestHealthIndicator(JestClient jestClient) {
|
public ElasticsearchJestHealthIndicator(JestClient jestClient) {
|
||||||
|
super("Elasticsearch health check failed");
|
||||||
this.jestClient = jestClient;
|
this.jestClient = jestClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.health;
|
package org.springframework.boot.actuate.health;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.boot.actuate.health.Health.Builder;
|
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
|
* 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 {
|
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 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
|
@Override
|
||||||
public final Health health() {
|
public final Health health() {
|
||||||
Health.Builder builder = new Health.Builder();
|
Health.Builder builder = new Health.Builder();
|
||||||
|
@ -43,7 +84,11 @@ public abstract class AbstractHealthIndicator implements HealthIndicator {
|
||||||
doHealthCheck(builder);
|
doHealthCheck(builder);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
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);
|
builder.down(ex);
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.build();
|
||||||
|
|
|
@ -25,6 +25,10 @@ package org.springframework.boot.actuate.health;
|
||||||
*/
|
*/
|
||||||
public class ApplicationHealthIndicator extends AbstractHealthIndicator {
|
public class ApplicationHealthIndicator extends AbstractHealthIndicator {
|
||||||
|
|
||||||
|
public ApplicationHealthIndicator() {
|
||||||
|
super("Application health check failed");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||||
builder.up();
|
builder.up();
|
||||||
|
|
|
@ -35,6 +35,7 @@ public class InfluxDbHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final InfluxDB influxDb;
|
private final InfluxDB influxDb;
|
||||||
|
|
||||||
public InfluxDbHealthIndicator(InfluxDB influxDb) {
|
public InfluxDbHealthIndicator(InfluxDB influxDb) {
|
||||||
|
super("InfluxDB health check failed");
|
||||||
Assert.notNull(influxDb, "InfluxDB must not be null");
|
Assert.notNull(influxDb, "InfluxDB must not be null");
|
||||||
this.influxDb = influxDb;
|
this.influxDb = influxDb;
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator
|
||||||
* Create a new {@link DataSourceHealthIndicator} instance.
|
* Create a new {@link DataSourceHealthIndicator} instance.
|
||||||
*/
|
*/
|
||||||
public DataSourceHealthIndicator() {
|
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})
|
* @param query the validation query to use (can be {@code null})
|
||||||
*/
|
*/
|
||||||
public DataSourceHealthIndicator(DataSource dataSource, String query) {
|
public DataSourceHealthIndicator(DataSource dataSource, String query) {
|
||||||
|
super("DataSource health check failed");
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
|
|
@ -34,6 +34,7 @@ public class JmsHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final ConnectionFactory connectionFactory;
|
private final ConnectionFactory connectionFactory;
|
||||||
|
|
||||||
public JmsHealthIndicator(ConnectionFactory connectionFactory) {
|
public JmsHealthIndicator(ConnectionFactory connectionFactory) {
|
||||||
|
super("JMS health check failed");
|
||||||
this.connectionFactory = connectionFactory;
|
this.connectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ public class LdapHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final LdapOperations ldapOperations;
|
private final LdapOperations ldapOperations;
|
||||||
|
|
||||||
public LdapHealthIndicator(LdapOperations ldapOperations) {
|
public LdapHealthIndicator(LdapOperations ldapOperations) {
|
||||||
|
super("LDAP health check failed");
|
||||||
Assert.notNull(ldapOperations, "LdapOperations must not be null");
|
Assert.notNull(ldapOperations, "LdapOperations must not be null");
|
||||||
this.ldapOperations = ldapOperations;
|
this.ldapOperations = ldapOperations;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ public class MailHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final JavaMailSenderImpl mailSender;
|
private final JavaMailSenderImpl mailSender;
|
||||||
|
|
||||||
public MailHealthIndicator(JavaMailSenderImpl mailSender) {
|
public MailHealthIndicator(JavaMailSenderImpl mailSender) {
|
||||||
|
super("Mail health check failed");
|
||||||
this.mailSender = mailSender;
|
this.mailSender = mailSender;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ public class MongoHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final MongoTemplate mongoTemplate;
|
private final MongoTemplate mongoTemplate;
|
||||||
|
|
||||||
public MongoHealthIndicator(MongoTemplate mongoTemplate) {
|
public MongoHealthIndicator(MongoTemplate mongoTemplate) {
|
||||||
|
super("MongoDB health check failed");
|
||||||
Assert.notNull(mongoTemplate, "MongoTemplate must not be null");
|
Assert.notNull(mongoTemplate, "MongoTemplate must not be null");
|
||||||
this.mongoTemplate = mongoTemplate;
|
this.mongoTemplate = mongoTemplate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@ public class Neo4jHealthIndicator extends AbstractHealthIndicator {
|
||||||
* @param sessionFactory the SessionFactory
|
* @param sessionFactory the SessionFactory
|
||||||
*/
|
*/
|
||||||
public Neo4jHealthIndicator(SessionFactory sessionFactory) {
|
public Neo4jHealthIndicator(SessionFactory sessionFactory) {
|
||||||
|
super("Neo4J health check failed");
|
||||||
this.sessionFactory = sessionFactory;
|
this.sessionFactory = sessionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,7 @@ public class RedisHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final RedisConnectionFactory redisConnectionFactory;
|
private final RedisConnectionFactory redisConnectionFactory;
|
||||||
|
|
||||||
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
|
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
|
||||||
|
super("Reddis health check failed");
|
||||||
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
|
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
|
||||||
this.redisConnectionFactory = connectionFactory;
|
this.redisConnectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ public class SolrHealthIndicator extends AbstractHealthIndicator {
|
||||||
private final SolrClient solrClient;
|
private final SolrClient solrClient;
|
||||||
|
|
||||||
public SolrHealthIndicator(SolrClient solrClient) {
|
public SolrHealthIndicator(SolrClient solrClient) {
|
||||||
|
super("Solr health check failed");
|
||||||
this.solrClient = solrClient;
|
this.solrClient = solrClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,7 @@ public class DiskSpaceHealthIndicator extends AbstractHealthIndicator {
|
||||||
* @param threshold the minimum disk space that should be available (in bytes)
|
* @param threshold the minimum disk space that should be available (in bytes)
|
||||||
*/
|
*/
|
||||||
public DiskSpaceHealthIndicator(File path, long threshold) {
|
public DiskSpaceHealthIndicator(File path, long threshold) {
|
||||||
|
super("DiskSpace health check failed");
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.threshold = threshold;
|
this.threshold = threshold;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue