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 2018/08/28 10:01:48 UTC

[camel] branch master updated: camel-zipkin - Generate tracing identifiers on exchange begin if they… (#2494)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 25a4eaf  camel-zipkin - Generate tracing identifiers on exchange begin if they… (#2494)
25a4eaf is described below

commit 25a4eafcb4792b13ea2efac2c8ece94c21f827a6
Author: ramu11 <kr...@gmail.com>
AuthorDate: Tue Aug 28 15:31:43 2018 +0530

    camel-zipkin - Generate tracing identifiers on exchange begin if they… (#2494)
    
    * camel-zipkin - Generate tracing identifiers on exchange begin if they do not exist
    
    * CAMEL-12109:Generate tracing identifiers on exchange begin if they do not exist
---
 .../java/org/apache/camel/zipkin/ZipkinTracer.java | 22 +++---
 .../zipkin/ZipkinMultiServerNoClientTest.java      | 71 ++++++++++++++++++
 .../camel/zipkin/ZipkinServerMulticastTest.java    | 83 ++++++++++++++++++++++
 3 files changed, 168 insertions(+), 8 deletions(-)

diff --git a/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinTracer.java b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinTracer.java
index 2513f36..e26cae6 100644
--- a/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinTracer.java
+++ b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinTracer.java
@@ -31,6 +31,7 @@ import brave.propagation.Propagation.Setter;
 import brave.propagation.TraceContext;
 import brave.propagation.TraceContext.Extractor;
 import brave.propagation.TraceContext.Injector;
+import brave.propagation.TraceContextOrSamplingFlags;
 import brave.sampler.Sampler;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -625,8 +626,14 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
             state = new ZipkinState();
             exchange.setProperty(ZipkinState.KEY, state);
         }
-
-        Span span = brave.tracer().nextSpan(EXTRACTOR.extract(exchange.getIn()));
+        Span span = null;
+        TraceContextOrSamplingFlags sampleFlag = EXTRACTOR.extract(exchange.getIn());
+        if (ObjectHelper.isEmpty(sampleFlag)) {
+            span = brave.tracer().nextSpan();
+            INJECTOR.inject(span.context(), exchange.getIn()); 
+        } else {
+            span = brave.tracer().nextSpan(sampleFlag);
+        }
         span.kind(Span.Kind.SERVER).start();
         ZipkinServerRequestAdapter parser = new ZipkinServerRequestAdapter(this, exchange);
         parser.onRequest(exchange, span.customizer());
@@ -734,13 +741,12 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
             // use route policy to track events when Camel a Camel route begins/end the lifecycle of an Exchange
             // these events corresponds to Zipkin server events
 
-            if (hasZipkinTraceId(exchange)) {
-                String serviceName = getServiceName(exchange, route.getEndpoint(), true, false);
-                Tracing brave = getTracing(serviceName);
-                if (brave != null) {
-                    serverRequest(brave, serviceName, exchange);
-                }
+            String serviceName = getServiceName(exchange, route.getEndpoint(), true, false);
+            Tracing brave = getTracing(serviceName);
+            if (brave != null) {
+                serverRequest(brave, serviceName, exchange);
             }
+          
         }
 
         // Report Server send after route has completed processing of the exchange.
diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinMultiServerNoClientTest.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinMultiServerNoClientTest.java
new file mode 100644
index 0000000..90bbcd2
--- /dev/null
+++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinMultiServerNoClientTest.java
@@ -0,0 +1,71 @@
+/**
+ * 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.zipkin;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+import zipkin2.reporter.Reporter;
+
+public class ZipkinMultiServerNoClientTest extends CamelTestSupport {
+    
+    private ZipkinTracer zipkin;
+
+    protected void setSpanReporter(ZipkinTracer zipkin) {
+        zipkin.setSpanReporter(Reporter.NOOP);
+    }
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        zipkin = new ZipkinTracer();
+        // we have one route as service
+        zipkin.addServerServiceMapping("seda:abc", "abc");
+        zipkin.addServerServiceMapping("seda:xyz", "xyz");
+        setSpanReporter(zipkin);
+        // attaching ourself to CamelContext
+        zipkin.init(context);
+        return context;
+    }
+    @Test
+    public void testZipkinRoute() throws Exception {
+        template.requestBody("direct:start", "Hello abc");
+    }
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("seda:abc");
+                
+                from("seda:abc").routeId("abc")
+                .log("routing at ${routeId}")
+                .multicast()
+                .to("seda:xyz")
+                .end()
+                .log("End of routing");
+                
+                from("seda:xyz").routeId("xyz")
+                .log("routing at ${routeId}")
+                .delay(simple("${random(1000,2000)}")); 
+            }
+        };
+    }
+}
+
+
diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinServerMulticastTest.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinServerMulticastTest.java
new file mode 100644
index 0000000..7ae2c93
--- /dev/null
+++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinServerMulticastTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.zipkin;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+import zipkin2.reporter.Reporter;
+
+public class ZipkinServerMulticastTest  extends CamelTestSupport {
+    
+    private ZipkinTracer zipkin;
+
+    protected void setSpanReporter(ZipkinTracer zipkin) {
+        zipkin.setSpanReporter(Reporter.NOOP);
+    }
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        zipkin = new ZipkinTracer();
+        // we have one route as service
+        zipkin.addClientServiceMapping("seda:abc", "abc");
+        zipkin.addServerServiceMapping("seda:xyz", "xyz");
+        zipkin.addServerServiceMapping("seda:cat", "cat");
+        zipkin.addServerServiceMapping("seda:dog", "dog");
+        setSpanReporter(zipkin);
+        // attaching ourself to CamelContext
+        zipkin.init(context);
+        return context;
+    }
+    @Test
+    public void testZipkinRoute() throws Exception {
+        template.requestBody("direct:start", "Hello abc");
+    }
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("seda:abc");
+                
+                from("seda:abc").routeId("abc")
+                .log("routing at ${routeId}")
+                .multicast()
+                    .to("seda:xyz")
+                    .to("seda:cat")
+                    .to("seda:dog")
+                .end()
+                .log("End of routing");
+                
+                from("seda:xyz").routeId("xyz")
+                .log("routing at ${routeId}")
+                .delay(simple("${random(1000,2000)}")); 
+                
+                from("seda:cat").routeId("cat")
+                .log("routing at ${routeId}")
+                .delay(simple("${random(1000,2000)}")); 
+
+                from("seda:dog").routeId("b")
+                    .log("routing at ${routeId}")
+                    .delay(simple("${random(1000,2000)}"));
+            }
+        };
+    }
+}
+
+