camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [09/34] git commit: Added header to enable overriding histogram value in the URI.
Date Wed, 30 Jul 2014 13:00:52 GMT
Added header to enable overriding histogram value in the URI.


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

Branch: refs/heads/master
Commit: 3fe1d6c6f3acc5a62c6894d33f2efa11694743c3
Parents: e6cd8d5
Author: Lauri Kimmel <lauri.kimmel@gmx.com>
Authored: Mon May 26 22:14:58 2014 +1000
Committer: Lauri Kimmel <lauri.kimmel@gmx.com>
Committed: Mon May 26 22:14:58 2014 +1000

----------------------------------------------------------------------
 README.md                                       | 14 ++++++
 .../apache/camel/metrics/MetricsComponent.java  |  1 +
 .../metrics/histogram/HistogramProducer.java    |  7 ++-
 .../histogram/HistogramProducerTest.java        | 33 ++++++++++++++
 .../metrics/histogram/HistogramRouteTest.java   | 47 ++++++++++++++++----
 5 files changed, 92 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3fe1d6c6/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index f961937..296b2f3 100644
--- a/README.md
+++ b/README.md
@@ -189,6 +189,20 @@ from("direct:in")
     .to("direct:out")
 ```
 
+### Headers
+
+| Name                       | Description
+|----------------------------|---------------------------------|
+| CamelMetricsHistogramValue | Override increment value in URI |
+
+```java
+// adds value 992 to simple.histogram
+from("direct:in")
+    .setHeader(MetricsComponent.HEADER_HISTOGRAM_VALUE, constant(992L))
+    .to("metric:histogram:simple.histogram?value=700")
+    .to("direct:out")
+```
+
 ## Metrics type timer
 
 ```

http://git-wip-us.apache.org/repos/asf/camel/blob/3fe1d6c6/src/main/java/org/apache/camel/metrics/MetricsComponent.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/MetricsComponent.java b/src/main/java/org/apache/camel/metrics/MetricsComponent.java
index cc6a64b..c129c07 100644
--- a/src/main/java/org/apache/camel/metrics/MetricsComponent.java
+++ b/src/main/java/org/apache/camel/metrics/MetricsComponent.java
@@ -29,6 +29,7 @@ public class MetricsComponent extends DefaultComponent {
     public static final String HEADER_METRIC_NAME = "CamelMetricsName";
     public static final String HEADER_COUNTER_INCREMENT = "CamelMetricsCounterIncrement";
     public static final String HEADER_COUNTER_DECREMENT = "CamelMetricsCounterDecrement";
+    public static final String HEADER_HISTOGRAM_VALUE = "CamelMetricsHistogramValue";
 
     private static final Logger LOG = LoggerFactory.getLogger(MetricsComponent.class);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/3fe1d6c6/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java b/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
index 41b5cf7..bd701c0 100644
--- a/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
+++ b/src/main/java/org/apache/camel/metrics/histogram/HistogramProducer.java
@@ -1,5 +1,7 @@
 package org.apache.camel.metrics.histogram;
 
+import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE;
+
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
@@ -24,8 +26,9 @@ public class HistogramProducer extends DefaultProducer {
         String metricsName = endpoint.getMetricsName(exchange);
         Histogram histogram = registry.histogram(metricsName);
         Long value = endpoint.getValue();
-        if (value != null) {
-            histogram.update(value);
+        Long finalValue = endpoint.getLongHeader(exchange, HEADER_HISTOGRAM_VALUE, value);
+        if (finalValue != null) {
+            histogram.update(finalValue);
         }
         else {
             LOG.warn("Cannot update histogram \"{}\" with null value", metricsName);

http://git-wip-us.apache.org/repos/asf/camel/blob/3fe1d6c6/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java b/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
index 8662e9d..5cb5529 100644
--- a/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
+++ b/src/test/java/org/apache/camel/metrics/histogram/HistogramProducerTest.java
@@ -1,5 +1,6 @@
 package org.apache.camel.metrics.histogram;
 
+import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.mockito.Mockito.times;
@@ -56,11 +57,13 @@ public class HistogramProducerTest {
     @Test
     public void testProcessValueSet() throws Exception {
         when(endpoint.getValue()).thenReturn(VALUE);
+        when(endpoint.getLongHeader(exchange, HEADER_HISTOGRAM_VALUE, VALUE)).thenReturn(VALUE);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(registry, times(1)).histogram(METRICS_NAME);
         inOrder.verify(endpoint, times(1)).getValue();
+        inOrder.verify(endpoint, times(1)).getLongHeader(exchange, HEADER_HISTOGRAM_VALUE,
VALUE);
         inOrder.verify(histogram, times(1)).update(VALUE);
         inOrder.verifyNoMoreInteractions();
     }
@@ -68,11 +71,41 @@ public class HistogramProducerTest {
     @Test
     public void testProcessValueNotSet() throws Exception {
         when(endpoint.getValue()).thenReturn(null);
+        when(endpoint.getLongHeader(exchange, HEADER_HISTOGRAM_VALUE, null)).thenReturn(null);
         producer.process(exchange);
         inOrder.verify(endpoint, times(1)).getRegistry();
         inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
         inOrder.verify(registry, times(1)).histogram(METRICS_NAME);
         inOrder.verify(endpoint, times(1)).getValue();
+        inOrder.verify(endpoint, times(1)).getLongHeader(exchange, HEADER_HISTOGRAM_VALUE,
null);
+        inOrder.verifyNoMoreInteractions();
+    }
+
+    @Test
+    public void testProcessOverrideValue() throws Exception {
+        when(endpoint.getValue()).thenReturn(VALUE);
+        when(endpoint.getLongHeader(exchange, HEADER_HISTOGRAM_VALUE, VALUE)).thenReturn(VALUE
+ 3);
+        producer.process(exchange);
+        inOrder.verify(endpoint, times(1)).getRegistry();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
+        inOrder.verify(registry, times(1)).histogram(METRICS_NAME);
+        inOrder.verify(endpoint, times(1)).getValue();
+        inOrder.verify(endpoint, times(1)).getLongHeader(exchange, HEADER_HISTOGRAM_VALUE,
VALUE);
+        inOrder.verify(histogram, times(1)).update(VALUE + 3);
+        inOrder.verifyNoMoreInteractions();
+    }
+
+    @Test
+    public void testProcessOverrideUriValueNotSet() throws Exception {
+        when(endpoint.getValue()).thenReturn(null);
+        when(endpoint.getLongHeader(exchange, HEADER_HISTOGRAM_VALUE, null)).thenReturn(VALUE
+ 2);
+        producer.process(exchange);
+        inOrder.verify(endpoint, times(1)).getRegistry();
+        inOrder.verify(endpoint, times(1)).getMetricsName(exchange);
+        inOrder.verify(registry, times(1)).histogram(METRICS_NAME);
+        inOrder.verify(endpoint, times(1)).getValue();
+        inOrder.verify(endpoint, times(1)).getLongHeader(exchange, HEADER_HISTOGRAM_VALUE,
null);
+        inOrder.verify(histogram, times(1)).update(VALUE + 2);
         inOrder.verifyNoMoreInteractions();
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/3fe1d6c6/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java b/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
index e8646cb..cc01a1d 100644
--- a/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
+++ b/src/test/java/org/apache/camel/metrics/histogram/HistogramRouteTest.java
@@ -1,5 +1,9 @@
 package org.apache.camel.metrics.histogram;
 
+import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE;
+import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME;
+import static org.apache.camel.metrics.MetricsComponent.METRIC_REGISTRY_NAME;
+import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.when;
 
@@ -8,11 +12,12 @@ import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.metrics.MetricsComponent;
 import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
 import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader;
 import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
 import org.apache.camel.test.spring.MockEndpoints;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InOrder;
@@ -37,6 +42,12 @@ public class HistogramRouteTest {
     @Produce(uri = "direct:in")
     private ProducerTemplate producer;
 
+    private MetricRegistry mockRegistry;
+
+    private Histogram mockHistogram;
+
+    private InOrder inOrder;
+
     @Configuration
     public static class TestConfig extends SingleRouteCamelConfiguration {
 
@@ -54,25 +65,45 @@ public class HistogramRouteTest {
             };
         }
 
-        @Bean(name = MetricsComponent.METRIC_REGISTRY_NAME)
+        @Bean(name = METRIC_REGISTRY_NAME)
         public MetricRegistry getMetricRegistry() {
             return Mockito.mock(MetricRegistry.class);
         }
     }
 
+    @Before
+    public void setup() {
+        // TODO - 12.05.2014, Lauri - is there any better way to set this up?
+        mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME,
MetricRegistry.class);
+        mockHistogram = Mockito.mock(Histogram.class);
+        inOrder = Mockito.inOrder(mockRegistry, mockHistogram);
+    }
+
+    @After
+    public void tearDown() {
+        endpoint.reset();
+        reset(mockRegistry);
+    }
+
     @Test
     public void testOverrideMetricsName() throws Exception {
-        // TODO - 12.05.2014, Lauri - is there any better way to set this up?
-        MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME,
MetricRegistry.class);
-        Histogram mockHistogram = Mockito.mock(Histogram.class);
-        InOrder inOrder = Mockito.inOrder(mockRegistry, mockHistogram);
         when(mockRegistry.histogram("B")).thenReturn(mockHistogram);
-
         endpoint.expectedMessageCount(1);
-        producer.sendBodyAndHeader(new Object(), MetricsComponent.HEADER_METRIC_NAME, "B");
+        producer.sendBodyAndHeader(new Object(), HEADER_METRIC_NAME, "B");
         endpoint.assertIsSatisfied();
         inOrder.verify(mockRegistry, times(1)).histogram("B");
         inOrder.verify(mockHistogram, times(1)).update(332491L);
         inOrder.verifyNoMoreInteractions();
     }
+
+    @Test
+    public void testOverrideValue() throws Exception {
+        when(mockRegistry.histogram("A")).thenReturn(mockHistogram);
+        endpoint.expectedMessageCount(1);
+        producer.sendBodyAndHeader(new Object(), HEADER_HISTOGRAM_VALUE, 181L);
+        endpoint.assertIsSatisfied();
+        inOrder.verify(mockRegistry, times(1)).histogram("A");
+        inOrder.verify(mockHistogram, times(1)).update(181L);
+        inOrder.verifyNoMoreInteractions();
+    }
 }


Mime
View raw message