Repository: camel Updated Branches: refs/heads/master 20200df36 -> a92932a0d
spring-boot/graphite metrics example Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/813622af Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/813622af Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/813622af Branch: refs/heads/master Commit: 813622af422f772a80a92f356ba72666003ae6d8 Parents: 20200df Author: jonmcewen <jon_mce...@hotmail.com> Authored: Fri Oct 16 09:02:09 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Oct 17 10:46:19 2015 +0200 ---------------------------------------------------------------------- .../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 + 4 files changed, 235 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/813622af/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 new file mode 100644 index 0000000..2e23b44 --- /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. http://git-wip-us.apache.org/repos/asf/camel/blob/813622af/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 new file mode 100644 index 0000000..38e4bdf --- /dev/null +++ b/examples/camel-example-springboot-metrics/pom.xml @@ -0,0 +1,96 @@ +<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/813622af/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 new file mode 100644 index 0000000..81539d9 --- /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); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/813622af/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 new file mode 100644 index 0000000..000da7f --- /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