You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2017/03/11 20:14:04 UTC

cxf git commit: CXF-7129: Provide an optional HTrace Logback span converter to enrich log records with tracing details.

Repository: cxf
Updated Branches:
  refs/heads/master e90f3dd64 -> 7dbb31aed


CXF-7129: Provide an optional HTrace Logback span converter to enrich log records with tracing details.


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/7dbb31ae
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7dbb31ae
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7dbb31ae

Branch: refs/heads/master
Commit: 7dbb31aed4bfa28bd170d6170d76ba4d7f43e7de
Parents: e90f3dd
Author: reta <dr...@gmail.com>
Authored: Sat Mar 11 15:13:38 2017 -0500
Committer: reta <dr...@gmail.com>
Committed: Sat Mar 11 15:13:38 2017 -0500

----------------------------------------------------------------------
 integration/tracing/tracing-htrace/pom.xml      |  5 ++
 .../htrace/ext/LogbackSpanConverter.java        | 72 ++++++++++++++++++++
 2 files changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/7dbb31ae/integration/tracing/tracing-htrace/pom.xml
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/pom.xml b/integration/tracing/tracing-htrace/pom.xml
index 2d436e2..3a342e4 100644
--- a/integration/tracing/tracing-htrace/pom.xml
+++ b/integration/tracing/tracing-htrace/pom.xml
@@ -64,6 +64,11 @@
             <artifactId>htrace-core4</artifactId>
         </dependency>
         <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-jdk14</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/cxf/blob/7dbb31ae/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LogbackSpanConverter.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LogbackSpanConverter.java b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LogbackSpanConverter.java
new file mode 100644
index 0000000..ec8f772
--- /dev/null
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LogbackSpanConverter.java
@@ -0,0 +1,72 @@
+/**
+ * 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.cxf.tracing.htrace.ext;
+
+import org.apache.htrace.core.Span;
+import org.apache.htrace.core.Tracer;
+
+import ch.qos.logback.classic.pattern.ClassicConverter;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+
+/**
+ * Logback conversion rule implementation to enrich log records with tracing details like spanId and tracerId. 
+ * For example, here is sample logback.xml configuration snippet:
+ * 
+ *  <conversionRule conversionWord="trace" converterClass="org.apache.cxf.tracing.htrace.ext.LogbackSpanConverter" />
+ * 
+ *  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ *      <encoder>
+ *          <pattern>[%level] [%trace] %d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} %msg%n</pattern>
+ *      </encoder>
+ *  </appender>
+ * 
+ * Which produces the following sample output:
+ *  
+ *  [INFO] [spanId: -, tracerId: -] 2017-03-11 14:40:13.603 org.eclipse.jetty.server.Server Started @2731ms
+ *  [INFO] [spanId: 6d3e0d975d4c883cce12aee1fd8f3e7e, tracerId: tracer-server/192.168.0.101] 2017-03-11 14:40:24.013 
+ *     com.example.rs.PeopleRestService Getting all employees
+ *  [INFO] [spanId: 6d3e0d975d4c883c7592f4c2317dec22, tracerId: tracer-server/192.168.0.101] 2017-03-11 14:40:28.017 
+ *     com.example.rs.PeopleRestService Looking up manager in the DB database
+ *
+ */
+public class LogbackSpanConverter extends ClassicConverter {
+    private static final String TRACER_ID = "tracerId";
+    private static final String SPAN_ID = "spanId";
+    private static final String EMPTY_TRACE = String.format("%s: -, %s: -", SPAN_ID, TRACER_ID);
+
+    @Override
+    public String convert(ILoggingEvent event) {
+        final Span currentSpan = Tracer.getCurrentSpan();
+        
+        if (currentSpan != null) {
+            return new StringBuilder()
+                .append(SPAN_ID)
+                .append(": ")
+                .append(currentSpan.getSpanId())
+                .append(", ")
+                .append(TRACER_ID)
+                .append(": ")
+                .append(currentSpan.getTracerId())
+                .toString();
+        }
+        
+        return EMPTY_TRACE;
+    }
+}