From 8496f9705c2906424f40deb10a9d1215c1371c61 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Oct 2015 10:57:09 +0100 Subject: [PATCH 1/3] Added test for multicast, but passes when I was expecting fail --- ...tricsRoutePolicyMulticastSubRouteTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java new file mode 100644 index 0000000000000..3febfa0bbc01f --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java @@ -0,0 +1,86 @@ +/** + * 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.metrics.routepolicy; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; + +/** + * CAMEL-9226 - check metrics are counted correctly in multicast sub-routes + */ +public class MetricsRoutePolicyMulticastSubRouteTest extends CamelTestSupport { + + private MetricRegistry registry = new MetricRegistry(); + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + + MetricsRoutePolicyFactory factory = new MetricsRoutePolicyFactory(); + factory.setUseJmx(false); + factory.setMetricsRegistry(registry); + context.addRoutePolicyFactory(factory); + + return context; + } + + @Test + public void testMetricsRoutePolicy() throws Exception { + getMockEndpoint("mock:foo").expectedMessageCount(1); + getMockEndpoint("mock:bar").expectedMessageCount(1); + getMockEndpoint("mock:end").expectedMessageCount(1); + + template.sendBody("seda:multicast", "Hello World"); + + assertMockEndpointsSatisfied(); + + // there should be 3 names + assertEquals(3, registry.getNames().size()); + + // there should be 3 Counters + assertEquals(3, registry.getTimers().size()); + + for (Map.Entry timerEntry : registry.getTimers().entrySet()) { + String metricName = timerEntry.getKey(); + Timer timer = timerEntry.getValue(); + // each count should be 1 + assertEquals("Count is wrong for " + metricName, 1, timer.getCount()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").routeId("foo").to("mock:foo"); + + from("direct:bar").routeId("bar").to("mock:bar"); + + from("seda:multicast").routeId("multicast").multicast().to("direct:foo").to("direct:bar").end().to("mock:end"); + + } + }; + } +} From d6d2f67c378ac8a57efff7475cf2fdbff665ed07 Mon Sep 17 00:00:00 2001 From: jonmcewen Date: Thu, 15 Oct 2015 14:35:49 +0100 Subject: [PATCH 2/3] Correction to multicast route in unit test --- .../MetricsRoutePolicyMulticastSubRouteTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java index 3febfa0bbc01f..09fb38908c5e5 100644 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicyMulticastSubRouteTest.java @@ -48,10 +48,10 @@ protected CamelContext createCamelContext() throws Exception { @Test public void testMetricsRoutePolicy() throws Exception { getMockEndpoint("mock:foo").expectedMessageCount(1); - getMockEndpoint("mock:bar").expectedMessageCount(1); - getMockEndpoint("mock:end").expectedMessageCount(1); + getMockEndpoint("mock:bar1").expectedMessageCount(1); + getMockEndpoint("mock:bar2").expectedMessageCount(1); - template.sendBody("seda:multicast", "Hello World"); + template.sendBody("direct:multicast", "Hello World"); assertMockEndpointsSatisfied(); @@ -76,9 +76,9 @@ protected RouteBuilder createRouteBuilder() throws Exception { public void configure() throws Exception { from("direct:foo").routeId("foo").to("mock:foo"); - from("direct:bar").routeId("bar").to("mock:bar"); + from("direct:bar").routeId("bar").multicast().to("mock:bar1", "mock:bar2"); - from("seda:multicast").routeId("multicast").multicast().to("direct:foo").to("direct:bar").end().to("mock:end"); + from("direct:multicast").routeId("multicast").multicast().to("direct:foo", "direct:bar"); } }; From 20d9cda1772e105c21382e6aa351fbac78b27990 Mon Sep 17 00:00:00 2001 From: jonmcewen Date: Fri, 16 Oct 2015 09:02:09 +0100 Subject: [PATCH 3/3] spring-boot/graphite metrics example --- .../README.md | 18 +++ .../camel-example-springboot-metrics/pom.xml | 96 ++++++++++++++ .../springboot/metrics/Application.java | 119 ++++++++++++++++++ .../src/main/resources/application.properties | 2 + 4 files changed, 235 insertions(+) create mode 100644 examples/camel-example-springboot-metrics/README.md create mode 100644 examples/camel-example-springboot-metrics/pom.xml create mode 100644 examples/camel-example-springboot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java create mode 100644 examples/camel-example-springboot-metrics/src/main/resources/application.properties diff --git a/examples/camel-example-springboot-metrics/README.md b/examples/camel-example-springboot-metrics/README.md new file mode 100644 index 0000000000000..2e23b440ae12d --- /dev/null +++ b/examples/camel-example-springboot-metrics/README.md @@ -0,0 +1,18 @@ +# camel-spring-boot-metrics-example +This example sends Camel route metrics to Graphite from a Spring Boot app. + +Spring Boot auto-configures the `com.codahale.metrics.MetricRegistry`. See code comments in `Application.java` for further details. + +If you already have a Graphite server, make sure that UDP is enabled (set `ENABLE_UDP_LISTENER = True` in carbon.conf). + +If Graphite is not on your local machine, replace `localhost` in `Application.java` with the hostname or IP address of your Graphite server. + +If you want to use TCP instead of UDP, use `com.codahale.metrics.graphite.Graphite` instead of `com.codahale.metrics.graphite.GraphiteUDP`, as shown here: http://metrics.dropwizard.io/3.1.0/manual/graphite/ + +If you can't be bothered to set up a Graphite server right now, you can simulate it by running `nc -ul 2003` on Linux. If you don't have `nc`, use `yum search netcat` to find a suitable package to install (e.g. nmap-ncat.x86_64). + +When you're ready to try it: +`mvn clean install` +`java -jar target/camel-metrics-example-0.0.1-SNAPSHOT.jar` + +You will see logging from the "Fast" and "Slow" routes, and metrics will be sent to Graphite (or nc) every 5 seconds. diff --git a/examples/camel-example-springboot-metrics/pom.xml b/examples/camel-example-springboot-metrics/pom.xml new file mode 100644 index 0000000000000..38e4bdfe29b3d --- /dev/null +++ b/examples/camel-example-springboot-metrics/pom.xml @@ -0,0 +1,96 @@ + + 4.0.0 + org.apache.camel + camel-example-springboot-metrics + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-parent + 1.3.0.BUILD-SNAPSHOT + + + + + 1.8 + UTF-8 + org.apache.camel.example.springboot.metrics.Application + 2.16.0 + + + + + + + org.jolokia + jolokia-core + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + io.dropwizard.metrics + metrics-core + + + io.dropwizard.metrics + metrics-graphite + + + org.apache.camel + camel-metrics + ${camel.version} + + + org.apache.camel + camel-spring-boot + ${camel.version} + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-snapshots + http://repo.spring.io/snapshot + + + spring-milestones + http://repo.spring.io/milestone + + + + + + spring-snapshots + http://repo.spring.io/snapshot + + + spring-milestones + http://repo.spring.io/milestone + + + + + + + + + diff --git a/examples/camel-example-springboot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java b/examples/camel-example-springboot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java new file mode 100644 index 0000000000000..81539d9139986 --- /dev/null +++ b/examples/camel-example-springboot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java @@ -0,0 +1,119 @@ +/** + * 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.example.springboot.metrics; + +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.metrics.routepolicy.MetricsRoutePolicyFactory; +import org.apache.camel.spring.boot.CamelContextConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import com.codahale.metrics.MetricFilter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.graphite.GraphiteReporter; +import com.codahale.metrics.graphite.GraphiteSender; +import com.codahale.metrics.graphite.GraphiteUDP; + +/** + * A simple Spring Boot application, with a couple of timed camel routes + * configured with camel-metrics. Reports metrics to Graphite via + * dropwizard-metrics GraphiteUDP sender. Has standard spring-actuator endpoints + * such as /beans, /autoconfig, /metrics + */ +@SpringBootApplication +public class Application { + + private static final Logger log = LoggerFactory.getLogger(Application.class); + + @Autowired + private MetricRegistry metricRegistry; + + /** + * @param args no command line args required + */ + public static void main(String[] args) { + log.info(" *** STARTING CAMEL METRICS EXAMPLE APPLICATION ***"); + SpringApplication.run(Application.class, args); + + } + + /** + * Create reporter bean and tell Spring to call stop() when shutting down. + * UPD must be enabled in carbon.conf + * + * @return graphite reporter + */ + @Bean(destroyMethod = "stop") + public GraphiteReporter graphiteReporter() { + final GraphiteSender graphite = new GraphiteUDP(new InetSocketAddress("localhost", 2003)); + final GraphiteReporter reporter = GraphiteReporter.forRegistry(metricRegistry).prefixedWith("camel-spring-boot").convertRatesTo(TimeUnit.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS).filter(MetricFilter.ALL).build(graphite); + reporter.start(5, TimeUnit.SECONDS); + return reporter; + } + + /** + * @return timed route that logs output every 6 seconds + */ + @Bean + public RouteBuilder slowRoute() { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + from("timer://foo?period=6000&daemon=false").routeId("slow-route").setBody().constant("Slow hello world!").log("${body}"); + } + }; + } + + /** + * @return timed route that logs output every 2 seconds + */ + @Bean + public RouteBuilder fastRoute() { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + from("timer://foo?period=2000&daemon=false").routeId("fast-route").setBody().constant("Fast hello world!").log("${body}"); + } + }; + } + + @Bean + CamelContextConfiguration contextConfiguration() { + return new CamelContextConfiguration() { + + @Override + public void beforeApplicationStart(CamelContext context) { + log.info("Configuring camel metrics on all routes"); + MetricsRoutePolicyFactory fac = new MetricsRoutePolicyFactory(); + fac.setMetricsRegistry(metricRegistry); + context.addRoutePolicyFactory(fac); + } + }; + } + +} diff --git a/examples/camel-example-springboot-metrics/src/main/resources/application.properties b/examples/camel-example-springboot-metrics/src/main/resources/application.properties new file mode 100644 index 0000000000000..000da7f8b37a2 --- /dev/null +++ b/examples/camel-example-springboot-metrics/src/main/resources/application.properties @@ -0,0 +1,2 @@ +info.build.name=Example of sending camel metrics to Graphite +camel.springboot.name=camel-metrics-example