You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/08/24 09:36:28 UTC

[7/7] camel git commit: CAMEL-11645 - Add pattern exclusion mechanism similar to the one provided by the camel-zipkin component

CAMEL-11645 - Add pattern exclusion mechanism similar to the one provided by the camel-zipkin component


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

Branch: refs/heads/master
Commit: 5481b9cf83af2123006a671fb92b06746aacabb4
Parents: eeca67c
Author: Gary Brown <ga...@brownuk.com>
Authored: Tue Aug 22 16:58:17 2017 +0100
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Thu Aug 24 11:33:06 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/opentracing.adoc              | 13 ++++
 .../camel/opentracing/OpenTracingTracer.java    | 42 +++++++++++-
 .../CamelOpenTracingTestSupport.java            |  7 ++
 .../opentracing/TwoServiceWithExcludeTest.java  | 69 ++++++++++++++++++++
 .../starter/OpenTracingAutoConfiguration.java   |  3 +
 .../OpenTracingConfigurationProperties.java     | 15 ++++-
 6 files changed, 146 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5481b9cf/components/camel-opentracing/src/main/docs/opentracing.adoc
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/docs/opentracing.adoc b/components/camel-opentracing/src/main/docs/opentracing.adoc
index f753880..f7e0a8b 100644
--- a/components/camel-opentracing/src/main/docs/opentracing.adoc
+++ b/components/camel-opentracing/src/main/docs/opentracing.adoc
@@ -14,6 +14,19 @@ See the http://opentracing.io/[OpenTracing] website for a list of supported trac
 
 ### Configuration
 
+The configuration properties for the OpenTracing tracer are:
+
+[width="100%",cols="10%,10%,80%",options="header",]
+|=======================================================================
+|Option |Default |Description
+
+|excludePatterns |  | Sets exclude pattern(s) that will disable tracing for Camel
+messages that matches the pattern. The content is a Set<String> where the key is a pattern. The pattern
+uses the rules from link:intercept.html[Intercept].
+
+|=======================================================================
+
+
 There are three ways in which an OpenTracing tracer can be configured to provide distributed tracing for a Camel application:
 
 #### Explicit

http://git-wip-us.apache.org/repos/asf/camel/blob/5481b9cf/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java
index 2cafa59..561c9f9 100644
--- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java
@@ -19,6 +19,7 @@ package org.apache.camel.opentracing;
 import java.net.URI;
 import java.util.EventObject;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
@@ -49,6 +50,7 @@ import org.apache.camel.support.EventNotifierSupport;
 import org.apache.camel.support.RoutePolicySupport;
 import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.CamelLogger;
+import org.apache.camel.util.EndpointHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
@@ -72,6 +74,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
     private final OpenTracingLogListener logListener = new OpenTracingLogListener();
     private Tracer tracer;
     private CamelContext camelContext;
+    private Set<String> excludePatterns = new HashSet<>();
 
     static {
         ServiceLoader.load(SpanDecorator.class).forEach(d -> {
@@ -121,6 +124,23 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
         this.camelContext = camelContext;
     }
 
+    public Set<String> getExcludePatterns() {
+        return excludePatterns;
+    }
+
+    public void setExcludePatterns(Set<String> excludePatterns) {
+        this.excludePatterns = excludePatterns;
+    }
+
+    /**
+     * Adds an exclude pattern that will disable tracing for Camel messages that matches the pattern.
+     *
+     * @param pattern  the pattern such as route id, endpoint url
+     */
+    public void addExcludePattern(String pattern) {
+        excludePatterns.add(pattern);
+    }
+
     public Tracer getTracer() {
         return tracer;
     }
@@ -176,6 +196,18 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
         return sd;
     }
 
+    private boolean isExcluded(Exchange exchange, Endpoint endpoint) {
+        String url = endpoint.getEndpointUri();
+        if (url != null && !excludePatterns.isEmpty()) {
+            for (String pattern : excludePatterns) {
+                if (EndpointHelper.matchEndpoint(exchange.getContext(), url, pattern)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     private final class OpenTracingEventNotifier extends EventNotifierSupport {
 
         @Override
@@ -184,7 +216,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
                 if (event instanceof ExchangeSendingEvent) {
                     ExchangeSendingEvent ese = (ExchangeSendingEvent) event;
                     SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
-                    if (!sd.newSpan()) {
+                    if (!sd.newSpan() || isExcluded(ese.getExchange(), ese.getEndpoint())) {
                         return;
                     }
                     Span parent = ActiveSpanManager.getSpan(ese.getExchange());
@@ -206,7 +238,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
                 } else if (event instanceof ExchangeSentEvent) {
                     ExchangeSentEvent ese = (ExchangeSentEvent) event;
                     SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
-                    if (!sd.newSpan()) {
+                    if (!sd.newSpan() || isExcluded(ese.getExchange(), ese.getEndpoint())) {
                         return;
                     }
                     Span span = ActiveSpanManager.getSpan(ese.getExchange());
@@ -247,6 +279,9 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
         @Override
         public void onExchangeBegin(Route route, Exchange exchange) {
             try {
+                if (isExcluded(exchange, route.getEndpoint())) {
+                    return;
+                }
                 SpanDecorator sd = getSpanDecorator(route.getEndpoint());
                 Span span = tracer.buildSpan(sd.getOperationName(exchange, route.getEndpoint()))
                     .asChildOf(tracer.extract(Format.Builtin.TEXT_MAP,
@@ -267,6 +302,9 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
         @Override
         public void onExchangeDone(Route route, Exchange exchange) {
             try {
+                if (isExcluded(exchange, route.getEndpoint())) {
+                    return;
+                }
                 Span span = ActiveSpanManager.getSpan(exchange);
                 if (span != null) {
                     if (LOG.isTraceEnabled()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/5481b9cf/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/CamelOpenTracingTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/CamelOpenTracingTestSupport.java b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/CamelOpenTracingTestSupport.java
index 4b87ab5..d56ed34 100644
--- a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/CamelOpenTracingTestSupport.java
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/CamelOpenTracingTestSupport.java
@@ -20,8 +20,10 @@ import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 import io.opentracing.Span;
@@ -55,6 +57,7 @@ public class CamelOpenTracingTestSupport extends CamelTestSupport {
 
         OpenTracingTracer ottracer = new OpenTracingTracer();
         ottracer.setTracer(tracer);
+        ottracer.setExcludePatterns(getExcludePatterns());
 
         ottracer.init(context);
 
@@ -65,6 +68,10 @@ public class CamelOpenTracingTestSupport extends CamelTestSupport {
         return tracer;
     }
 
+    protected Set<String> getExcludePatterns() {
+        return new HashSet<String>();
+    }
+
     protected void verify() {
         verify(false);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/5481b9cf/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/TwoServiceWithExcludeTest.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/TwoServiceWithExcludeTest.java b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/TwoServiceWithExcludeTest.java
new file mode 100644
index 0000000..a24f950
--- /dev/null
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/TwoServiceWithExcludeTest.java
@@ -0,0 +1,69 @@
+/**
+ * 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.opentracing;
+
+import java.util.Collections;
+import java.util.Set;
+
+import io.opentracing.tag.Tags;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class TwoServiceWithExcludeTest extends CamelOpenTracingTestSupport {
+
+    private static SpanTestData[] testdata = {
+        new SpanTestData().setLabel("ServiceA server").setUri("direct://ServiceA").setOperation("ServiceA")
+            .setKind(Tags.SPAN_KIND_SERVER).setParentId(1),
+        new SpanTestData().setLabel("ServiceA client").setUri("direct://ServiceA").setOperation("ServiceA")
+            .setKind(Tags.SPAN_KIND_CLIENT)
+    };
+
+    public TwoServiceWithExcludeTest() {
+        super(testdata);
+    }
+
+    @Override
+    protected Set<String> getExcludePatterns() {
+        return Collections.singleton("direct:ServiceB");
+    }
+
+    @Test
+    public void testRoute() throws Exception {
+        template.requestBody("direct:ServiceA", "Hello");
+
+        verify();
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:ServiceA")
+                    .log("ServiceA has been called")
+                    .delay(simple("${random(1000,2000)}"))
+                    .to("direct:ServiceB");
+
+                from("direct:ServiceB")
+                    .log("ServiceB has been called")
+                    .delay(simple("${random(0,500)}"));
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/5481b9cf/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingAutoConfiguration.java
index ec25984..76e5455 100644
--- a/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingAutoConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingAutoConfiguration.java
@@ -44,6 +44,9 @@ public class OpenTracingAutoConfiguration {
         if (tracer != null) {
             ottracer.setTracer(tracer);
         }
+        if (config.getExcludePatterns() != null) {
+            ottracer.setExcludePatterns(config.getExcludePatterns());
+        }
         ottracer.init(camelContext);
 
         return ottracer;

http://git-wip-us.apache.org/repos/asf/camel/blob/5481b9cf/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingConfigurationProperties.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingConfigurationProperties.java b/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingConfigurationProperties.java
index 6c82934..8add6b9 100644
--- a/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingConfigurationProperties.java
+++ b/platforms/spring-boot/components-starter/camel-opentracing-starter/src/main/java/org/apache/camel/opentracing/starter/OpenTracingConfigurationProperties.java
@@ -16,11 +16,24 @@
  */
 package org.apache.camel.opentracing.starter;
 
+import java.util.Set;
+
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
 @ConfigurationProperties(prefix = "camel.opentracing")
 public class OpenTracingConfigurationProperties {
 
-    // Placeholder for configuration properties
+    /**
+     * Sets exclude pattern(s) that will disable tracing for Camel messages that matches the pattern.
+     */
+    private Set<String> excludePatterns;
+
+    public Set<String> getExcludePatterns() {
+        return excludePatterns;
+    }
+
+    public void setExcludePatterns(Set<String> excludePatterns) {
+        this.excludePatterns = excludePatterns;
+    }
 
 }