This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new eee0a50f46e CAMEL-18630: camel-jbang - Add get health command eee0a50f46e is described below commit eee0a50f46e2dfc57c937b78158e6ee04f2f6932 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Oct 24 10:56:43 2022 +0200 CAMEL-18630: camel-jbang - Add get health command --- .../java/org/apache/camel/health/HealthCheck.java | 2 ++ .../camel/impl/health/AbstractHealthCheck.java | 26 ++++++++++++++++++++++ .../jbang/core/commands/process/ListHealth.java | 22 ++++++++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java index e02a4c402f2..caf2a85fbcb 100644 --- a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java +++ b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java @@ -38,10 +38,12 @@ public interface HealthCheck extends HasGroup, HasId, Ordered { @Deprecated String INVOCATION_ATTEMPT_TIME = "invocation.attempt.time"; String FAILURE_COUNT = "failure.count"; + String FAILURE_START_TIME = "failure.start.time"; String FAILURE_TIME = "failure.time"; String ENDPOINT_URI = "endpoint.uri"; String FAILURE_ERROR_COUNT = "failure.error.count"; String SUCCESS_COUNT = "success.count"; + String SUCCESS_START_TIME = "success.start.time"; String SUCCESS_TIME = "success.time"; String HTTP_RESPONSE_CODE = "http.response.code"; /** diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java index 305c0f4b45e..a5911776d0e 100644 --- a/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java +++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java @@ -157,8 +157,10 @@ public abstract class AbstractHealthCheck implements HealthCheck, CamelContextAw int invocationCount = (Integer) meta.getOrDefault(INVOCATION_COUNT, 0); int failureCount = (Integer) meta.getOrDefault(FAILURE_COUNT, 0); String failureTime = (String) meta.get(FAILURE_TIME); + String failureStartTime = (String) meta.get(FAILURE_START_TIME); int successCount = (Integer) meta.getOrDefault(SUCCESS_COUNT, 0); String successTime = (String) meta.get(SUCCESS_TIME); + String successStartTime = (String) meta.get(SUCCESS_START_TIME); String invocationTime = ZonedDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME); @@ -183,11 +185,16 @@ public abstract class AbstractHealthCheck implements HealthCheck, CamelContextAw if (builder.state() == State.DOWN) { // reset success since it failed successCount = 0; + successStartTime = null; failureCount++; failureTime = invocationTime; + if (failureStartTime == null) { + failureStartTime = invocationTime; + } } else if (builder.state() == State.UP) { // reset failure since it ok failureCount = 0; + failureStartTime = null; successCount++; if (successTime == null) { // first time we are OK, then reset failure as we only want to capture @@ -195,6 +202,9 @@ public abstract class AbstractHealthCheck implements HealthCheck, CamelContextAw failureTime = null; } successTime = invocationTime; + if (successStartTime == null) { + successStartTime = invocationTime; + } } meta.put(INVOCATION_TIME, invocationTime); @@ -205,12 +215,22 @@ public abstract class AbstractHealthCheck implements HealthCheck, CamelContextAw } else { meta.remove(FAILURE_TIME); } + if (failureStartTime != null) { + meta.put(FAILURE_START_TIME, failureStartTime); + } else { + meta.remove(FAILURE_START_TIME); + } meta.put(SUCCESS_COUNT, successCount); if (successTime != null) { meta.put(SUCCESS_TIME, successTime); } else { meta.remove(SUCCESS_TIME); } + if (successStartTime != null) { + meta.put(SUCCESS_START_TIME, successStartTime); + } else { + meta.remove(SUCCESS_START_TIME); + } // Copy some meta-data bits to the response attributes so the // response caches the health-check state at the time of the invocation. @@ -220,10 +240,16 @@ public abstract class AbstractHealthCheck implements HealthCheck, CamelContextAw if (meta.containsKey(FAILURE_TIME)) { builder.detail(FAILURE_TIME, meta.get(FAILURE_TIME)); } + if (meta.containsKey(FAILURE_START_TIME)) { + builder.detail(FAILURE_START_TIME, meta.get(FAILURE_START_TIME)); + } builder.detail(SUCCESS_COUNT, meta.get(SUCCESS_COUNT)); if (meta.containsKey(SUCCESS_TIME)) { builder.detail(SUCCESS_TIME, meta.get(SUCCESS_TIME)); } + if (meta.containsKey(SUCCESS_START_TIME)) { + builder.detail(SUCCESS_START_TIME, meta.get(SUCCESS_START_TIME)); + } return builder; } diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java index d5a99651668..922585aa12c 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java @@ -124,6 +124,14 @@ public class ListHealth extends ProcessBaseCommand { row.sinceSuccess = TimeUtils.printAge(delta); } } + time = d.getString("success.start.time"); + if (time != null) { + ZonedDateTime zdt = ZonedDateTime.parse(time); + if (zdt != null) { + long delta = Math.abs(ZonedDateTime.now().until(zdt, ChronoUnit.MILLIS)); + row.sinceStartSuccess = TimeUtils.printAge(delta); + } + } time = d.getString("failure.time"); if (time != null) { ZonedDateTime zdt = ZonedDateTime.parse(time); @@ -132,6 +140,14 @@ public class ListHealth extends ProcessBaseCommand { row.sinceFailure = TimeUtils.printAge(delta); } } + time = d.getString("failure.start.time"); + if (time != null) { + ZonedDateTime zdt = ZonedDateTime.parse(time); + if (zdt != null) { + long delta = Math.abs(ZonedDateTime.now().until(zdt, ChronoUnit.MILLIS)); + row.sinceStartFailure = TimeUtils.printAge(delta); + } + } } boolean add = true; @@ -237,8 +253,8 @@ public class ListHealth extends ProcessBaseCommand { protected String getSince(Row r) { String s1 = r.sinceLast != null ? r.sinceLast : "-"; - String s2 = r.sinceSuccess != null ? r.sinceSuccess : "-"; - String s3 = r.sinceFailure != null ? r.sinceFailure : "-"; + String s2 = r.sinceStartSuccess != null ? r.sinceStartSuccess : "-"; + String s3 = r.sinceStartFailure != null ? r.sinceStartFailure : "-"; return s1 + "/" + s2 + "/" + s3; } @@ -257,7 +273,9 @@ public class ListHealth extends ProcessBaseCommand { String failure; String sinceLast; String sinceSuccess; + String sinceStartSuccess; String sinceFailure; + String sinceStartFailure; String message; }