CAMEL-9014: More endpoint usage statitics on the runtime endpoint mbean

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/27017c80
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/27017c80
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/27017c80

Branch: refs/heads/master
Commit: 27017c80c373c63e8ae799b333969d6dbdadc669
Parents: 4f5c1d3
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun Jul 26 14:59:48 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Jul 26 14:59:48 2015 +0200

----------------------------------------------------------------------
 .../management/mbean/CamelOpenMBeanTypes.java   | 11 ++++
 .../ManagedRuntimeEndpointRegistryMBean.java    |  5 ++
 .../impl/DefaultRuntimeEndpointRegistry.java    | 64 ++++++++++++++++++++
 .../mbean/ManagedRuntimeEndpointRegistry.java   | 52 +++++++++++++++-
 .../camel/spi/RuntimeEndpointRegistry.java      | 44 +++++++++++++-
 5 files changed, 171 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/27017c80/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java
 
b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java
index 6c8f571..c72d382 100644
--- 
a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java
+++ 
b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java
@@ -66,6 +66,17 @@ public final class CamelOpenMBeanTypes {
                 new OpenType[]{SimpleType.STRING, SimpleType.BOOLEAN, 
SimpleType.BOOLEAN});
     }
 
+    public static TabularType listRuntimeEndpointsTabularType() throws 
OpenDataException {
+        CompositeType ct = listRuntimeEndpointsCompositeType();
+        return new TabularType("listRuntimeEndpoints", "Lists all the input 
and output endpoints gathered during runtime", ct, new String[]{"url"});
+    }
+
+    public static CompositeType listRuntimeEndpointsCompositeType() throws 
OpenDataException {
+        return new CompositeType("endpoints", "Endpoints", new String[]{"url", 
"routeId", "input", "output", "static", "dynamic"},
+                new String[]{"Url", "Route Id", "Input", "Output", "Static", 
"Dynamic"},
+                new OpenType[]{SimpleType.STRING, SimpleType.STRING, 
SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.BOOLEAN, 
SimpleType.BOOLEAN});
+    }
+
     public static TabularType explainComponentTabularType() throws 
OpenDataException {
         CompositeType ct = explainComponentCompositeType();
         return new TabularType("explainComponent", "Explain how this component 
is configured", ct, new String[]{"option"});

http://git-wip-us.apache.org/repos/asf/camel/blob/27017c80/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRuntimeEndpointRegistryMBean.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRuntimeEndpointRegistryMBean.java
 
b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRuntimeEndpointRegistryMBean.java
index 57637f1..b63d3f1 100644
--- 
a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRuntimeEndpointRegistryMBean.java
+++ 
b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRuntimeEndpointRegistryMBean.java
@@ -18,6 +18,8 @@ package org.apache.camel.api.management.mbean;
 
 import java.util.List;
 
+import javax.management.openmbean.TabularData;
+
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedOperation;
 
@@ -38,4 +40,7 @@ public interface ManagedRuntimeEndpointRegistryMBean extends 
ManagedServiceMBean
     @ManagedOperation(description = " Gets all the endpoint uris captured 
during runtime that are in-use for the given route.")
     List<String> getEndpointsPerRoute(String routeId, boolean includeInputs);
 
+    @ManagedOperation(description = "Lists all the endpoints in the registry 
(url)")
+    TabularData listEndpoints();
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/27017c80/camel-core/src/main/java/org/apache/camel/impl/DefaultRuntimeEndpointRegistry.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultRuntimeEndpointRegistry.java
 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultRuntimeEndpointRegistry.java
index 890b3d3..8faf4b8 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultRuntimeEndpointRegistry.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultRuntimeEndpointRegistry.java
@@ -21,6 +21,7 @@ import java.util.Collections;
 import java.util.EventObject;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -83,6 +84,34 @@ public class DefaultRuntimeEndpointRegistry extends 
EventNotifierSupport impleme
     }
 
     @Override
+    public Map<String, Statistic> getStatistics() {
+        Map<String, Statistic> answer = new LinkedHashMap<String, Statistic>();
+
+        // inputs
+        for (Map.Entry<String, Set<String>> entry : inputs.entrySet()) {
+            String routeId = entry.getKey();
+            for (String uri : entry.getValue()) {
+                answer.put(uri, new EndpointRuntimeStatistics(routeId, true, 
false, 0));
+            }
+        }
+
+        // outputs
+        for (Map.Entry<String, Map<String, String>> entry : 
outputs.entrySet()) {
+            String routeId = entry.getKey();
+            for (String uri : entry.getValue().keySet()) {
+                if (answer.containsKey(uri)) {
+                    // both input and output
+                    answer.put(uri, new EndpointRuntimeStatistics(routeId, 
true, true, 0));
+                } else {
+                    answer.put(uri, new EndpointRuntimeStatistics(routeId, 
false, true, 0));
+                }
+            }
+        }
+
+        return answer;
+    }
+
+    @Override
     public int getLimit() {
         return limit;
     }
@@ -172,4 +201,39 @@ public class DefaultRuntimeEndpointRegistry extends 
EventNotifierSupport impleme
                 || event instanceof RouteAddedEvent
                 || event instanceof RouteRemovedEvent;
     }
+
+    private static class EndpointRuntimeStatistics implements Statistic {
+
+        private final String routeId;
+        private final boolean input;
+        private final boolean output;
+        private final long hits;
+
+        private EndpointRuntimeStatistics(String routeId, boolean input, 
boolean output, long hits) {
+            this.routeId = routeId;
+            this.input = input;
+            this.output = output;
+            this.hits = hits;
+        }
+
+        @Override
+        public String getRouteId() {
+            return routeId;
+        }
+
+        @Override
+        public boolean isInput() {
+            return input;
+        }
+
+        @Override
+        public boolean isOutput() {
+            return output;
+        }
+
+        @Override
+        public long getHits() {
+            return hits;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/27017c80/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRuntimeEndpointRegistry.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRuntimeEndpointRegistry.java
 
b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRuntimeEndpointRegistry.java
index 8b9a286..74efaba 100644
--- 
a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRuntimeEndpointRegistry.java
+++ 
b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRuntimeEndpointRegistry.java
@@ -17,25 +17,39 @@
 package org.apache.camel.management.mbean;
 
 import java.util.List;
+import java.util.Map;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
 import 
org.apache.camel.api.management.mbean.ManagedRuntimeEndpointRegistryMBean;
+import org.apache.camel.spi.EndpointRegistry;
+import org.apache.camel.spi.ManagementStrategy;
 import org.apache.camel.spi.RuntimeEndpointRegistry;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
 
-/**
- *
- */
 @ManagedResource(description = "Managed RuntimeEndpointRegistry")
 public class ManagedRuntimeEndpointRegistry extends ManagedService implements 
ManagedRuntimeEndpointRegistryMBean {
 
     private final RuntimeEndpointRegistry registry;
+    private boolean sanitize;
 
     public ManagedRuntimeEndpointRegistry(CamelContext context, 
RuntimeEndpointRegistry registry) {
         super(context, registry);
         this.registry = registry;
     }
 
+    public void init(ManagementStrategy strategy) {
+        super.init(strategy);
+        sanitize = strategy.getManagementAgent().getMask() != null ? 
strategy.getManagementAgent().getMask() : false;
+    }
+
     @Override
     public void reset() {
         registry.reset();
@@ -60,4 +74,36 @@ public class ManagedRuntimeEndpointRegistry extends 
ManagedService implements Ma
     public List<String> getEndpointsPerRoute(String routeId, boolean 
includeInputs) {
         return registry.getEndpointsPerRoute(routeId, includeInputs);
     }
+
+    @Override
+    public TabularData listEndpoints() {
+        try {
+            TabularData answer = new 
TabularDataSupport(CamelOpenMBeanTypes.listRuntimeEndpointsTabularType());
+
+            EndpointRegistry staticRegistry = 
getContext().getEndpointRegistry();
+
+            Map<String, RuntimeEndpointRegistry.Statistic> stats = 
registry.getStatistics();
+            for (Map.Entry<String, RuntimeEndpointRegistry.Statistic> entry : 
stats.entrySet()) {
+                CompositeType ct = 
CamelOpenMBeanTypes.listRuntimeEndpointsCompositeType();
+
+                String url = entry.getKey();
+
+                Boolean isStatic = staticRegistry.isStatic(url);
+                Boolean isDynamic = staticRegistry.isDynamic(url);
+                if (sanitize) {
+                    url = URISupport.sanitizeUri(url);
+                }
+                String routeId = entry.getValue().getRouteId();
+                Boolean input = entry.getValue().isInput();
+                Boolean output = entry.getValue().isOutput();
+
+                CompositeData data = new CompositeDataSupport(ct, new 
String[]{"url", "routeId", "input", "output", "static", "dynamic"},
+                        new Object[]{url, routeId, input, output, isStatic, 
isDynamic});
+                answer.put(data);
+            }
+            return answer;
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/27017c80/camel-core/src/main/java/org/apache/camel/spi/RuntimeEndpointRegistry.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/spi/RuntimeEndpointRegistry.java 
b/camel-core/src/main/java/org/apache/camel/spi/RuntimeEndpointRegistry.java
index b0d71d9..a56cc04 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/RuntimeEndpointRegistry.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/RuntimeEndpointRegistry.java
@@ -17,13 +17,47 @@
 package org.apache.camel.spi;
 
 import java.util.List;
+import java.util.Map;
 
-import org.apache.camel.Service;
+import org.apache.camel.StaticService;
 
 /**
  * A registry which listen for runtime usage of {@link 
org.apache.camel.Endpoint} during routing in Camel.
  */
-public interface RuntimeEndpointRegistry extends Service {
+public interface RuntimeEndpointRegistry extends StaticService {
+
+    /**
+     * Statistics gathered about the endpoint.
+     */
+    public interface Statistic {
+
+        /**
+         * The route id (if the endpoint is associated with a route)
+         */
+        String getRouteId();
+
+        /**
+         * Whether the endpoint is used as input
+         * <p/>
+         * Notice an endpoint can be used as both input and output, such as 
when its linking two routes
+         */
+        boolean isInput();
+
+        /**
+         * Whether the endpoint is used as output
+         * <p/>
+         * Notice an endpoint can be used as both input and output, such as 
when its linking two routes
+         */
+        boolean isOutput();
+
+        /**
+         * Usage of the endpoint, such as how many messages it has received / 
sent to
+         * <p/>
+         * This information is only available if {@link 
org.apache.camel.ManagementStatisticsLevel} is configured as
+         * {@link org.apache.camel.ManagementStatisticsLevel#Extended}.
+         */
+        long getHits();
+    }
 
     /**
      * Whether gathering runtime usage is enabled or not.
@@ -71,4 +105,10 @@ public interface RuntimeEndpointRegistry extends Service {
      * @param includeInputs whether to include route inputs
      */
     List<String> getEndpointsPerRoute(String routeId, boolean includeInputs);
+
+    /**
+     * Gets details about all the endpoint captured from the given route 
during runtime routing that are in-use of the routes.
+     */
+    Map<String, Statistic> getStatistics();
+
 }

Reply via email to