camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/3] camel git commit: Provide implementation of Apache Camel Thread Pool Factory SPI that works with Dropwizard Metrics.
Date Sun, 04 Sep 2016 07:34:51 GMT
Repository: camel
Updated Branches:
  refs/heads/master 108275ac0 -> 47ee70851


Provide implementation of Apache Camel Thread Pool Factory SPI that works with Dropwizard
Metrics.


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

Branch: refs/heads/master
Commit: ccf4425fcf2434693a25b27c1a5a02579b43ec3f
Parents: 108275a
Author: Derek Bassett <pabassett@gmail.com>
Authored: Fri Sep 2 17:02:36 2016 -0600
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Sep 4 09:28:08 2016 +0200

----------------------------------------------------------------------
 .../src/main/docs/metrics-component.adoc        |  11 ++
 .../spi/InstrumentedThreadPoolFactory.java      |  51 +++++++++
 .../spi/InstrumentedThreadPoolFactoryTest.java  | 109 +++++++++++++++++++
 3 files changed, 171 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ccf4425f/components/camel-metrics/src/main/docs/metrics-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/main/docs/metrics-component.adoc b/components/camel-metrics/src/main/docs/metrics-component.adoc
index b05f500..80f63d5 100644
--- a/components/camel-metrics/src/main/docs/metrics-component.adoc
+++ b/components/camel-metrics/src/main/docs/metrics-component.adoc
@@ -561,6 +561,17 @@ String json = service.dumpStatisticsAsJson();
 And the JMX API the MBean is registered in the `type=services` tree
 with `name=MetricsMessageHistoryService`.
 
+[[MetricsComponent-InstrumentedThreadPoolFactory]]
+InstrumentedThreadPoolFactory
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+*Available as of Camel 2.18*
+
+This factory allows you to gather performance information about Camel Thread Pools by injecting
a InstrumentedThreadPoolFactory
+which collects information from inside of Camel.
+See more details at link:advanced-configuration-of-camelcontext-using-spring.html[Advanced
configuration of CamelContext using Spring]
+
+
 [[MetricsComponent-SeeAlso]]
 See Also
 ^^^^^^^^

http://git-wip-us.apache.org/repos/asf/camel/blob/ccf4425f/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactory.java
b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactory.java
new file mode 100644
index 0000000..09d3e9c
--- /dev/null
+++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactory.java
@@ -0,0 +1,51 @@
+package org.apache.camel.component.metrics.spi;
+
+import com.codahale.metrics.InstrumentedExecutorService;
+import com.codahale.metrics.InstrumentedScheduledExecutorService;
+import com.codahale.metrics.MetricRegistry;
+import org.apache.camel.impl.DefaultThreadPoolFactory;
+import org.apache.camel.spi.ThreadPoolFactory;
+import org.apache.camel.spi.ThreadPoolProfile;
+import org.apache.camel.util.ObjectHelper;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * This implements a {@link ThreadPoolFactory} and generates an Instrumented versions of
ExecutorService used to
+ * monitor performance of each thread using Metrics.
+ */
+public class InstrumentedThreadPoolFactory implements ThreadPoolFactory {
+
+    private MetricRegistry metricRegistry;
+
+    private ThreadPoolFactory threadPoolFactory;
+
+    public InstrumentedThreadPoolFactory(MetricRegistry metricRegistry) {
+        this(metricRegistry, new DefaultThreadPoolFactory());
+    }
+
+    public InstrumentedThreadPoolFactory(MetricRegistry metricRegistry, ThreadPoolFactory
threadPoolFactory) {
+        ObjectHelper.notNull(metricRegistry, "metricRegistry", this);
+        ObjectHelper.notNull(threadPoolFactory, "threadPoolFactory", this);
+        this.metricRegistry = metricRegistry;
+        this.threadPoolFactory = threadPoolFactory;
+    }
+
+    @Override
+    public ExecutorService newCachedThreadPool(ThreadFactory threadFactory){
+        return new InstrumentedExecutorService(threadPoolFactory.newCachedThreadPool(threadFactory),
metricRegistry);
+    }
+
+    @Override
+    public ExecutorService newThreadPool(ThreadPoolProfile profile, ThreadFactory threadFactory)
{
+        return new InstrumentedExecutorService(threadPoolFactory.newThreadPool(profile, threadFactory),
metricRegistry, profile.getId());
+    }
+
+    @Override
+    public ScheduledExecutorService newScheduledThreadPool(ThreadPoolProfile profile, ThreadFactory
threadFactory) {
+        return new InstrumentedScheduledExecutorService(threadPoolFactory.newScheduledThreadPool(profile,
threadFactory), metricRegistry, profile.getId());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ccf4425f/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactoryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactoryTest.java
b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactoryTest.java
new file mode 100644
index 0000000..6c16b0c
--- /dev/null
+++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/spi/InstrumentedThreadPoolFactoryTest.java
@@ -0,0 +1,109 @@
+package org.apache.camel.component.metrics.spi;
+
+import com.codahale.metrics.InstrumentedExecutorService;
+import com.codahale.metrics.InstrumentedScheduledExecutorService;
+import com.codahale.metrics.MetricRegistry;
+import org.apache.camel.ThreadPoolRejectedPolicy;
+import org.apache.camel.spi.ThreadPoolFactory;
+import org.apache.camel.spi.ThreadPoolProfile;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.times;
+
+@RunWith(MockitoJUnitRunner.class)
+public class InstrumentedThreadPoolFactoryTest {
+
+    private static final String METRICS_NAME = "metrics.name";
+
+    @Mock
+    private MetricRegistry registry;
+
+    @Mock
+    private ThreadPoolFactory threadPoolFactory;
+
+    @Mock
+    private ThreadFactory threadFactory;
+
+    private ThreadPoolProfile profile;
+
+    private InstrumentedThreadPoolFactory instrumentedThreadPoolFactory;
+
+    private InOrder inOrder;
+
+    @Before
+    public void setUp() throws Exception {
+        profile = new ThreadPoolProfile(METRICS_NAME);
+        profile.setDefaultProfile(false);
+        profile.setMaxPoolSize(10);
+        profile.setMaxQueueSize(1000);
+        profile.setPoolSize(5);
+        profile.setKeepAliveTime(5L);
+        profile.setTimeUnit(TimeUnit.SECONDS);
+        profile.setRejectedPolicy(ThreadPoolRejectedPolicy.CallerRuns);
+
+        instrumentedThreadPoolFactory = new InstrumentedThreadPoolFactory(registry, threadPoolFactory);
+
+        inOrder = Mockito.inOrder(registry);
+    }
+
+    @Test
+    public void testNewCacheThreadPool() throws Exception {
+        final ExecutorService executorService = instrumentedThreadPoolFactory.newCachedThreadPool(threadFactory);
+        assertThat(executorService, is(notNullValue()));
+        assertThat(executorService, is(instanceOf(InstrumentedExecutorService.class)));
+
+        inOrder.verify(registry, times(1)).meter(anyString());
+        inOrder.verify(registry, times(1)).counter(anyString());
+        inOrder.verify(registry, times(1)).meter(anyString());
+        inOrder.verify(registry, times(1)).timer(anyString());
+        inOrder.verifyNoMoreInteractions();
+    }
+
+    @Test
+    public void testNewThreadPool() throws Exception {
+        final ExecutorService executorService = instrumentedThreadPoolFactory.newThreadPool(profile,
threadFactory);
+        assertThat(executorService, is(notNullValue()));
+        assertThat(executorService, is(instanceOf(InstrumentedExecutorService.class)));
+
+        inOrder.verify(registry, times(1)).meter(MetricRegistry.name(METRICS_NAME, new String[]{"submitted"}));
+        inOrder.verify(registry, times(1)).counter(MetricRegistry.name(METRICS_NAME, new
String[]{"running"}));
+        inOrder.verify(registry, times(1)).meter(MetricRegistry.name(METRICS_NAME, new String[]{"completed"}));
+        inOrder.verify(registry, times(1)).timer(MetricRegistry.name(METRICS_NAME, new String[]{"duration"}));
+        inOrder.verifyNoMoreInteractions();
+    }
+
+    @Test
+    public void testNewScheduledThreadPool() throws Exception {
+        final ScheduledExecutorService scheduledExecutorService = instrumentedThreadPoolFactory.newScheduledThreadPool(profile,
threadFactory);
+
+        assertThat(scheduledExecutorService, is(notNullValue()));
+        assertThat(scheduledExecutorService, is(instanceOf(InstrumentedScheduledExecutorService.class)));
+
+        inOrder.verify(registry, times(1)).meter(MetricRegistry.name(METRICS_NAME, new String[]{"submitted"}));
+        inOrder.verify(registry, times(1)).counter(MetricRegistry.name(METRICS_NAME, new
String[]{"running"}));
+        inOrder.verify(registry, times(1)).meter(MetricRegistry.name(METRICS_NAME, new String[]{"completed"}));
+        inOrder.verify(registry, times(1)).timer(MetricRegistry.name(METRICS_NAME, new String[]{"duration"}));
+        inOrder.verify(registry, times(1)).meter(MetricRegistry.name(METRICS_NAME, new String[]{"scheduled.once"}));
+        inOrder.verify(registry, times(1)).meter(MetricRegistry.name(METRICS_NAME, new String[]{"scheduled.repetitively"}));
+        inOrder.verify(registry, times(1)).counter(MetricRegistry.name(METRICS_NAME, new
String[]{"scheduled.overrun"}));
+        inOrder.verify(registry, times(1)).histogram(MetricRegistry.name(METRICS_NAME, new
String[]{"scheduled.percent-of-period"}));
+        inOrder.verifyNoMoreInteractions();
+    }
+
+}


Mime
View raw message