pattern: Implement Health Check for Microservices Observability (#2695) (#2774)

* Add Health Check pattern implementation

The commit introduces  Health Check pattern, providing a series of health indicators for system performance and stability monitoring, including checks for system CPU load, process CPU load, database health, memory usage, and garbage collection metrics. It also includes asynchronous execution and caching mechanisms for health checks, and retry configurations for resilience.

Implements health checking components as per issue #2695.

* Test cases and javadoc for HealthEndpointIntegrationTest

* Added more log to test case to see why it returns 503

* Change config values to see if the system High system CPU load is resolved or not in CI.

* Fixes for test cases.

* some fixes for Sonar.

* some fixes for Sonar.
ADDED HIGH_PROCESS_CPU_LOAD_MESSAGE_WITHOUT_PARAM
ADDED HIGH_SYSTEM_CPU_LOAD_MESSAGE_WITHOUT_PARAM

* Sonar fixes address "Define and throw a dedicated exception instead of using a generic one."

added HealthCheckInterruptedException
refactored CustomHealthIndicator

* fixes checkstyle violation.
This commit is contained in:
Doksanbir
2023-12-02 15:17:01 +03:00
committed by GitHub
parent 83dba617c5
commit 21f7b026f5
27 changed files with 2382 additions and 0 deletions
@@ -0,0 +1,54 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.health.check.RetryConfig;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.retry.support.RetryTemplate;
/**
* Unit tests for the {@link RetryConfig} class.
*
* @author ydoksanbir
*/
@SpringBootTest(classes = RetryConfig.class)
class RetryConfigTest {
/** Injected RetryTemplate instance. */
@Autowired private RetryTemplate retryTemplate;
/**
* Tests that the retry template retries three times with a two-second delay.
*
* <p>Verifies that the retryable operation is executed three times before throwing an exception,
* and that the total elapsed time for the retries is at least four seconds.
*/
@Test
void shouldRetryThreeTimesWithTwoSecondDelay() {
AtomicInteger attempts = new AtomicInteger();
Runnable retryableOperation =
() -> {
attempts.incrementAndGet();
throw new RuntimeException("Test exception for retry");
};
long startTime = System.currentTimeMillis();
try {
retryTemplate.execute(
context -> {
retryableOperation.run();
return null;
});
} catch (Exception e) {
// Expected exception
}
long endTime = System.currentTimeMillis();
assertEquals(3, attempts.get(), "Should have retried three times");
assertTrue(
(endTime - startTime) >= 4000,
"Should have waited at least 4 seconds in total for backoff");
}
}