From 6c8c8502e3098f50ec3f21be0d79aad756e012e8 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 11 Sep 2020 15:34:47 +0200 Subject: [PATCH] Log failing calls to health indicators Closes gh-22632 Co-authored-by: Madhura Bhave --- .../health/AbstractHealthIndicator.java | 16 ++- .../boot/actuate/health/Health.java | 12 ++ .../health/AbstractHealthIndicatorTests.java | 111 ++++++++++++++++++ 3 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/AbstractHealthIndicatorTests.java diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java index 31492ccec5..2a9dee87a4 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java @@ -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 diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java index 438bec08c6..c95ef8e324 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java @@ -193,6 +193,8 @@ public final class Health extends HealthComponent { private Map 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. diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/AbstractHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/AbstractHealthIndicatorTests.java new file mode 100644 index 0000000000..4aae7019ef --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/AbstractHealthIndicatorTests.java @@ -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"); + } + +}