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/31 06:00:15 UTC

[camel] branch master updated: CAMEL-12721:camel-zipkin - Add support for easy enabling of logging integration (#2497)

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 510eb15  CAMEL-12721:camel-zipkin - Add support for easy enabling of logging integration (#2497)
510eb15 is described below

commit 510eb15608ec75b562468f3a4136fdb1aee3755f
Author: ramu11 <kr...@gmail.com>
AuthorDate: Fri Aug 31 11:30:09 2018 +0530

    CAMEL-12721:camel-zipkin - Add support for easy enabling of logging integration (#2497)
---
 components/camel-zipkin/pom.xml                    |  9 +++
 .../java/org/apache/camel/zipkin/ZipkinTracer.java | 78 +++++++++++++++-------
 .../camel/zipkin/ZipkinMDCScopeDecoratorTest.java  | 78 ++++++++++++++++++++++
 .../src/test/resources/log4j2.properties           |  6 +-
 4 files changed, 145 insertions(+), 26 deletions(-)

diff --git a/components/camel-zipkin/pom.xml b/components/camel-zipkin/pom.xml
index 2dc1ef9..80b4542 100644
--- a/components/camel-zipkin/pom.xml
+++ b/components/camel-zipkin/pom.xml
@@ -66,6 +66,15 @@
       <artifactId>brave</artifactId>
       <version>${brave-zipkin-version}</version>
     </dependency>
+    <dependency>
+	  <groupId>io.zipkin.brave</groupId>
+	  <artifactId>brave-context-slf4j</artifactId>
+	  <version>${brave-zipkin-version}</version>
+    </dependency>
+    <dependency>
+       <groupId>org.slf4j</groupId>
+       <artifactId>slf4j-api</artifactId>
+     </dependency>
 
     <!-- test dependencies -->
     <dependency>
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 e26cae6..7ee86f9 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
@@ -25,9 +25,11 @@ import java.util.Set;
 
 import brave.Span;
 import brave.Tracing;
+import brave.context.slf4j.MDCScopeDecorator;
 import brave.propagation.B3Propagation;
 import brave.propagation.Propagation.Getter;
 import brave.propagation.Propagation.Setter;
+import brave.propagation.ThreadLocalCurrentTraceContext;
 import brave.propagation.TraceContext;
 import brave.propagation.TraceContext.Extractor;
 import brave.propagation.TraceContext.Injector;
@@ -61,6 +63,7 @@ import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
 import zipkin2.reporter.AsyncReporter;
 import zipkin2.reporter.Reporter;
 import zipkin2.reporter.libthrift.LibthriftSender;
@@ -534,10 +537,21 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
     }
 
     private Tracing newTracing(String serviceName) {
-        return Tracing.newBuilder()
-            .localServiceName(serviceName)
-            .sampler(Sampler.create(rate))
-            .spanReporter(spanReporter).build();
+        Tracing brave = null;
+        if (camelContext.isUseMDCLogging()) {
+            brave = Tracing.newBuilder()
+                     .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
+                     .addScopeDecorator(MDCScopeDecorator.create()).build())        
+                     .localServiceName(serviceName)
+                     .sampler(Sampler.create(rate))
+                     .spanReporter(spanReporter).build();
+        } else {
+            brave = Tracing.newBuilder()
+                    .localServiceName(serviceName)
+                    .sampler(Sampler.create(rate))
+                    .spanReporter(spanReporter).build();
+        }
+        return brave;
     }
 
     private Tracing getTracing(String serviceName) {
@@ -578,12 +592,16 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
 
         // store span after request
         state.pushClientSpan(span);
-
+        TraceContext context = span.context();
+        String traceId = "" + context.traceIdString();
+        String spanId = "" + context.spanId();
+        String parentId = context.parentId() != null ? "" + context.parentId() : null;
+        if (camelContext.isUseMDCLogging()) {
+            MDC.put("traceId", traceId);
+            MDC.put("spanId", spanId);
+            MDC.put("parentId", parentId);
+        }
         if (LOG.isDebugEnabled()) {
-            TraceContext context = span.context();
-            String traceId = "" + context.traceIdString();
-            String spanId = "" + context.spanId();
-            String parentId = context.parentId() != null ? "" + context.parentId() : null;
             if (parentId != null) {
                 LOG.debug(String.format("clientRequest [service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId));
             } else {
@@ -604,12 +622,16 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
             ZipkinClientResponseAdaptor parser = new ZipkinClientResponseAdaptor(this, event.getEndpoint());
             parser.onResponse(event.getExchange(), span.customizer());
             span.finish();
-
+            TraceContext context = span.context();
+            String traceId = "" + context.traceIdString();
+            String spanId = "" + context.spanId();
+            String parentId = context.parentId() != null ? "" + context.parentId() : null;
+            if (camelContext.isUseMDCLogging()) {
+                MDC.put("traceId", traceId);
+                MDC.put("spanId", spanId);
+                MDC.put("parentId", parentId);
+            }
             if (LOG.isDebugEnabled()) {
-                TraceContext context = span.context();
-                String traceId = "" + context.traceIdString();
-                String spanId = "" + context.spanId();
-                String parentId = context.parentId() != null ? "" + context.parentId() : null;
                 if (parentId != null) {
                     LOG.debug(String.format("clientResponse[service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId));
                 } else {
@@ -640,12 +662,16 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
 
         // store span after request
         state.pushServerSpan(span);
-
+        TraceContext context = span.context();
+        String traceId = "" + context.traceIdString();
+        String spanId = "" + context.spanId();
+        String parentId = context.parentId() != null ? "" + context.parentId() : null;
+        if (camelContext.isUseMDCLogging()) {
+            MDC.put("traceId", traceId);
+            MDC.put("spanId", spanId);
+            MDC.put("parentId", parentId);
+        }
         if (LOG.isDebugEnabled()) {
-            TraceContext context = span.context();
-            String traceId = "" + context.traceIdString();
-            String spanId = "" + context.spanId();
-            String parentId = context.parentId() != null ? "" + context.parentId() : null;
             if (parentId != null) {
                 LOG.debug(String.format("serverRequest [service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId));
             } else {
@@ -668,12 +694,16 @@ public class ZipkinTracer extends ServiceSupport implements RoutePolicyFactory,
             ZipkinServerResponseAdapter parser = new ZipkinServerResponseAdapter(this, exchange);
             parser.onResponse(exchange, span.customizer());
             span.finish();
-
+            TraceContext context = span.context();
+            String traceId = "" + context.traceIdString();
+            String spanId = "" + context.spanId();
+            String parentId = context.parentId() != null ? "" + context.parentId() : null;
+            if (camelContext.isUseMDCLogging()) {
+                MDC.put("traceId", traceId);
+                MDC.put("spanId", spanId);
+                MDC.put("parentId", parentId);
+            }
             if (LOG.isDebugEnabled()) {
-                TraceContext context = span.context();
-                String traceId = "" + context.traceIdString();
-                String spanId = "" + context.spanId();
-                String parentId = context.parentId() != null ? "" + context.parentId() : null;
                 if (parentId != null) {
                     LOG.debug(String.format("serverResponse[service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId));
                 } else {
diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinMDCScopeDecoratorTest.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinMDCScopeDecoratorTest.java
new file mode 100644
index 0000000..8989df7
--- /dev/null
+++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/ZipkinMDCScopeDecoratorTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+import org.slf4j.MDC;
+
+import zipkin2.reporter.Reporter;
+
+public class ZipkinMDCScopeDecoratorTest 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 2 routes as services
+        zipkin.addClientServiceMapping("seda:cat", "cat");
+        zipkin.addServerServiceMapping("seda:cat", "cat");
+        // capture message body as well
+        zipkin.setIncludeMessageBody(true);
+        setSpanReporter(zipkin);
+        context.setUseMDCLogging(true);
+        // attaching ourself to CamelContext
+        zipkin.init(context);
+        return context;
+    }
+    @Test
+    public void testZipkinRoute() throws Exception {
+        template.requestBody("direct:start", "Camel say hello Cat");
+    }
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("seda:cat");
+
+                from("seda:cat").routeId("cat")
+                        .delay(simple("${random(1000,2000)}"))
+                        .setBody().constant("Cat says hello Dog")
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                assertNotNull(MDC.get("traceId"));
+                                assertNotNull(MDC.get("spanId"));
+                                assertNotNull(MDC.get("parentId"));
+                            }
+                        }); 
+            }
+        };
+    }
+}
+
+
diff --git a/components/camel-zipkin/src/test/resources/log4j2.properties b/components/camel-zipkin/src/test/resources/log4j2.properties
index c598515..0da40e4 100644
--- a/components/camel-zipkin/src/test/resources/log4j2.properties
+++ b/components/camel-zipkin/src/test/resources/log4j2.properties
@@ -19,12 +19,14 @@ appender.file.type = File
 appender.file.name = file
 appender.file.fileName = target/camel-zipkin-test.log
 appender.file.layout.type = PatternLayout
-appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+appender.file.layout.pattern = %d{ABSOLUTE} %-5p [%t] %C{2} (%F:%L) [%X{traceId}/%X{spanId}] - %m%n
 appender.out.type = Console
 appender.out.name = out
 appender.out.layout.type = PatternLayout
-appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+appender.out.layout.pattern = %d{ABSOLUTE} %-5p [%t] %C{2} (%F:%L) [%X{traceId}/%X{spanId}] - %m%n
 logger.zipkin.name = org.apache.camel.zipkin
 logger.zipkin.level = DEBUG
 rootLogger.level = INFO
 rootLogger.appenderRef.file.ref = file
+
+