From 230aa902785b136dfb615cbd236dc038f9bfa32f Mon Sep 17 00:00:00 2001
From: litetex <80211953+alb2k@users.noreply.github.com>
Date: Sat, 20 Mar 2021 18:01:28 +0100
Subject: [PATCH 1/3] #7 Added healthchecks
---
README.md | 1 +
pom.xml | 9 +++-
.../system/health/LivenessHealthCheck.java | 19 ++++++++
.../system/health/ReadinessHealthCheck.java | 42 ++++++++++++++++
.../microstream/storage/DBManager.java | 48 +++++++++++++++++++
5 files changed, 118 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/hackathon/microstream/service/system/health/LivenessHealthCheck.java
create mode 100644 src/main/java/hackathon/microstream/service/system/health/ReadinessHealthCheck.java
diff --git a/README.md b/README.md
index 15bb3c9..68ffa4a 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@ This project was created for the [Microstream hackathon](https://hackathon.micro
* [Microstream](https://microstream.one/platforms/microstream-for-java/)
* [Microprofile (config)](https://github.com/eclipse/microprofile-config)
* [Helidon MP](https://helidon.io/#getting-started)
+* [MP Health](https://github.com/eclipse/microprofile-health)
* Logging via [SLF4J](http://www.slf4j.org/) and [Apache Log4j 2](https://logging.apache.org/log4)
* [OpenApi](https://www.openapis.org/)
* [OpenApi-UI](https://swagger.io/tools/swagger-ui/)
diff --git a/pom.xml b/pom.xml
index 157f7a4..10598b7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,17 +56,24 @@
io.helidon.microprofile.bundles
helidon-microprofile-core
-
+
org.slf4j
slf4j-jdk14
+
io.helidon.microprofile.openapi
helidon-microprofile-openapi
+
+
+ io.helidon.microprofile.health
+ helidon-microprofile-health
+
+
io.helidon.logging
helidon-logging-slf4j
diff --git a/src/main/java/hackathon/microstream/service/system/health/LivenessHealthCheck.java b/src/main/java/hackathon/microstream/service/system/health/LivenessHealthCheck.java
new file mode 100644
index 0000000..c995cab
--- /dev/null
+++ b/src/main/java/hackathon/microstream/service/system/health/LivenessHealthCheck.java
@@ -0,0 +1,19 @@
+package hackathon.microstream.service.system.health;
+
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.eclipse.microprofile.health.Liveness;
+
+import javax.enterprise.context.ApplicationScoped;
+
+/**
+ * Checks if the app is alive
+ */
+@Liveness
+@ApplicationScoped
+public class LivenessHealthCheck implements HealthCheck {
+ @Override
+ public HealthCheckResponse call() {
+ return HealthCheckResponse.up("liveness");
+ }
+}
diff --git a/src/main/java/hackathon/microstream/service/system/health/ReadinessHealthCheck.java b/src/main/java/hackathon/microstream/service/system/health/ReadinessHealthCheck.java
new file mode 100644
index 0000000..8831ac2
--- /dev/null
+++ b/src/main/java/hackathon/microstream/service/system/health/ReadinessHealthCheck.java
@@ -0,0 +1,42 @@
+package hackathon.microstream.service.system.health;
+
+import hackathon.microstream.storage.DBManager;
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.eclipse.microprofile.health.Readiness;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.enterprise.context.ApplicationScoped;
+
+/**
+ * Checks if the app is ready (for traffic)
+ */
+@Readiness
+@ApplicationScoped
+public class ReadinessHealthCheck implements HealthCheck {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ReadinessHealthCheck.class);
+
+ @Override
+ public HealthCheckResponse call() {
+
+ boolean up = DBManager.getInstance().isReady();
+
+ var builder = HealthCheckResponse.builder()
+ .name("storage")
+ .withData("demoMode", DBManager.getInstance().isDemoMode())
+ .state(up);
+
+ if(up) {
+ var storageManagerDetails = DBManager.getInstance().getStorageManagerDetails();
+ builder.withData("storageManager.name", storageManagerDetails.getName());
+ builder.withData("storageManager.active", storageManagerDetails.isActive());
+ builder.withData("storageManager.initializationTime", storageManagerDetails.getInitializationTime());
+ builder.withData("storageManager.initializationDuration", storageManagerDetails.getInitializationDuration());
+ builder.withData("storageManager.operationModeTime", storageManagerDetails.getOperationModeTime());
+ }
+
+ return builder.build();
+ }
+}
diff --git a/src/main/java/hackathon/microstream/storage/DBManager.java b/src/main/java/hackathon/microstream/storage/DBManager.java
index 61f0c60..1dc0993 100644
--- a/src/main/java/hackathon/microstream/storage/DBManager.java
+++ b/src/main/java/hackathon/microstream/storage/DBManager.java
@@ -126,4 +126,52 @@ public void save(Iterable