Fixes #645. Polished the example.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a92932a0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a92932a0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a92932a0 Branch: refs/heads/master Commit: a92932a0d351a48a38bb91bacb05d62856ac4f80 Parents: 813622a Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Oct 17 11:12:32 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Oct 17 11:12:32 2015 +0200 ---------------------------------------------------------------------- .../camel-example-spring-boot-metrics/README.md | 22 ++++ .../camel-example-spring-boot-metrics/pom.xml | 115 ++++++++++++++++++ .../example/springboot/metrics/Application.java | 118 ++++++++++++++++++ .../src/main/resources/application.properties | 2 + examples/camel-example-spring-boot/pom.xml | 5 +- .../camel-example-springboot-metrics/README.md | 18 --- .../camel-example-springboot-metrics/pom.xml | 96 --------------- .../example/springboot/metrics/Application.java | 119 ------------------- .../src/main/resources/application.properties | 2 - examples/pom.xml | 1 + 10 files changed, 261 insertions(+), 237 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-spring-boot-metrics/README.md ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-metrics/README.md b/examples/camel-example-spring-boot-metrics/README.md new file mode 100644 index 0000000..2e941f3 --- /dev/null +++ b/examples/camel-example-spring-boot-metrics/README.md @@ -0,0 +1,22 @@ +# 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-example-spring-boot-metrics.jar + +You will see logging from the "Fast" and "Slow" routes, and metrics will be sent to Graphite (or nc) every 5 seconds. http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-spring-boot-metrics/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-metrics/pom.xml b/examples/camel-example-spring-boot-metrics/pom.xml new file mode 100644 index 0000000..e8921ce --- /dev/null +++ b/examples/camel-example-spring-boot-metrics/pom.xml @@ -0,0 +1,115 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>examples</artifactId> + <version>2.17-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-metrics</artifactId> + <name>Camel :: Example :: Spring Boot Metrics</name> + <description>An example showing how to work with Camel and Spring Boot and report metrics to Graphite</description> + + <properties> + <spring.boot-version>${spring-boot-version}</spring.boot-version> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-dependencies</artifactId> + <version>${spring.boot-version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-boot</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-metrics</artifactId> + </dependency> + + <!-- web --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + + <!-- operations --> + <dependency> + <groupId>org.jolokia</groupId> + <artifactId>jolokia-core</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + + <!-- metrics --> + <dependency> + <groupId>io.dropwizard.metrics</groupId> + <artifactId>metrics-core</artifactId> + </dependency> + <dependency> + <groupId>io.dropwizard.metrics</groupId> + <artifactId>metrics-graphite</artifactId> + </dependency> + + </dependencies> + + <build> + + <!-- we do not want version in the JAR name --> + <finalName>${project.artifactId}</finalName> + + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring.boot-version}</version> + <configuration> + <mainClass>org.apache.camel.example.springboot.metrics.Application</mainClass> + </configuration> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> + + + + http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java b/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java new file mode 100644 index 0000000..9873a54 --- /dev/null +++ b/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java @@ -0,0 +1,118 @@ +/** + * 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 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; +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; + +/** + * 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); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-spring-boot-metrics/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-metrics/src/main/resources/application.properties b/examples/camel-example-spring-boot-metrics/src/main/resources/application.properties new file mode 100644 index 0000000..1f2aa04 --- /dev/null +++ b/examples/camel-example-spring-boot-metrics/src/main/resources/application.properties @@ -0,0 +1,2 @@ +info.build.name=Example of sending camel metrics to Graphite +camel.springboot.name=camel-example-spring-boot-metrics http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-spring-boot/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot/pom.xml b/examples/camel-example-spring-boot/pom.xml index 8f3e6c7..adcecc8 100755 --- a/examples/camel-example-spring-boot/pom.xml +++ b/examples/camel-example-spring-boot/pom.xml @@ -55,7 +55,7 @@ <artifactId>spring-boot-starter-web</artifactId> </dependency> - <!-- Operations- --> + <!-- operations --> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> @@ -65,7 +65,7 @@ <artifactId>spring-boot-starter-actuator</artifactId> </dependency> - <!-- Testing --> + <!-- testing --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> @@ -88,6 +88,7 @@ <attachClasses>true</attachClasses> </configuration> </plugin> + <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-springboot-metrics/README.md ---------------------------------------------------------------------- diff --git a/examples/camel-example-springboot-metrics/README.md b/examples/camel-example-springboot-metrics/README.md deleted file mode 100644 index 2e23b44..0000000 --- a/examples/camel-example-springboot-metrics/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# 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. http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-springboot-metrics/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-springboot-metrics/pom.xml b/examples/camel-example-springboot-metrics/pom.xml deleted file mode 100644 index 38e4bdf..0000000 --- a/examples/camel-example-springboot-metrics/pom.xml +++ /dev/null @@ -1,96 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <groupId>org.apache.camel</groupId> - <artifactId>camel-example-springboot-metrics</artifactId> - <version>0.0.1-SNAPSHOT</version> - - <parent> - <!-- Spring Boot supplies the version numbers for dependencies, and some - other default config --> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-parent</artifactId> - <version>1.3.0.BUILD-SNAPSHOT</version> - <relativePath></relativePath> - </parent> - - <properties> - <java.version>1.8</java.version> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <start-class>org.apache.camel.example.springboot.metrics.Application</start-class> - <camel.version>2.16.0</camel.version> - </properties> - - - - <dependencies> - <dependency> - <groupId>org.jolokia</groupId> - <artifactId>jolokia-core</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-web</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-actuator</artifactId> - </dependency> - <dependency> - <groupId>io.dropwizard.metrics</groupId> - <artifactId>metrics-core</artifactId> - </dependency> - <dependency> - <groupId>io.dropwizard.metrics</groupId> - <artifactId>metrics-graphite</artifactId> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-metrics</artifactId> - <version>${camel.version}</version> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-spring-boot</artifactId> - <version>${camel.version}</version> - </dependency> - - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> - </plugin> - </plugins> - </build> - - <pluginRepositories> - <pluginRepository> - <id>spring-snapshots</id> - <url>http://repo.spring.io/snapshot</url> - </pluginRepository> - <pluginRepository> - <id>spring-milestones</id> - <url>http://repo.spring.io/milestone</url> - </pluginRepository> - </pluginRepositories> - - <repositories> - <repository> - <id>spring-snapshots</id> - <url>http://repo.spring.io/snapshot</url> - </repository> - <repository> - <id>spring-milestones</id> - <url>http://repo.spring.io/milestone</url> - </repository> - </repositories> - - -</project> - - - - http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-springboot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java ---------------------------------------------------------------------- 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 deleted file mode 100644 index 81539d9..0000000 --- a/examples/camel-example-springboot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * 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); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/camel-example-springboot-metrics/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-springboot-metrics/src/main/resources/application.properties b/examples/camel-example-springboot-metrics/src/main/resources/application.properties deleted file mode 100644 index 000da7f..0000000 --- a/examples/camel-example-springboot-metrics/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -info.build.name=Example of sending camel metrics to Graphite -camel.springboot.name=camel-metrics-example http://git-wip-us.apache.org/repos/asf/camel/blob/a92932a0/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index 71da860..eff394a 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -71,6 +71,7 @@ <module>camel-example-splunk</module> <module>camel-example-spring</module> <module>camel-example-spring-boot</module> + <module>camel-example-spring-boot-metrics</module> <module>camel-example-spring-javaconfig</module> <module>camel-example-spring-jms</module> <module>camel-example-spring-ws</module>