This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch ra
in repository https://gitbox.apache.org/repos/asf/camel.git

commit e65fef342c36071a37bc450661f83c4780abce8d
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon May 27 14:09:25 2024 +0200

    CAMEL-20798: Add RemoteAddress to endpoints so they can tell the 
url/hostname etc for the system it connects. This is needed for better 
monitoring, tracing and management. Add this information into camel-tracer as 
tags.
---
 .../org/apache/camel/dev-console/service.json      | 15 ++++
 .../apache/camel/dev-console/{protocol => service} |  2 +-
 .../org/apache/camel/dev-consoles.properties       |  2 +-
 ...tocolDevConsole.java => ServiceDevConsole.java} | 92 +++++++++++++++++++---
 4 files changed, 99 insertions(+), 12 deletions(-)

diff --git 
a/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/service.json
 
b/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/service.json
new file mode 100644
index 00000000000..9d130a8ba71
--- /dev/null
+++ 
b/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/service.json
@@ -0,0 +1,15 @@
+{
+  "console": {
+    "kind": "console",
+    "group": "camel",
+    "name": "service",
+    "title": "Services",
+    "description": "Services used for network communication with clients",
+    "deprecated": false,
+    "javaType": "org.apache.camel.impl.console.ServiceDevConsole",
+    "groupId": "org.apache.camel",
+    "artifactId": "camel-console",
+    "version": "4.7.0-SNAPSHOT"
+  }
+}
+
diff --git 
a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/protocol
 
b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/service
similarity index 51%
rename from 
core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/protocol
rename to 
core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/service
index 0ba65348685..42a2d8a9592 100644
--- 
a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/protocol
+++ 
b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/service
@@ -1,2 +1,2 @@
 # Generated by camel build tools - do NOT edit this file!
-class=org.apache.camel.impl.console.ProtocolDevConsole
+class=org.apache.camel.impl.console.ServiceDevConsole
diff --git 
a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
 
b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
index 923478dde7d..8462c8e6fdd 100644
--- 
a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
+++ 
b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
@@ -1,5 +1,5 @@
 # Generated by camel build tools - do NOT edit this file!
-dev-consoles=bean blocked circuit-breaker consumer context debug endpoint 
event gc health inflight java-security jvm log memory properties protocol 
reload route route-controller route-dump source startup-recorder thread top 
trace transformers type-converters variables
+dev-consoles=bean blocked circuit-breaker consumer context debug endpoint 
event gc health inflight java-security jvm log memory properties reload route 
route-controller route-dump service source startup-recorder thread top trace 
transformers type-converters variables
 groupId=org.apache.camel
 artifactId=camel-console
 version=4.7.0-SNAPSHOT
diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ProtocolDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ServiceDevConsole.java
similarity index 54%
rename from 
core/camel-console/src/main/java/org/apache/camel/impl/console/ProtocolDevConsole.java
rename to 
core/camel-console/src/main/java/org/apache/camel/impl/console/ServiceDevConsole.java
index 04f5732c617..fa990f17114 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ProtocolDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ServiceDevConsole.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.impl.console;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -29,12 +30,13 @@ import org.apache.camel.spi.RuntimeEndpointRegistry;
 import org.apache.camel.spi.annotations.DevConsole;
 import org.apache.camel.support.DefaultConsumer;
 import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.json.JsonObject;
 
-@DevConsole(name = "protocol", displayName = "Protocols", description = 
"Protocols used for network communication with clients")
-public class ProtocolDevConsole extends AbstractDevConsole {
+@DevConsole(name = "service", displayName = "Services", description = 
"Services used for network communication with clients")
+public class ServiceDevConsole extends AbstractDevConsole {
 
-    public ProtocolDevConsole() {
-        super("camel", "protocol", "Protocols", "Protocols used for network 
communication with clients");
+    public ServiceDevConsole() {
+        super("camel", "service", "Services", "Services used for network 
communication with clients");
     }
 
     @Override
@@ -82,8 +84,9 @@ public class ProtocolDevConsole extends AbstractDevConsole {
                     boolean skip
                             = "platform-http".equals(component) || 
stat.isPresent() && "in".equals(stat.get().getDirection());
                     if (!skip) {
+                        stat = findStats(stats, endpoint.getEndpointUri(), 
"out");
                         var uri = endpoint.toString();
-                        printLine(sb, component, stat, "in", hosted, protocol, 
adr, uri);
+                        printLine(sb, component, stat, "out", hosted, 
protocol, adr, uri);
                     }
                 }
             }
@@ -109,17 +112,86 @@ public class ProtocolDevConsole extends 
AbstractDevConsole {
         }
         sb.append(String.format("\n    Component: %s", component));
         sb.append(String.format("\n    Direction: %s", dir));
-        sb.append(String.format("\n    Hosted Service: %b", hosted));
-        sb.append(String.format("\n    Usage: %d", total));
+        sb.append(String.format("\n    Hosted: %b", hosted));
         sb.append(String.format("\n    Protocol: %s", protocol));
-        sb.append(String.format("\n    Service: %s", adr));
+        sb.append(String.format("\n    Address: %s", adr));
         sb.append(String.format("\n    Endpoint Uri: %s", uri));
+        sb.append(String.format("\n    Total Messages: %d", total));
     }
 
     @Override
     protected Map<String, Object> doCallJson(Map<String, Object> options) {
-        // TODO:
-        return null;
+        JsonObject root = new JsonObject();
+
+        // runtime registry is optional but if enabled we have additional 
statistics to use in output
+        List<RuntimeEndpointRegistry.Statistic> stats = null;
+        RuntimeEndpointRegistry runtimeReg = 
getCamelContext().getRuntimeEndpointRegistry();
+        if (runtimeReg != null) {
+            stats = runtimeReg.getEndpointStatistics();
+        }
+        EndpointRegistry reg = getCamelContext().getEndpointRegistry();
+
+        final List<JsonObject> list = new ArrayList<>();
+        root.put("services", list);
+
+        // find all consumers (IN) direction
+        for (Route route : getCamelContext().getRoutes()) {
+            Consumer consumer = route.getConsumer();
+            Endpoint endpoint = consumer.getEndpoint();
+            if (endpoint instanceof EndpointServiceLocation raa) {
+                String component = endpoint.getComponent().getDefaultName();
+                boolean hosted = false;
+                if (consumer instanceof DefaultConsumer dc) {
+                    hosted = dc.isHostedService();
+                }
+                String adr = raa.getServiceUrl();
+                String protocol = raa.getServiceProtocol();
+                if (adr != null) {
+                    var stat = findStats(stats, endpoint.getEndpointUri(), 
"in");
+                    var uri = endpoint.toString();
+                    JsonObject jo = new JsonObject();
+                    jo.put("component", component);
+                    jo.put("direction", "in");
+                    jo.put("hosted", hosted);
+                    jo.put("protocol", protocol);
+                    jo.put("address", adr);
+                    jo.put("endpointUri", uri);
+                    stat.ifPresent(s -> jo.put("totalMessages", s.getHits()));
+                    list.add(jo);
+                }
+            }
+        }
+
+        // find all endpoint (OUT) direction
+        for (Endpoint endpoint : reg.getReadOnlyValues()) {
+            if (endpoint instanceof EndpointServiceLocation raa) {
+                String component = endpoint.getComponent().getDefaultName();
+                boolean hosted = false;
+                String adr = raa.getServiceUrl();
+                String protocol = raa.getServiceProtocol();
+                if (adr != null) {
+                    var stat = findStats(stats, endpoint.getEndpointUri(), 
"in");
+                    // skip IN as already found via consumer (platform-http is 
only IN)
+                    boolean skip
+                            = "platform-http".equals(component) || 
stat.isPresent() && "in".equals(stat.get().getDirection());
+                    if (!skip) {
+                        stat = findStats(stats, endpoint.getEndpointUri(), 
"out");
+                        var uri = endpoint.toString();
+                        JsonObject jo = new JsonObject();
+                        jo.put("component", component);
+                        jo.put("direction", "in");
+                        jo.put("hosted", hosted);
+                        jo.put("protocol", protocol);
+                        jo.put("address", adr);
+                        jo.put("endpointUri", uri);
+                        stat.ifPresent(s -> jo.put("totalMessages", 
s.getHits()));
+                        list.add(jo);
+                    }
+                }
+            }
+        }
+
+        return root;
     }
 
     private static Optional<RuntimeEndpointRegistry.Statistic> findStats(

Reply via email to