-
Notifications
You must be signed in to change notification settings - Fork 36
Improve exception handling in the failed health checks #676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xstefank
wants to merge
1
commit into
smallrye:main
Choose a base branch
from
xstefank:exception-log-669
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
implementation/src/main/java/io/smallrye/health/log/ExceptionErrorIdDetail.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.smallrye.health.log; | ||
|
|
||
| public enum ExceptionErrorIdDetail { | ||
| EXCEPTION_CLASS, | ||
| EXCEPTION_MESSAGE, | ||
| STACKTRACE | ||
| } |
8 changes: 8 additions & 0 deletions
8
implementation/src/main/java/io/smallrye/health/log/ExceptionLogType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package io.smallrye.health.log; | ||
|
|
||
| public enum ExceptionLogType { | ||
| NONE, | ||
| ERROR_ID, | ||
| EXCEPTION_CLASS, | ||
| EXCEPTION_MESSAGE | ||
| } |
180 changes: 180 additions & 0 deletions
180
implementation/src/test/java/io/smallrye/health/ErrorLogTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package io.smallrye.health; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.util.logging.Level; | ||
|
|
||
| import jakarta.json.JsonObject; | ||
|
|
||
| import org.eclipse.microprofile.health.HealthCheck; | ||
| import org.eclipse.microprofile.health.HealthCheckResponse; | ||
| import org.jboss.logging.Logger; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import io.smallrye.health.log.ExceptionErrorIdDetail; | ||
| import io.smallrye.health.log.ExceptionLogType; | ||
| import io.smallrye.testing.logging.LogCapture; | ||
|
|
||
| /** | ||
| * The new log API test. Will replace the log tests in {@link SmallRyeHealthReporterTest}. | ||
| */ | ||
| public class ErrorLogTest { | ||
|
|
||
| @RegisterExtension | ||
| static LogCapture logCapture = LogCapture.with(logRecord -> true, Level.ALL); | ||
|
|
||
| public static class FailingHealthCheck implements HealthCheck { | ||
| @Override | ||
| public HealthCheckResponse call() { | ||
| throw new RuntimeException("this health check has failed"); | ||
| } | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void createReporter() { | ||
| logCapture.records().clear(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionTypeNone() { | ||
| SmallRyeHealth health = reportHealthWithFailure(ExceptionLogType.NONE); | ||
|
|
||
| assertNull(health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\"," | ||
| + "\"checks\":[{\"name\":\"io.smallrye.health.ErrorLogTest$FailingHealthCheck\",\"status\":\"DOWN\"}]}"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionTypeErrorIdStacktrace() { | ||
| SmallRyeHealth health = reportHealthWithFailure(ExceptionLogType.ERROR_ID); | ||
|
|
||
| JsonObject data = health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"); | ||
| assertNotNull(data); | ||
| assertEquals(1, data.size()); | ||
| assertTrue(data.getString("<error>").contains("See error-id ")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\""); | ||
| assertAnyLogMessageMatches("Health check \"io\\.smallrye\\.health\\.ErrorLogTest\\$FailingHealthCheck\" failed " | ||
| + "\\(error-id [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\\) " | ||
| + "java\\.lang\\.RuntimeException: this health check has failed\\R" | ||
| + "\\s*at io\\.smallrye\\.health\\.ErrorLogTest\\$FailingHealthCheck\\.call[\\s\\S]*", Logger.Level.ERROR); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionTypeErrorIdExceptionClass() { | ||
| SmallRyeHealth health = reportHealthWithFailure(ExceptionLogType.ERROR_ID, ExceptionErrorIdDetail.EXCEPTION_CLASS); | ||
|
|
||
| JsonObject data = health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"); | ||
| assertNotNull(data); | ||
| assertEquals(1, data.size()); | ||
| assertTrue(data.getString("<error>").contains("See error-id ")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\""); | ||
| assertAnyLogMessageMatches("Health check \"io\\.smallrye\\.health\\.ErrorLogTest\\$FailingHealthCheck\" failed " + | ||
| "\\(error-id [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\\) " + | ||
| "java\\.lang\\.RuntimeException$", Logger.Level.ERROR); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionTypeErrorIdExceptionMessage() { | ||
| SmallRyeHealth health = reportHealthWithFailure(ExceptionLogType.ERROR_ID, ExceptionErrorIdDetail.EXCEPTION_MESSAGE); | ||
|
|
||
| JsonObject data = health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"); | ||
| assertNotNull(data); | ||
| assertEquals(1, data.size()); | ||
| assertTrue(data.getString("<error>").contains("See error-id ")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\""); | ||
| assertAnyLogMessageMatches("Health check \"io\\.smallrye\\.health\\.ErrorLogTest\\$FailingHealthCheck\" failed " + | ||
| "\\(error-id [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\\) " + | ||
| "java\\.lang\\.RuntimeException: this health check has failed$", Logger.Level.ERROR); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionTypeExceptionClass() { | ||
| SmallRyeHealth health = reportHealthWithFailure(ExceptionLogType.EXCEPTION_CLASS); | ||
|
|
||
| JsonObject data = health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"); | ||
| assertNotNull(data); | ||
| assertEquals(1, data.size()); | ||
| assertTrue(data.getString("<error>").contains("java.lang.RuntimeException")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\""); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionTypeExceptionMessage() { | ||
| SmallRyeHealth health = reportHealthWithFailure(ExceptionLogType.EXCEPTION_MESSAGE); | ||
|
|
||
| JsonObject data = health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"); | ||
| assertNotNull(data); | ||
| assertEquals(1, data.size()); | ||
| assertTrue(data.getString("<error>").contains("java.lang.RuntimeException: this health check has failed")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\""); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportWhenDownExceptionLogLevelWithErrorId() { | ||
| AsyncHealthCheckFactory asyncHealthCheckFactory = new AsyncHealthCheckFactory(); | ||
| asyncHealthCheckFactory.exceptionLogType = ExceptionLogType.ERROR_ID; | ||
| asyncHealthCheckFactory.exceptionLogLevel = Logger.Level.DEBUG; | ||
| asyncHealthCheckFactory.removeDeprecatedExceptionDataStyle = true; | ||
| SmallRyeHealthReporter reporter = new SmallRyeHealthReporter(); | ||
| reporter.asyncHealthCheckFactory = asyncHealthCheckFactory; | ||
| reporter.addHealthCheck(new FailingHealthCheck()); | ||
| SmallRyeHealth health = reporter.getHealth(); | ||
| reporter.reportHealth(new ByteArrayOutputStream(), health); | ||
|
|
||
| JsonObject data = health.getPayload().getJsonArray("checks").getJsonObject(0).getJsonObject("data"); | ||
| assertNotNull(data); | ||
| assertEquals(1, data.size()); | ||
| assertTrue(data.getString("<error>").contains("See error-id ")); | ||
| assertAnyLogMessageContains("SRHCK01001: Reporting health down status: {\"status\":\"DOWN\""); | ||
| assertAnyLogMessageMatches("Health check \"io\\.smallrye\\.health\\.ErrorLogTest\\$FailingHealthCheck\" failed " | ||
| + "\\(error-id [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\\) " | ||
| + "java\\.lang\\.RuntimeException: this health check has failed\\R" | ||
| + "\\s*at io\\.smallrye\\.health\\.ErrorLogTest\\$FailingHealthCheck\\.call[\\s\\S]*", Logger.Level.DEBUG); | ||
| } | ||
|
|
||
| private SmallRyeHealth reportHealthWithFailure(ExceptionLogType type) { | ||
| return reportHealthWithFailure(type, ExceptionErrorIdDetail.STACKTRACE); | ||
| } | ||
|
|
||
| private SmallRyeHealth reportHealthWithFailure(ExceptionLogType exceptionLogType, | ||
| ExceptionErrorIdDetail exceptionErrorIdDetail) { | ||
| SmallRyeHealthReporter reporter = createReporter(exceptionLogType, exceptionErrorIdDetail); | ||
| reporter.addHealthCheck(new FailingHealthCheck()); | ||
| SmallRyeHealth health = reporter.getHealth(); | ||
| reporter.reportHealth(new ByteArrayOutputStream(), health); | ||
| return health; | ||
| } | ||
|
|
||
| private static SmallRyeHealthReporter createReporter(ExceptionLogType exceptionLogType, | ||
| ExceptionErrorIdDetail exceptionErrorIdDetail) { | ||
| AsyncHealthCheckFactory asyncHealthCheckFactory = new AsyncHealthCheckFactory(); | ||
| asyncHealthCheckFactory.removeDeprecatedExceptionDataStyle = true; | ||
| asyncHealthCheckFactory.exceptionLogType = exceptionLogType; | ||
| asyncHealthCheckFactory.exceptionErrorIdDetail = exceptionErrorIdDetail; | ||
| SmallRyeHealthReporter reporter = new SmallRyeHealthReporter(); | ||
| reporter.asyncHealthCheckFactory = asyncHealthCheckFactory; | ||
| return reporter; | ||
| } | ||
|
|
||
| private void assertAnyLogMessageContains(String expected) { | ||
| assertFalse(logCapture.records().isEmpty(), "Expected that the log is not empty"); | ||
| assertTrue(logCapture.records().stream().anyMatch(logRecord -> logRecord.getMessage().contains(expected)), | ||
| "Log doesn't contain requested message: " + expected); | ||
| } | ||
|
|
||
| private void assertAnyLogMessageMatches(String regex, Logger.Level level) { | ||
| assertFalse(logCapture.records().isEmpty(), "Expected that the log is not empty"); | ||
| assertTrue( | ||
| logCapture.records().stream() | ||
| .anyMatch(logRecord -> logRecord.getMessage().matches(regex) | ||
| && (level == null || level.equals(Logger.Level.valueOf(level.toString())))), | ||
| "Log doesn't contain requested message matching: " + regex); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this deserve a piece of documentation? I don't think
io.smallrye.health.exception-log-typeandio.smallrye.health.exception.errorid.detailproperties are documented? How would the user know how to use them?