cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6360] Introducing an abstract support
Date Fri, 05 Jun 2015 14:05:05 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 03d63bca6 -> e4858d27b


[CXF-6360] Introducing an abstract support


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

Branch: refs/heads/master
Commit: e4858d27b356f577eb31908dd43817681f007a4b
Parents: 03d63bc
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Jun 5 15:04:49 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Jun 5 15:04:49 2015 +0100

----------------------------------------------------------------------
 integration/tracing/tracing-htrace/pom.xml      |   1 +
 .../tracing/htrace/AbstractHTraceProvider.java  | 123 +++++++++++++++++++
 .../tracing/htrace/jaxrs/HTraceProvider.java    |  99 ++-------------
 3 files changed, 135 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/e4858d27/integration/tracing/tracing-htrace/pom.xml
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/pom.xml b/integration/tracing/tracing-htrace/pom.xml
index 8172c60..e856f7c 100644
--- a/integration/tracing/tracing-htrace/pom.xml
+++ b/integration/tracing/tracing-htrace/pom.xml
@@ -52,6 +52,7 @@
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-frontend-jaxrs</artifactId>
             <version>${project.version}</version>
+            <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>

http://git-wip-us.apache.org/repos/asf/cxf/blob/e4858d27/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/AbstractHTraceProvider.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/AbstractHTraceProvider.java
b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/AbstractHTraceProvider.java
new file mode 100644
index 0000000..724f472
--- /dev/null
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/AbstractHTraceProvider.java
@@ -0,0 +1,123 @@
+/**
+ * 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;
+
+import java.util.List;
+import java.util.Map;
+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.cxf.helpers.CastUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.PhaseInterceptorChain;
+import org.apache.cxf.tracing.TracerHeaders;
+import org.apache.htrace.Sampler;
+import org.apache.htrace.Trace;
+import org.apache.htrace.TraceInfo;
+import org.apache.htrace.TraceScope;
+import org.apache.htrace.Tracer;
+
+import static org.apache.cxf.tracing.TracerHeaders.DEFAULT_HEADER_SPAN_ID;
+import static org.apache.cxf.tracing.TracerHeaders.DEFAULT_HEADER_TRACE_ID;
+
+public abstract class AbstractHTraceProvider  { 
+    protected static final Logger LOG = LogUtils.getL7dLogger(AbstractHTraceProvider.class);
+    protected static final String TRACE_SPAN = "org.apache.cxf.tracing.htrace.span";
+        
+    private final Sampler< ? > sampler;
+    
+    public AbstractHTraceProvider(final Sampler< ? > sampler) {
+        this.sampler = sampler;
+    }
+
+    @SuppressWarnings("unchecked")
+    protected TraceScope startTraceSpan(final Map<String, List<String>> requestHeaders,
String path) {
+        
+        // Try to extract the Trace Id value from the request header
+        final long traceId = getFirstValueOrDefault(requestHeaders, getTraceIdHeader(), 
+            Tracer.DONT_TRACE.traceId);
+        
+        // Try to extract the Span Id value from the request header
+        final long spanId = getFirstValueOrDefault(requestHeaders, getSpanIdHeader(), 
+            Tracer.DONT_TRACE.spanId); 
+        
+        if (traceId != Tracer.DONT_TRACE.traceId && spanId != Tracer.DONT_TRACE.spanId)
{
+            return Trace.startSpan(path, 
+                                   (Sampler< TraceInfo >)sampler, new TraceInfo(traceId,
spanId));
+        }
+        return null;
+    }
+    
+    protected void stopTraceSpan(final Map<String, List<String>> requestHeaders,
+                                 final Map<String, List<Object>> responseHeaders,
+                                 final TraceScope span) {
+        final String traceIdHeader = getTraceIdHeader();
+        final String spanIdHeader = getSpanIdHeader();
+
+        // Transfer tracing headers into the response headers
+        if (requestHeaders.containsKey(traceIdHeader) && requestHeaders.containsKey(spanIdHeader))
{
+            responseHeaders.put(traceIdHeader, CastUtils.cast(requestHeaders.get(traceIdHeader)));
+            responseHeaders.put(spanIdHeader, CastUtils.cast(requestHeaders.get(spanIdHeader)));
+        }
+        
+        if (span != null) {
+            span.close();
+        }
+    }
+    
+    private static Long getFirstValueOrDefault(final Map<String, List<String>>
headers, 
+            final String header, final long defaultValue) {
+        List<String> value = headers.get(header);
+        if (value != null && !value.isEmpty()) {
+            try {
+                return Long.parseLong(value.get(0));
+            } catch (NumberFormatException ex) {
+                LOG.log(Level.FINE, String.format("Unable to parse '%s' header value to long
number", header), ex);
+            }
+        }
+        return defaultValue;
+    }
+    
+    private static String getSpanIdHeader() {
+        return getHeaderOrDefault(TracerHeaders.HEADER_SPAN_ID, DEFAULT_HEADER_SPAN_ID);
+    }
+    
+    private static String getTraceIdHeader() {
+        return getHeaderOrDefault(TracerHeaders.HEADER_TRACE_ID, DEFAULT_HEADER_TRACE_ID);
+    }
+
+    private static String getHeaderOrDefault(final String property, final String fallback)
{
+        final Message message = PhaseInterceptorChain.getCurrentMessage();
+        
+        if (message != null) {
+            final Object header = message.getContextualProperty(property);
+            
+            if (header instanceof String) {
+                final String name = (String)header;
+                if (!StringUtils.isEmpty(name)) {
+                    return name;
+                }
+            }
+        }
+        
+        return fallback;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/e4858d27/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceProvider.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceProvider.java
b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceProvider.java
index 2479157..5f0ff2f 100644
--- a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceProvider.java
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceProvider.java
@@ -19,121 +19,44 @@
 package org.apache.cxf.tracing.htrace.jaxrs;
 
 import java.io.IOException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import javax.ws.rs.container.ContainerRequestContext;
 import javax.ws.rs.container.ContainerRequestFilter;
 import javax.ws.rs.container.ContainerResponseContext;
 import javax.ws.rs.container.ContainerResponseFilter;
-import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.Provider;
 
-import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.jaxrs.utils.JAXRSUtils;
-import org.apache.cxf.message.Message;
-import org.apache.cxf.tracing.TracerHeaders;
+import org.apache.cxf.tracing.htrace.AbstractHTraceProvider;
 import org.apache.htrace.Sampler;
-import org.apache.htrace.Trace;
-import org.apache.htrace.TraceInfo;
 import org.apache.htrace.TraceScope;
-import org.apache.htrace.Tracer;
 import org.apache.htrace.impl.NeverSampler;
 
-import static org.apache.cxf.tracing.TracerHeaders.DEFAULT_HEADER_SPAN_ID;
-import static org.apache.cxf.tracing.TracerHeaders.DEFAULT_HEADER_TRACE_ID;
-
 @Provider
-public class HTraceProvider implements ContainerRequestFilter, ContainerResponseFilter {

-    private static final Logger LOG = LogUtils.getL7dLogger(HTraceProvider.class);
-    private static final String TRACE_SPAN = "org.apache.cxf.jaxrs.tracing.htrace.span";
-        
-    private final Sampler< ? > sampler;
-    
+public class HTraceProvider extends AbstractHTraceProvider 
+    implements ContainerRequestFilter, ContainerResponseFilter { 
     public HTraceProvider() {
         this(NeverSampler.INSTANCE);
     }
 
     public HTraceProvider(final Sampler< ? > sampler) {
-        this.sampler = sampler;
+        super(sampler);
     }
 
-    @SuppressWarnings("unchecked")
     @Override
     public void filter(final ContainerRequestContext requestContext) throws IOException {
-        final MultivaluedMap<String, String> headers = requestContext.getHeaders();
+        TraceScope scope = super.startTraceSpan(requestContext.getHeaders(), 
+                                                requestContext.getUriInfo().getPath());
         
-        // Try to extract the Trace Id value from the request header
-        final long traceId = getFirstValueOrDefault(headers, getTraceIdHeader(), 
-            Tracer.DONT_TRACE.traceId);
-        
-        // Try to extract the Span Id value from the request header
-        final long spanId = getFirstValueOrDefault(headers, getSpanIdHeader(), 
-            Tracer.DONT_TRACE.spanId); 
-        
-        if (traceId != Tracer.DONT_TRACE.traceId && spanId != Tracer.DONT_TRACE.spanId)
{
-            requestContext.setProperty(TRACE_SPAN, Trace.startSpan(requestContext.getUriInfo().getPath(),

-                (Sampler< TraceInfo >)sampler, new TraceInfo(traceId, spanId)));
+        if (scope != null) {
+            requestContext.setProperty(TRACE_SPAN, scope);
         }
     }
     
     @Override
     public void filter(final ContainerRequestContext requestContext,
             final ContainerResponseContext responseContext) throws IOException {
-        final MultivaluedMap<String, String> headers = requestContext.getHeaders();
-        
-        final String traceIdHeader = getTraceIdHeader();
-        final String spanIdHeader = getSpanIdHeader();
-
-        // Transfer tracing headers into the response headers
-        if (headers.containsKey(traceIdHeader) && headers.containsKey(spanIdHeader))
{
-            responseContext.getHeaders().add(traceIdHeader, headers.getFirst(traceIdHeader));
-            responseContext.getHeaders().add(spanIdHeader, headers.getFirst(spanIdHeader));
-        }
-        
-        final Object value = requestContext.getProperty(TRACE_SPAN);
-        if (value instanceof TraceScope) {
-            final TraceScope span = (TraceScope)value;
-            span.close();
-        }
-    }
-    
-    private static Long getFirstValueOrDefault(final MultivaluedMap<String, String>
headers, 
-            final String header, final long defaultValue) {
-        String value = headers.getFirst(header);
-        if (value != null) {
-            try {
-                return Long.parseLong(value);
-            } catch (NumberFormatException ex) {
-                LOG.log(Level.FINE, String.format("Unable to parse '%s' header value to long
number", header), ex);
-            }
-        }
-        return defaultValue;
-    }
-    
-    private static String getSpanIdHeader() {
-        return getHeaderOrDefault(TracerHeaders.HEADER_SPAN_ID, DEFAULT_HEADER_SPAN_ID);
-    }
-    
-    private static String getTraceIdHeader() {
-        return getHeaderOrDefault(TracerHeaders.HEADER_TRACE_ID, DEFAULT_HEADER_TRACE_ID);
-    }
-
-    private static String getHeaderOrDefault(final String property, final String fallback)
{
-        final Message message = JAXRSUtils.getCurrentMessage();
-        
-        if (message != null) {
-            final Object header = message.getContextualProperty(property);
-            
-            if (header instanceof String) {
-                final String name = (String)header;
-                if (!StringUtils.isEmpty(name)) {
-                    return name;
-                }
-            }
-        }
-        
-        return fallback;
+        super.stopTraceSpan(requestContext.getHeaders(), 
+                            responseContext.getHeaders(), 
+                            (TraceScope)requestContext.getProperty(TRACE_SPAN));
     }
 }


Mime
View raw message