mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Log failing calls to health indicators
Closes gh-22632 Co-authored-by: Madhura Bhave <bhavem@vmware.com>
This commit is contained in:
parent
e0465f7357
commit
6c8c8502e3
@ -82,15 +82,23 @@ public abstract class AbstractHealthIndicator implements HealthIndicator {
|
||||
doHealthCheck(builder);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (this.logger.isWarnEnabled()) {
|
||||
String message = this.healthCheckFailedMessage.apply(ex);
|
||||
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, ex);
|
||||
}
|
||||
builder.down(ex);
|
||||
}
|
||||
logExceptionIfPresent(builder);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private void logExceptionIfPresent(Builder builder) {
|
||||
Throwable ex = builder.getException();
|
||||
if (ex != null && this.logger.isWarnEnabled()) {
|
||||
String message = null;
|
||||
if (ex instanceof Exception) {
|
||||
message = this.healthCheckFailedMessage.apply((Exception) ex);
|
||||
}
|
||||
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actual health check logic.
|
||||
* @param builder the {@link Builder} to report health status and details
|
||||
|
@ -193,6 +193,8 @@ public final class Health extends HealthComponent {
|
||||
|
||||
private Map<String, Object> details;
|
||||
|
||||
private Throwable exception;
|
||||
|
||||
/**
|
||||
* Create new Builder instance.
|
||||
*/
|
||||
@ -231,6 +233,7 @@ public final class Health extends HealthComponent {
|
||||
*/
|
||||
public Builder withException(Throwable ex) {
|
||||
Assert.notNull(ex, "Exception must not be null");
|
||||
this.exception = ex;
|
||||
return withDetail("error", ex.getClass().getName() + ": " + ex.getMessage());
|
||||
}
|
||||
|
||||
@ -320,6 +323,15 @@ public final class Health extends HealthComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Exception}.
|
||||
* @return the exception or {@code null} if the builder has no exception
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public Throwable getException() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Health} instance with the previously specified code and
|
||||
* details.
|
||||
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2012-2020 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.health;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AbstractHealthIndicator}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
class AbstractHealthIndicatorTests {
|
||||
|
||||
@Test
|
||||
void healthCheckWhenUpDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.up();
|
||||
}
|
||||
}.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(output).doesNotContain("Test message");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionThrownDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
throw new IllegalStateException("Test exception");
|
||||
}
|
||||
}.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Test message").contains("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.down().withException(new IllegalStateException("Test exception"));
|
||||
}
|
||||
}.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Test message").contains("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessageTwice(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
IllegalStateException ex = new IllegalStateException("Test exception");
|
||||
builder.down().withException(ex);
|
||||
throw ex;
|
||||
}
|
||||
}.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Test message").containsOnlyOnce("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionAndNoFailureMessageLogsDefaultMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator() {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.down().withException(new IllegalStateException("Test exception"));
|
||||
}
|
||||
}.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Health check failed").contains("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithErrorLogsDefaultMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test Message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.down().withException(new Error("Test error"));
|
||||
}
|
||||
}.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Health check failed").contains("Test error");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user