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 3d3159fde01440fd8699eb89c56c77e2b9b84f7e
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri May 24 14:21:13 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.
---
 .../camel/impl/console/EndpointDevConsole.java     | 18 ++--
 .../dsl/jbang/core/commands/CamelJBangMain.java    |  1 +
 .../{ListEndpoint.java => ListAddress.java}        | 96 ++++++++++------------
 .../jbang/core/commands/process/ListConsumer.java  |  2 +-
 .../jbang/core/commands/process/ListEndpoint.java  | 30 -------
 5 files changed, 55 insertions(+), 92 deletions(-)

diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/EndpointDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/EndpointDevConsole.java
index 794a57cf584..4f8c057802a 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/EndpointDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/EndpointDevConsole.java
@@ -96,8 +96,16 @@ public class EndpointDevConsole extends AbstractDevConsole {
         Collection<Endpoint> col = reg.getReadOnlyValues();
         for (Endpoint e : col) {
             JsonObject jo = new JsonObject();
-            boolean stub = 
e.getComponent().getClass().getSimpleName().equals("StubComponent");
             jo.put("uri", e.getEndpointUri());
+            boolean stub = 
e.getComponent().getClass().getSimpleName().equals("StubComponent");
+            jo.put("stub", stub);
+            var stat = findStats(stats, e.getEndpointUri());
+            if (stat.isPresent()) {
+                var st = stat.get();
+                jo.put("direction", st.getDirection());
+                jo.put("hits", st.getHits());
+                jo.put("routeId", st.getRouteId());
+            }
             if (e instanceof EndpointLocationAddress raa) {
                 JsonObject ro = new JsonObject();
                 ro.put("hosted", raa.isHostedAddress());
@@ -109,14 +117,6 @@ public class EndpointDevConsole extends AbstractDevConsole 
{
                 }
                 jo.put("location", ro);
             }
-            jo.put("stub", stub);
-            var stat = findStats(stats, e.getEndpointUri());
-            if (stat.isPresent()) {
-                var st = stat.get();
-                jo.put("direction", st.getDirection());
-                jo.put("hits", st.getHits());
-                jo.put("routeId", st.getRouteId());
-            }
             list.add(jo);
         }
 
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index 60844e1e3af..8dd7c5eaf98 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -90,6 +90,7 @@ public class CamelJBangMain implements Callable<Integer> {
                         .addSubcommand("variable", new CommandLine(new 
ListVariable(main)))
                         .addSubcommand("consumer", new CommandLine(new 
ListConsumer(main)))
                         .addSubcommand("endpoint", new CommandLine(new 
ListEndpoint(main)))
+                        .addSubcommand("address", new CommandLine(new 
ListAddress(main)))
                         .addSubcommand("event", new CommandLine(new 
ListEvent(main)))
                         .addSubcommand("inflight", new CommandLine(new 
ListInflight(main)))
                         .addSubcommand("blocked", new CommandLine(new 
ListBlocked(main)))
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListAddress.java
similarity index 75%
copy from 
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
copy to 
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListAddress.java
index 4e576d79446..446de7b95f7 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListAddress.java
@@ -18,7 +18,6 @@ package org.apache.camel.dsl.jbang.core.commands.process;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Iterator;
 import java.util.List;
 import java.util.StringJoiner;
 
@@ -27,65 +26,43 @@ import com.github.freva.asciitable.Column;
 import com.github.freva.asciitable.HorizontalAlign;
 import com.github.freva.asciitable.OverflowBehaviour;
 import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PidNameAgeCompletionCandidates;
 import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
 import org.apache.camel.support.PatternHelper;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.json.JsonArray;
 import org.apache.camel.util.json.JsonObject;
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
 
-@Command(name = "endpoint", description = "Get usage of Camel endpoints", 
sortOptions = false)
-public class ListEndpoint extends ProcessWatchCommand {
-
-    public static class PidNameAgeTotalCompletionCandidates implements 
Iterable<String> {
-
-        public PidNameAgeTotalCompletionCandidates() {
-        }
-
-        @Override
-        public Iterator<String> iterator() {
-            return List.of("pid", "name", "age", "total").iterator();
-        }
-
-    }
+@Command(name = "address", description = "Get usage of Camel service addresses 
(hosted and external)", sortOptions = false)
+public class ListAddress extends ProcessWatchCommand {
 
     @CommandLine.Parameters(description = "Name or pid of running Camel 
integration", arity = "0..1")
     String name = "*";
 
-    @CommandLine.Option(names = { "--sort" }, completionCandidates = 
PidNameAgeTotalCompletionCandidates.class,
-                        description = "Sort by pid, name, age or total", 
defaultValue = "pid")
+    @CommandLine.Option(names = {"--sort"}, completionCandidates = 
PidNameAgeCompletionCandidates.class,
+            description = "Sort by pid, name or total", defaultValue = "pid")
     String sort;
 
-    @CommandLine.Option(names = { "--limit" },
-                        description = "Filter endpoints by limiting to the 
given number of rows")
+    @CommandLine.Option(names = {"--limit"},
+            description = "Filter addresses by limiting to the given number of 
rows")
     int limit;
 
-    @CommandLine.Option(names = { "--filter" },
-                        description = "Filter endpoints by URI")
+    @CommandLine.Option(names = {"--filter"},
+            description = "Filter addresses")
     String filter;
 
-    @CommandLine.Option(names = { "--filter-direction" },
-                        description = "Filter by direction (in or out)")
+    @CommandLine.Option(names = {"--filter-direction"},
+            description = "Filter by direction (in or out)")
     String filterDirection;
 
-    @CommandLine.Option(names = { "--filter-total" },
-                        description = "Filter endpoints that must be higher 
than the given usage")
-    long filterTotal;
-
-    @CommandLine.Option(names = { "--short-uri" },
-                        description = "List endpoint URI without query 
parameters (short)")
-    boolean shortUri;
-
-    @CommandLine.Option(names = { "--wide-uri" },
-                        description = "List endpoint URI in full details")
+    @CommandLine.Option(names = {"--wide-uri"},
+            description = "List endpoint URI in full details")
     boolean wideUri;
 
-    @CommandLine.Option(names = { "--address" },
-                        description = "List address specific information (only 
possible for some components)")
-    boolean address;
-
-    public ListEndpoint(CamelJBangMain main) {
+    public ListAddress(CamelJBangMain main) {
         super(main);
     }
 
@@ -121,16 +98,15 @@ public class ListEndpoint extends ProcessWatchCommand {
                                 if (ro != null) {
                                     row.address = ro;
                                 }
-                                row.stub = o.getBooleanOrDefault("stub", 
false);
                                 row.direction = o.getString("direction");
                                 row.total = o.getString("hits");
                                 row.uptime = extractSince(ph);
                                 row.age = TimeUtils.printSince(row.uptime);
                                 boolean add = true;
-                                if (filterTotal > 0 && (row.total == null || 
Long.parseLong(row.total) < filterTotal)) {
+                                if (filterDirection != null && 
!filterDirection.equals(row.direction)) {
                                     add = false;
                                 }
-                                if (filterDirection != null && 
!filterDirection.equals(row.direction)) {
+                                if (row.address == null) {
                                     add = false;
                                 }
                                 if (filter != null) {
@@ -174,23 +150,23 @@ public class ListEndpoint extends ProcessWatchCommand {
                 new 
Column().header("NAME").dataAlign(HorizontalAlign.LEFT).maxWidth(30, 
OverflowBehaviour.ELLIPSIS_RIGHT)
                         .with(r -> r.name),
                 new 
Column().header("AGE").headerAlign(HorizontalAlign.CENTER).with(r -> r.age),
-                new Column().header("DIR").with(r -> r.direction),
+                new Column().header("DIR").with(this::getDirection),
                 new Column().header("TOTAL").with(r -> r.total),
-                new 
Column().header("STUB").dataAlign(HorizontalAlign.CENTER).with(r -> r.stub ? 
"x" : ""),
-                new 
Column().header("ADDRESS").visible(address).dataAlign(HorizontalAlign.LEFT)
-                        .maxWidth(90, OverflowBehaviour.ELLIPSIS_RIGHT)
-                        .with(this::getAddress),
+                new 
Column().header("HOSTED").dataAlign(HorizontalAlign.CENTER).with(this::getHosted),
                 new 
Column().header("URI").visible(!wideUri).dataAlign(HorizontalAlign.LEFT)
                         .maxWidth(90, OverflowBehaviour.ELLIPSIS_RIGHT)
                         .with(this::getUri),
                 new 
Column().header("URI").visible(wideUri).dataAlign(HorizontalAlign.LEFT)
                         .maxWidth(140, OverflowBehaviour.NEWLINE)
-                        .with(this::getUri))));
+                        .with(this::getUri),
+                new Column().header("ADDRESS").dataAlign(HorizontalAlign.LEFT)
+                        .maxWidth(90, OverflowBehaviour.ELLIPSIS_RIGHT)
+                        .with(this::getAddress))));
     }
 
     private String getUri(Row r) {
         String u = r.endpoint;
-        if (shortUri) {
+        if (!wideUri) {
             int pos = u.indexOf('?');
             if (pos > 0) {
                 u = u.substring(0, pos);
@@ -199,6 +175,15 @@ public class ListEndpoint extends ProcessWatchCommand {
         return u;
     }
 
+    private String getDirection(Row r) {
+        String dir = r.direction;
+        if (dir == null || dir.isEmpty()) {
+            // hosted is always in
+            dir = "x".equals(getHosted(r)) ? "in" : "out";
+        }
+        return dir;
+    }
+
     private String getAddress(Row r) {
         String a = "";
         if (r.address != null) {
@@ -210,12 +195,22 @@ public class ListEndpoint extends ProcessWatchCommand {
                         sj.add(k + "=" + v);
                     }
                 });
-                a = a + " (" + sj + ")";
+                if (sj.length() > 0) {
+                    a = a + " (" + sj + ")";
+                }
             }
         }
         return a;
     }
 
+    private String getHosted(Row r) {
+        boolean hosted = false;
+        if (r.address != null) {
+            hosted = r.address.getBooleanOrDefault("hosted", false);
+        }
+        return hosted ? "x" : "";
+    }
+
     protected int sortRow(Row o1, Row o2) {
         String s = sort;
         int negate = 1;
@@ -230,8 +225,6 @@ public class ListEndpoint extends ProcessWatchCommand {
                 return o1.name.compareToIgnoreCase(o2.name) * negate;
             case "age":
                 return Long.compare(o1.uptime, o2.uptime) * negate;
-            case "total":
-                return Long.compare(Long.parseLong(o1.total), 
Long.parseLong(o2.total)) * negate;
             default:
                 return 0;
         }
@@ -245,7 +238,6 @@ public class ListEndpoint extends ProcessWatchCommand {
         String endpoint;
         String direction;
         String total;
-        boolean stub;
         JsonObject address;
     }
 
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListConsumer.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListConsumer.java
index 211f3c83368..ee5224aca8c 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListConsumer.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListConsumer.java
@@ -42,7 +42,7 @@ public class ListConsumer extends ProcessWatchCommand {
     String name = "*";
 
     @CommandLine.Option(names = { "--sort" }, completionCandidates = 
PidNameAgeCompletionCandidates.class,
-                        description = "Sort by pid, name, or age", 
defaultValue = "pid")
+                        description = "Sort by pid, name or age", defaultValue 
= "pid")
     String sort;
 
     @CommandLine.Option(names = { "--limit" },
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
index 4e576d79446..7fe9e947d19 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListEndpoint.java
@@ -20,7 +20,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
-import java.util.StringJoiner;
 
 import com.github.freva.asciitable.AsciiTable;
 import com.github.freva.asciitable.Column;
@@ -81,10 +80,6 @@ public class ListEndpoint extends ProcessWatchCommand {
                         description = "List endpoint URI in full details")
     boolean wideUri;
 
-    @CommandLine.Option(names = { "--address" },
-                        description = "List address specific information (only 
possible for some components)")
-    boolean address;
-
     public ListEndpoint(CamelJBangMain main) {
         super(main);
     }
@@ -117,10 +112,6 @@ public class ListEndpoint extends ProcessWatchCommand {
                                 }
                                 row.pid = Long.toString(ph.pid());
                                 row.endpoint = o.getString("uri");
-                                JsonObject ro = (JsonObject) o.get("location");
-                                if (ro != null) {
-                                    row.address = ro;
-                                }
                                 row.stub = o.getBooleanOrDefault("stub", 
false);
                                 row.direction = o.getString("direction");
                                 row.total = o.getString("hits");
@@ -177,9 +168,6 @@ public class ListEndpoint extends ProcessWatchCommand {
                 new Column().header("DIR").with(r -> r.direction),
                 new Column().header("TOTAL").with(r -> r.total),
                 new 
Column().header("STUB").dataAlign(HorizontalAlign.CENTER).with(r -> r.stub ? 
"x" : ""),
-                new 
Column().header("ADDRESS").visible(address).dataAlign(HorizontalAlign.LEFT)
-                        .maxWidth(90, OverflowBehaviour.ELLIPSIS_RIGHT)
-                        .with(this::getAddress),
                 new 
Column().header("URI").visible(!wideUri).dataAlign(HorizontalAlign.LEFT)
                         .maxWidth(90, OverflowBehaviour.ELLIPSIS_RIGHT)
                         .with(this::getUri),
@@ -199,23 +187,6 @@ public class ListEndpoint extends ProcessWatchCommand {
         return u;
     }
 
-    private String getAddress(Row r) {
-        String a = "";
-        if (r.address != null) {
-            a = r.address.getString("address");
-            if (r.address.size() > 1) {
-                StringJoiner sj = new StringJoiner(" ");
-                r.address.forEach((k, v) -> {
-                    if (!"address".equals(k) && !"hosted".equals(k) && 
!"remote".equals(k)) {
-                        sj.add(k + "=" + v);
-                    }
-                });
-                a = a + " (" + sj + ")";
-            }
-        }
-        return a;
-    }
-
     protected int sortRow(Row o1, Row o2) {
         String s = sort;
         int negate = 1;
@@ -246,7 +217,6 @@ public class ListEndpoint extends ProcessWatchCommand {
         String direction;
         String total;
         boolean stub;
-        JsonObject address;
     }
 
 }

Reply via email to