camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] camel git commit: CAMEL-9879: Circuit Breaker EIP - That is using hystrix.
Date Wed, 20 Apr 2016 09:54:28 GMT
CAMEL-9879: Circuit Breaker EIP - That is using hystrix.


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

Branch: refs/heads/master
Commit: 441a3c6f8a1f1545849cd9f6f21251c4ffb546bb
Parents: bebcce4
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Apr 20 11:52:28 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Apr 20 11:54:19 2016 +0200

----------------------------------------------------------------------
 .../metrics/HystrixEventStreamService.java      | 73 ++++++++++++++++----
 .../servlet/HystrixEventStreamServlet.java      | 42 +++++++++++
 .../processor/HystrixManagementTest.java        | 29 +++++---
 .../src/test/resources/log4j.properties         |  4 +-
 4 files changed, 126 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/441a3c6f/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/HystrixEventStreamService.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/HystrixEventStreamService.java
b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/HystrixEventStreamService.java
index cdfe955..6da30a8 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/HystrixEventStreamService.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/HystrixEventStreamService.java
@@ -5,9 +5,9 @@
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
- * <p/>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p/>
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,25 +16,37 @@
  */
 package org.apache.camel.component.hystrix.metrics;
 
+import java.util.Queue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Stream;
+
 import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsPoller;
 import org.apache.camel.StaticService;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedOperation;
 import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.component.hystrix.metrics.servlet.HystrixEventStreamServlet;
 import org.apache.camel.support.ServiceSupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * To gather hystrix metrics and offer the metrics over JMX and Java APIs.
+ * <p/>
+ * If you want to expose the metrics over HTTP then you can use the {@link HystrixEventStreamServlet}
servlet which
+ * provides such functionality.
+ */
 @ManagedResource(description = "Managed Hystrix EventStreamService")
 public class HystrixEventStreamService extends ServiceSupport implements StaticService, HystrixMetricsPoller.MetricsAsJsonPollerListener
{
 
-    // TODO: need for command and thread pool
-    // or use some queue to store in backlog
-
+    public static final int METRICS_QUEUE_SIZE = 1000;
     private static final Logger LOG = LoggerFactory.getLogger(HystrixEventStreamService.class);
+
     private int delay = 500;
+    private int queueSize = METRICS_QUEUE_SIZE;
     private HystrixMetricsPoller poller;
-    private transient String latest;
+    // use a queue with a upper limit to avoid storing too many metrics
+    private Queue<String> queue;
 
     public int getDelay() {
         return delay;
@@ -47,9 +59,35 @@ public class HystrixEventStreamService extends ServiceSupport implements
StaticS
         this.delay = delay;
     }
 
-    @ManagedOperation(description = "Returns the latest metrics as JSon format")
-    public String latestMetricsAsJSon() {
-        return latest;
+    public int getQueueSize() {
+        return queueSize;
+    }
+
+    /**
+     * Sets the queue size for how many metrics collected are stored in-memory in a backlog
+     */
+    public void setQueueSize(int queueSize) {
+        this.queueSize = queueSize;
+    }
+
+    /**
+     * Return a stream of the JSon metrics.
+     */
+    public Stream<String> streamMetrics() {
+        if (queue != null) {
+            return queue.stream();
+        } else {
+            return null;
+        }
+    }
+
+    @ManagedOperation(description = "Returns the oldest metrics as JSon format")
+    public String oldestMetricsAsJSon() {
+        if (queue != null) {
+            return queue.peek();
+        } else {
+            return null;
+        }
     }
 
     @ManagedOperation(description = "Starts the metrics poller")
@@ -74,7 +112,8 @@ public class HystrixEventStreamService extends ServiceSupport implements
StaticS
 
     @Override
     protected void doStart() throws Exception {
-        LOG.info("Starting HystrixMetricsPoller with delay: {}", delay);
+        LOG.info("Starting HystrixMetricsPoller with delay: {} and queue size: {}", delay,
queueSize);
+        queue = new LinkedBlockingQueue<String>(queueSize);
         poller = new HystrixMetricsPoller(this, delay);
         poller.start();
     }
@@ -90,6 +129,16 @@ public class HystrixEventStreamService extends ServiceSupport implements
StaticS
     @Override
     public void handleJsonMetric(String json) {
         LOG.debug("handleJsonMetric: {}", json);
-        this.latest = json;
+
+        // ensure there is space on the queue by polling until at least single slot is free
+        int drain = queue.size() - queueSize + 1;
+        if (drain > 0) {
+            LOG.debug("Draining queue to make room: {}", drain);
+            for (int i = 0; i < drain; i++) {
+                queue.poll();
+            }
+        }
+
+        queue.add(json);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/441a3c6f/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/servlet/HystrixEventStreamServlet.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/servlet/HystrixEventStreamServlet.java
b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/servlet/HystrixEventStreamServlet.java
new file mode 100644
index 0000000..5e08bd5
--- /dev/null
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/metrics/servlet/HystrixEventStreamServlet.java
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.hystrix.metrics.servlet;
+
+/**
+ * Streams Hystrix metrics in text/event-stream format.
+ * <p/>
+ * Install by:
+ * <p/>
+ * 1) Including camel-hystrix-*.jar in your classpath.
+ * <p/>
+ * 2) Adding the following to web.xml:
+ * <pre>{@code
+ * <servlet>
+ *  <display-name>HystrixEventStreamServlet</display-name>
+ *  <servlet-name>HystrixEventStreamServlet</servlet-name>
+ *  <servlet-class>org.apache.camel.component.hystrix.metrics.servlet.HystrixEventStreamServlet</servlet-class>
+ * </servlet>
+ * <servlet-mapping>
+ *  <servlet-name>HystrixEventStreamServlet</servlet-name>
+ *  <url-pattern>/hystrix.stream</url-pattern>
+ * </servlet-mapping>
+ * } </pre>
+ * <p/>
+ * See more details at: https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-metrics-event-stream
+ */
+public class HystrixEventStreamServlet extends com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet
{
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/441a3c6f/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixManagementTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixManagementTest.java
b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixManagementTest.java
index 51cd31b..2272c94 100644
--- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixManagementTest.java
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixManagementTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.hystrix.processor;
 
+import java.util.stream.Stream;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
@@ -26,6 +27,8 @@ import org.junit.Test;
 
 public class HystrixManagementTest extends CamelTestSupport {
 
+    private HystrixEventStreamService stream = new HystrixEventStreamService();
+
     @Override
     protected boolean useJmx() {
         return true;
@@ -73,7 +76,16 @@ public class HystrixManagementTest extends CamelTestSupport {
         Long errorCount = (Long) mbeanServer.getAttribute(on, "HystrixErrorCount");
         assertEquals(0, errorCount.longValue());
 
-        Thread.sleep(5000);
+        // let it gather for a while
+        Thread.sleep(1000);
+
+        String latest = stream.oldestMetricsAsJSon();
+        log.info("Oldest json stream: {}", latest);
+
+        Stream<String> jsons = stream.streamMetrics();
+        jsons.forEach(s -> {
+                log.info("JSon: {}", s);
+            });
     }
 
     @Override
@@ -81,20 +93,21 @@ public class HystrixManagementTest extends CamelTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-
                 // add the stream
-                context.addService(new HystrixEventStreamService());
+                stream.setQueueSize(10);
+                stream.setDelay(100);
+                context.addService(stream);
 
                 from("direct:start").routeId("start")
-                    .hystrix().id("myHystrix")
+                        .hystrix().id("myHystrix")
                         .to("direct:foo")
-                    .onFallback()
+                        .onFallback()
                         .transform().constant("Fallback message")
-                    .end()
-                    .to("mock:result");
+                        .end()
+                        .to("mock:result");
 
                 from("direct:foo")
-                    .transform().constant("Bye World");
+                        .transform().constant("Bye World");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/441a3c6f/components/camel-hystrix/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/resources/log4j.properties b/components/camel-hystrix/src/test/resources/log4j.properties
index ca2c0bd..2d541ed 100644
--- a/components/camel-hystrix/src/test/resources/log4j.properties
+++ b/components/camel-hystrix/src/test/resources/log4j.properties
@@ -18,10 +18,10 @@
 #
 # The logging properties used for testing.
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
 
 # uncomment the following to enable camel debugging
-log4j.logger.org.apache.camel.component.hystrix=DEBUG
+#log4j.logger.org.apache.camel.component.hystrix=DEBUG
 
 # CONSOLE appender not used by default
 log4j.appender.out=org.apache.log4j.ConsoleAppender


Mime
View raw message