You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/10/26 13:16:17 UTC

[camel] branch main updated: CAMEL-18650: camel-micrometer - Add dev console. Reuse created MetricRegistry if none exists.

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 226cb42e2b1 CAMEL-18650: camel-micrometer - Add dev console. Reuse created MetricRegistry if none exists.
226cb42e2b1 is described below

commit 226cb42e2b1785749aee46b144c01189e42088c1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 26 15:15:59 2022 +0200

    CAMEL-18650: camel-micrometer - Add dev console. Reuse created MetricRegistry if none exists.
---
 .../camel/component/micrometer/micrometer.json     |  2 +-
 .../component/micrometer/MicrometerConsole.java    | 67 ++++++++++++++++++----
 .../component/micrometer/MicrometerEndpoint.java   |  2 +-
 3 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/components/camel-micrometer/src/generated/resources/org/apache/camel/component/micrometer/micrometer.json b/components/camel-micrometer/src/generated/resources/org/apache/camel/component/micrometer/micrometer.json
index d1300e3cdc2..55e053463c3 100644
--- a/components/camel-micrometer/src/generated/resources/org/apache/camel/component/micrometer/micrometer.json
+++ b/components/camel-micrometer/src/generated/resources/org/apache/camel/component/micrometer/micrometer.json
@@ -38,7 +38,7 @@
     "metricsType": { "kind": "path", "displayName": "Metrics Type", "group": "producer", "label": "", "required": true, "type": "object", "javaType": "io.micrometer.core.instrument.Meter.Type", "enum": [ "counter", "distribution_summary", "timer" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Type of metrics" },
     "metricsName": { "kind": "path", "displayName": "Metrics Name", "group": "producer", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Name of metrics" },
     "tags": { "kind": "path", "displayName": "Tags", "group": "producer", "label": "", "required": false, "type": "object", "javaType": "java.lang.Iterable<io.micrometer.core.instrument.Tag>", "deprecated": false, "autowired": false, "secret": false, "description": "Tags of metrics" },
-    "action": { "kind": "parameter", "displayName": "Action", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Action expression when using timer type" },
+    "action": { "kind": "parameter", "displayName": "Action", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "enum": [ "start", "stop" ], "deprecated": false, "autowired": false, "secret": false, "description": "Action expression when using timer type" },
     "decrement": { "kind": "parameter", "displayName": "Decrement", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Decrement value expression when using counter type" },
     "increment": { "kind": "parameter", "displayName": "Increment", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Increment value expression when using counter type" },
     "value": { "kind": "parameter", "displayName": "Value", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Value expression when using histogram type" },
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerConsole.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerConsole.java
index 021b7073c74..ad20e5a530b 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerConsole.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerConsole.java
@@ -27,6 +27,7 @@ import io.micrometer.core.instrument.Gauge;
 import io.micrometer.core.instrument.LongTaskTimer;
 import io.micrometer.core.instrument.Meter;
 import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.Timer;
 import org.apache.camel.impl.console.AbstractDevConsole;
 import org.apache.camel.spi.annotations.DevConsole;
 import org.apache.camel.util.json.JsonObject;
@@ -52,7 +53,11 @@ public class MicrometerConsole extends AbstractDevConsole {
                 }
                 i++;
                 String name = c.getId().getName();
-                double cnt = c.count();
+                String cnt = String.valueOf(c.count());
+                // strip decimal if counter is integer based
+                if (cnt.endsWith(".0") || cnt.endsWith(",0")) {
+                    cnt = cnt.substring(0, cnt.length() - 2);
+                }
                 sb.append(String.format("    %s: %s\n", name, cnt));
             }
         }
@@ -70,6 +75,22 @@ public class MicrometerConsole extends AbstractDevConsole {
             }
         }
         i = 0;
+        for (Meter m : mr.getMeters()) {
+            if (m instanceof Timer) {
+                Timer t = (Timer) m;
+                if (i == 0) {
+                    sb.append("\nTimer:\n");
+                }
+                i++;
+                String name = t.getId().getName();
+                long count = t.count();
+                long mean = Math.round(t.mean(TimeUnit.MILLISECONDS));
+                long max = Math.round(t.max(TimeUnit.MILLISECONDS));
+                long total = Math.round(t.totalTime(TimeUnit.MILLISECONDS));
+                sb.append(String.format("    %s: %d (total: %dms mean: %dms max: %dms)\n", name, count, total, mean, max));
+            }
+        }
+        i = 0;
         for (Meter m : mr.getMeters()) {
             if (m instanceof LongTaskTimer) {
                 LongTaskTimer t = (LongTaskTimer) m;
@@ -79,10 +100,10 @@ public class MicrometerConsole extends AbstractDevConsole {
                 i++;
                 String name = t.getId().getName();
                 int tasks = t.activeTasks();
-                double mean = t.mean(TimeUnit.MILLISECONDS);
-                double max = t.max(TimeUnit.MILLISECONDS);
-                double duration = t.duration(TimeUnit.MILLISECONDS);
-                sb.append(String.format("    %s: %d (duration: %f mean: %f max: %f)\n", name, tasks, duration, mean, max));
+                long mean = Math.round(t.mean(TimeUnit.MILLISECONDS));
+                long max = Math.round(t.max(TimeUnit.MILLISECONDS));
+                long duration = Math.round(t.duration(TimeUnit.MILLISECONDS));
+                sb.append(String.format("    %s: %d (duration: %dms mean: %dms max: %dms)\n", name, tasks, duration, mean, max));
             }
         }
         i = 0;
@@ -121,7 +142,15 @@ public class MicrometerConsole extends AbstractDevConsole {
                 i++;
                 JsonObject jo = new JsonObject();
                 jo.put("name", c.getId().getName());
-                jo.put("count", c.count());
+                // strip decimal if counter is integer based
+                String cnt = String.valueOf(c.count());
+                if (cnt.endsWith(".0") || cnt.endsWith(",0")) {
+                    cnt = cnt.substring(0, cnt.length() - 2);
+                    jo.put("count", Long.valueOf(cnt));
+                } else {
+                    // it has decimals so store as-is
+                    jo.put("count", c.count());
+                }
                 list.add(jo);
             }
         }
@@ -141,20 +170,38 @@ public class MicrometerConsole extends AbstractDevConsole {
             }
         }
         i = 0;
+        for (Meter m : mr.getMeters()) {
+            if (m instanceof Timer) {
+                final List<JsonObject> list = new ArrayList<>();
+                Timer t = (Timer) m;
+                if (i == 0) {
+                    root.put("timers", list);
+                }
+                i++;
+                JsonObject jo = new JsonObject();
+                jo.put("name", t.getId().getName());
+                jo.put("count", t.count());
+                jo.put("mean", Math.round(t.mean(TimeUnit.MILLISECONDS)));
+                jo.put("max", Math.round(t.max(TimeUnit.MILLISECONDS)));
+                jo.put("total", Math.round(t.totalTime(TimeUnit.MILLISECONDS)));
+                list.add(jo);
+            }
+        }
+        i = 0;
         for (Meter m : mr.getMeters()) {
             if (m instanceof LongTaskTimer) {
                 final List<JsonObject> list = new ArrayList<>();
                 LongTaskTimer t = (LongTaskTimer) m;
                 if (i == 0) {
-                    root.put("timers", list);
+                    root.put("longTaskTimers", list);
                 }
                 i++;
                 JsonObject jo = new JsonObject();
                 jo.put("name", t.getId().getName());
                 jo.put("activeTasks", t.activeTasks());
-                jo.put("mean", t.mean(TimeUnit.MILLISECONDS));
-                jo.put("max", t.max(TimeUnit.MILLISECONDS));
-                jo.put("duration", t.duration(TimeUnit.MILLISECONDS));
+                jo.put("mean", Math.round(t.mean(TimeUnit.MILLISECONDS)));
+                jo.put("max", Math.round(t.max(TimeUnit.MILLISECONDS)));
+                jo.put("duration", Math.round(t.duration(TimeUnit.MILLISECONDS)));
                 list.add(jo);
             }
         }
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerEndpoint.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerEndpoint.java
index a2ca49af90f..1086d7166e6 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerEndpoint.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerEndpoint.java
@@ -49,7 +49,7 @@ public class MicrometerEndpoint extends DefaultEndpoint {
     protected final String metricsName;
     @UriPath(description = "Tags of metrics")
     protected final Iterable<Tag> tags;
-    @UriParam(description = "Action expression when using timer type")
+    @UriParam(description = "Action expression when using timer type", enums = "start,stop")
     private String action;
     @UriParam(description = "Value expression when using histogram type")
     private String value;