cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cxf git commit: CXF-7129: Provide an optional HTrace Logging receiver, prettified the default output
Date Sat, 25 Feb 2017 19:32:48 GMT
Repository: cxf
Updated Branches:
  refs/heads/master e7c9896f7 -> ceb04f964


CXF-7129: Provide an optional HTrace Logging receiver, prettified the default output


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

Branch: refs/heads/master
Commit: ceb04f964e0772389b7a773114c282769ba913a3
Parents: e7c9896
Author: reta <drreta@gmail.com>
Authored: Sat Feb 25 14:32:25 2017 -0500
Committer: reta <drreta@gmail.com>
Committed: Sat Feb 25 14:32:25 2017 -0500

----------------------------------------------------------------------
 .../tracing/htrace/ext/LoggingSpanReceiver.java | 114 ++++++++++++++++++-
 1 file changed, 113 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ceb04f96/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LoggingSpanReceiver.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LoggingSpanReceiver.java
b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LoggingSpanReceiver.java
index 68845be..332b385 100644
--- a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LoggingSpanReceiver.java
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/ext/LoggingSpanReceiver.java
@@ -20,14 +20,23 @@
 package org.apache.cxf.tracing.htrace.ext;
 
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Map;
+import java.util.function.Function;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.htrace.core.HTraceConfiguration;
 import org.apache.htrace.core.Span;
 import org.apache.htrace.core.SpanReceiver;
+import org.apache.htrace.core.TimelineAnnotation;
 
+/**
+ * Span receiver implementation which outputs spans into logs. 
+ */
 public class LoggingSpanReceiver extends SpanReceiver {
     public static final String LOG_LEVEL_KEY = "cxf.log.level";
     public static final String LOG_LEVEL_ERROR = Level.SEVERE.getName();
@@ -44,10 +53,113 @@ public class LoggingSpanReceiver extends SpanReceiver {
 
     @Override
     public void receiveSpan(Span span) {
-        LOG.log(level, span.toString());
+        LOG.log(level, toString(span));
     }
 
     @Override
     public void close() throws IOException {
     }
+    
+    /**
+     * Sample log statements:
+     * 
+     * INFO org.apache.cxf.tracing.htrace.ext.LoggingSpanReceiver - spanId=e5999a29a1ea468201acac30ec04ae39

+     *   tracerId="tracer-server/192.168.0.100" start=1488049449621 stop=1488049451623 description="Get
Employees" 
+     *   parents=[e5999a29a1ea4682d346ae17e51e0bd4] kvs=[] timelines=[[time=1488049451623

+     *   message="Getting all employees"]]
+     * 
+     * INFO org.apache.cxf.tracing.htrace.ext.LoggingSpanReceiver - spanId=e5999a29a1ea4682ac0a9ad638e084ed

+     *   tracerId="tracer-client/192.168.0.100" start=1488049449074 stop=1488049454894 
+     *   description="GET http://localhost:8282/rest/api/people" parents=[] kvs=[] timelines=[]
+     */
+    
+    private String toString(TimelineAnnotation annotation) {
+        final StringBuilder sb = new StringBuilder();
+        append(sb, "time", annotation.getTime());
+        append(sb, "message", annotation.getMessage(), true);
+        return sb.toString();
+    }
+    
+    private String toString(Span span) {
+        final StringBuilder sb = new StringBuilder();
+        
+        if (span.getSpanId().isValid()) {
+            append(sb, "spanId", span.getSpanId().toString());
+        }
+        
+        String tracerId = span.getTracerId();
+        if (!StringUtils.isEmpty(tracerId)) {
+            append(sb, "tracerId", tracerId, true);
+        }
+        
+        if (span.getStartTimeMillis() != 0) {
+            append(sb, "start", span.getStartTimeMillis());
+        }
+        
+        if (span.getStopTimeMillis() != 0) {
+            append(sb, "stop", span.getStopTimeMillis());
+        }
+        
+        if (!StringUtils.isEmpty(span.getDescription())) {
+            append(sb, "description", span.getDescription(), true);
+        }
+        
+        append(sb, "parents", span.getParents());
+        append(sb, "kvs", span.getKVAnnotations());
+        append(sb, "timelines", span.getTimelineAnnotations(), t -> "[" + toString(t)
+ "]");
+        
+        return sb.toString();
+    }
+
+    private<T> void append(StringBuilder sb, String key, Map<String, String>
values) {
+        final StringBuilder inner = new StringBuilder();
+        
+        for (final Map.Entry<String, String> entry : values.entrySet()) {
+            append(inner, quote(entry.getKey()), entry.getValue(), true);
+        }
+
+        append(sb, key, inner.insert(0, "[").append("]").toString());
+    }
+    
+    private<T> void append(StringBuilder sb, String key, Collection<T> values,
Function<T, String> stringifyer) {
+        append(sb, key, Arrays.toString(values.stream().map(stringifyer::apply).toArray(String[]::new)));
+    }
+    
+    private<T> void append(StringBuilder sb, String key, T[] values) {
+        append(sb, key, Arrays.toString(Arrays.stream(values).map(T::toString).toArray(String[]::new)));
+    }
+    
+    private void append(StringBuilder sb, String key, long value) {
+        append(sb, key, Long.toString(value));
+    }
+    
+    private void append(StringBuilder sb, String key, String value) {
+        append(sb, key, value, false);
+    }
+    
+    private void append(StringBuilder sb, String key, String value, boolean quoted) {
+        if (sb.length() > 0) {
+            sb.append(" ");
+        }
+        
+        sb.append(key).append("=");
+        quote(sb, value, quoted);
+    }
+
+    private String quote(String value) {
+        final StringBuilder sb = new StringBuilder();
+        quote(sb, value, true);
+        return sb.toString();
+    }
+    
+    private void quote(StringBuilder sb, String value, boolean quoted) {
+        if (quoted) {
+            sb.append("\"");
+        }
+        
+        sb.append(value);
+        if (quoted) {
+            sb.append("\"");
+        }
+    }
 }


Mime
View raw message