You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/03/30 11:12:53 UTC

[2/4] camel git commit: CAMEL-9759: camel-zipkin - Instrument Camel. Work in progress.

CAMEL-9759: camel-zipkin - Instrument Camel. Work in progress.


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

Branch: refs/heads/master
Commit: 82ead72ba5f5006898453ed7104cebf77635bcef
Parents: 70633c4
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Mar 30 09:25:06 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Mar 30 11:12:28 2016 +0200

----------------------------------------------------------------------
 .../zipkin/starter/ZipkinAutoConfiguration.java |  8 +-
 .../starter/ZipkinConfigurationProperties.java  | 12 +++
 .../camel/zipkin/ZipkinEventNotifier.java       | 89 ++++++++++++--------
 examples/camel-example-zipkin/pom.xml           |  2 +-
 .../java/sample/camel/HelloCamelRouter.java     |  7 +-
 .../java/sample/camel/ReplyCamelRouter.java     | 39 +++++++++
 .../src/main/resources/application.properties   | 11 ++-
 7 files changed, 130 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinAutoConfiguration.java b/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinAutoConfiguration.java
index c640068..31cac0d 100644
--- a/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinAutoConfiguration.java
+++ b/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinAutoConfiguration.java
@@ -17,6 +17,7 @@
 package org.apache.camel.zipkin.starter;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.zipkin.ZipkinEventNotifier;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -39,7 +40,12 @@ public class ZipkinAutoConfiguration {
         notifier.setHostName(configurationProperties.getHostName());
         notifier.setPort(configurationProperties.getPort());
         notifier.setRate(configurationProperties.getRate());
-        notifier.setServiceName(configurationProperties.getServiceName());
+        if (ObjectHelper.isNotEmpty(configurationProperties.getServiceName())) {
+            notifier.setServiceName(configurationProperties.getServiceName());
+        }
+        if (ObjectHelper.isNotEmpty(configurationProperties.getExcludePattern())) {
+            notifier.addExcludePattern(configurationProperties.getExcludePattern());
+        }
         notifier.setIncludeMessageBody(configurationProperties.isIncludeMessageBody());
 
         // register the bean into CamelContext

http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinConfigurationProperties.java
----------------------------------------------------------------------
diff --git a/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinConfigurationProperties.java b/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinConfigurationProperties.java
index 0714819..5d9f4fa 100644
--- a/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinConfigurationProperties.java
+++ b/components/camel-zipkin-starter/src/main/java/org/apache/camel/zipkin/starter/ZipkinConfigurationProperties.java
@@ -26,6 +26,7 @@ public class ZipkinConfigurationProperties {
     private float rate = 1.0f;
     private boolean includeMessageBody;
     private String serviceName;
+    private String excludePattern;
 
     public String getHostName() {
         return hostName;
@@ -87,4 +88,15 @@ public class ZipkinConfigurationProperties {
     public void setServiceName(String serviceName) {
         this.serviceName = serviceName;
     }
+
+    public String getExcludePattern() {
+        return excludePattern;
+    }
+
+    /**
+     * Sets an exclude pattern that will disable tracing with zipkin for Camel messages that matches the pattern.
+     */
+    public void setExcludePattern(String excludePattern) {
+        this.excludePattern = excludePattern;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java
----------------------------------------------------------------------
diff --git a/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java
index 27286fa..3d91c6f 100644
--- a/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java
+++ b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java
@@ -285,35 +285,38 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
     private String getServiceName(Exchange exchange, Endpoint endpoint) {
         String answer = null;
 
-        String id = routeIdExpression().evaluate(exchange, String.class);
-        if (id != null) {
-            // exclude patterns take precedence
-            for (String pattern : excludePatterns) {
-                if (EndpointHelper.matchPattern(id, pattern)) {
-                    return null;
+        // endpoint takes precedence over route
+        if (endpoint != null) {
+            String url = endpoint.getEndpointUri();
+            if (url != null) {
+                // exclude patterns take precedence
+                for (String pattern : excludePatterns) {
+                    if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
+                        return null;
+                    }
                 }
-            }
-            for (Map.Entry<String, String> entry : serviceMappings.entrySet()) {
-                String pattern = entry.getKey();
-                if (EndpointHelper.matchPattern(id, pattern)) {
-                    answer = entry.getValue();
-                    break;
+                for (Map.Entry<String, String> entry : serviceMappings.entrySet()) {
+                    String pattern = entry.getKey();
+                    if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
+                        answer = entry.getValue();
+                        break;
+                    }
                 }
             }
         }
 
-        if (answer == null) {
-            id = exchange.getFromRouteId();
-            if (id != null) {
+        if (answer == null && exchange.getFromEndpoint() != null) {
+            String url = exchange.getFromEndpoint().getEndpointUri();
+            if (url != null) {
                 // exclude patterns take precedence
                 for (String pattern : excludePatterns) {
-                    if (EndpointHelper.matchPattern(id, pattern)) {
+                    if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
                         return null;
                     }
                 }
                 for (Map.Entry<String, String> entry : serviceMappings.entrySet()) {
                     String pattern = entry.getKey();
-                    if (EndpointHelper.matchPattern(id, pattern)) {
+                    if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
                         answer = entry.getValue();
                         break;
                     }
@@ -321,18 +324,19 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
             }
         }
 
-        if (answer == null && endpoint != null) {
-            String url = endpoint.getEndpointUri();
-            if (url != null) {
+        // route
+        if (answer == null) {
+            String id = routeIdExpression().evaluate(exchange, String.class);
+            if (id != null) {
                 // exclude patterns take precedence
                 for (String pattern : excludePatterns) {
-                    if (EndpointHelper.matchPattern(url, pattern)) {
+                    if (EndpointHelper.matchPattern(id, pattern)) {
                         return null;
                     }
                 }
                 for (Map.Entry<String, String> entry : serviceMappings.entrySet()) {
                     String pattern = entry.getKey();
-                    if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
+                    if (EndpointHelper.matchPattern(id, pattern)) {
                         answer = entry.getValue();
                         break;
                     }
@@ -340,18 +344,18 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
             }
         }
 
-        if (answer == null && exchange.getFromEndpoint() != null) {
-            String url = exchange.getFromEndpoint().getEndpointUri();
-            if (url != null) {
+        if (answer == null) {
+            String id = exchange.getFromRouteId();
+            if (id != null) {
                 // exclude patterns take precedence
                 for (String pattern : excludePatterns) {
-                    if (EndpointHelper.matchPattern(url, pattern)) {
+                    if (EndpointHelper.matchPattern(id, pattern)) {
                         return null;
                     }
                 }
                 for (Map.Entry<String, String> entry : serviceMappings.entrySet()) {
                     String pattern = entry.getKey();
-                    if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
+                    if (EndpointHelper.matchPattern(id, pattern)) {
                         answer = entry.getValue();
                         break;
                     }
@@ -453,7 +457,11 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
         event.getExchange().setProperty(key, span);
 
         if (log.isDebugEnabled()) {
-            log.debug("clientRequest\t[service={}, spanId={}]", serviceName, span != null ? span.getId() : "<null>");
+            String id = "<null>";
+            if (span != null) {
+                id = "" + span.getId();
+            }
+            log.debug("clientRequest\t[service={}, spanId={}]", serviceName, id);
         }
     }
 
@@ -466,8 +474,11 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
         binder.setCurrentSpan(null);
 
         if (log.isDebugEnabled()) {
-            // one space to align client vs server in the logs
-            log.debug("clientResponse\t[service={}, spanId={}]", serviceName, span != null ? span.getId() : "<null>");
+            String id = "<null>";
+            if (span != null) {
+                id = "" + span.getId();
+            }
+            log.debug("clientResponse\t[service={}, spanId={}]", serviceName, id);
         }
     }
 
@@ -479,7 +490,11 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
         event.getExchange().setProperty(key, span);
 
         if (log.isDebugEnabled()) {
-            log.debug("serverRequest\t[service={}, spanId={}]", serviceName, span != null ? span.getSpan().getId() : "<null>");
+            String id = "<null>";
+            if (span != null && span.getSpan() != null) {
+                id = "" + span.getSpan().getId();
+            }
+            log.debug("serverRequest\t[service={}, spanId={}]", serviceName, id);
         }
     }
 
@@ -492,7 +507,11 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
         binder.setCurrentSpan(null);
 
         if (log.isDebugEnabled()) {
-            log.debug("serverResponse\t[service={}, spanId={}]\t[status=exchangeCompleted]", serviceName, span != null ? span.getSpan().getId() : "<null>");
+            String id = "<null>";
+            if (span != null && span.getSpan() != null) {
+                id = "" + span.getSpan().getId();
+            }
+            log.debug("serverResponse\t[service={}, spanId={}]\t[status=exchangeCompleted]", serviceName, id);
         }
     }
 
@@ -505,7 +524,11 @@ public class ZipkinEventNotifier extends EventNotifierSupport implements Statefu
         binder.setCurrentSpan(null);
 
         if (log.isDebugEnabled()) {
-            log.debug("serverResponse[service={}, spanId={}]\t[status=exchangeFailed]", serviceName, span != null ? span.getSpan().getId() : "<null>");
+            String id = "<null>";
+            if (span != null && span.getSpan() != null) {
+                id = "" + span.getSpan().getId();
+            }
+            log.debug("serverResponse[service={}, spanId={}]\t[status=exchangeFailed]", serviceName, id);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/examples/camel-example-zipkin/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-zipkin/pom.xml b/examples/camel-example-zipkin/pom.xml
index 618d346..a965769 100644
--- a/examples/camel-example-zipkin/pom.xml
+++ b/examples/camel-example-zipkin/pom.xml
@@ -58,7 +58,7 @@
     <!-- spring-web -->
     <dependency>
       <groupId>org.springframework.boot</groupId>
-      <artifactId>spring-boot-starter-web</artifactId>
+      <artifactId>spring-boot-starter</artifactId>
     </dependency>
 
     <!-- camel -->

http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/examples/camel-example-zipkin/src/main/java/sample/camel/HelloCamelRouter.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-zipkin/src/main/java/sample/camel/HelloCamelRouter.java b/examples/camel-example-zipkin/src/main/java/sample/camel/HelloCamelRouter.java
index e1a82fa..17f9bf6 100644
--- a/examples/camel-example-zipkin/src/main/java/sample/camel/HelloCamelRouter.java
+++ b/examples/camel-example-zipkin/src/main/java/sample/camel/HelloCamelRouter.java
@@ -16,6 +16,7 @@
  */
 package sample.camel;
 
+import org.apache.camel.ExchangePattern;
 import org.apache.camel.builder.RouteBuilder;
 import org.springframework.stereotype.Component;
 
@@ -29,9 +30,11 @@ public class HelloCamelRouter extends RouteBuilder {
 
     @Override
     public void configure() throws Exception {
-        from("timer:hello?period={{timer.period}}")
+        from("timer:hello?period={{timer.period}}").routeId("client")
+                .setExchangePattern(ExchangePattern.InOut)
                 .transform(method("myBean", "saySomething"))
-                .to("stream:out");
+                .log("Saying ${body}")
+                .to("seda:hello");
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/examples/camel-example-zipkin/src/main/java/sample/camel/ReplyCamelRouter.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-zipkin/src/main/java/sample/camel/ReplyCamelRouter.java b/examples/camel-example-zipkin/src/main/java/sample/camel/ReplyCamelRouter.java
new file mode 100644
index 0000000..1cab297
--- /dev/null
+++ b/examples/camel-example-zipkin/src/main/java/sample/camel/ReplyCamelRouter.java
@@ -0,0 +1,39 @@
+/**
+ * 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 sample.camel;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.stereotype.Component;
+
+/**
+ * A simple Camel route that triggers from a timer and calls a bean and prints to system out.
+ * <p/>
+ * Use <tt>@Component</tt> to make Camel auto detect this route when starting.
+ */
+@Component
+public class ReplyCamelRouter extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("seda:hello").routeId("server")
+            .delay(simple("${random(1000,2000)}"))
+            .transform(simple("You said ${body}"))
+            .log("Replying with ${body}");
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/82ead72b/examples/camel-example-zipkin/src/main/resources/application.properties
----------------------------------------------------------------------
diff --git a/examples/camel-example-zipkin/src/main/resources/application.properties b/examples/camel-example-zipkin/src/main/resources/application.properties
index 6d46306..39f6bd5 100644
--- a/examples/camel-example-zipkin/src/main/resources/application.properties
+++ b/examples/camel-example-zipkin/src/main/resources/application.properties
@@ -16,15 +16,22 @@
 #
 
 # the name of Camel
-camel.springboot.name = ZipkinCamel
+camel.springboot.name=ZipkinCamel
+camel.springboot.main-run-controller=true
 
 # configure zipkin
 camel.zipkin.host-name=192.168.99.100
 camel.zipkin.port=9410
 
 camel.zipkin.service-name=hello
+# the timer is just to trigger new requests (
+camel.zipkin.exclude-pattern=timer:hello*
+# include the message body in the zipkin traces
 camel.zipkin.include-message-body=true
 
+# logging to see the zipkin client/server events and span-ids
+logging.level.org.apache.camel.zipkin: DEBUG
+
 # properties used in the Camel route and beans
 # --------------------------------------------
 
@@ -33,3 +40,5 @@ greeting = Hello World
 
 # how often to trigger the timer
 timer.period = 2000
+
+