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-6360: Integration with Apache HTrace
Date Thu, 04 Jun 2015 01:31:45 GMT
Repository: cxf
Updated Branches:
  refs/heads/master f97ef8372 -> 1db0f0bbe


CXF-6360: Integration with Apache HTrace


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

Branch: refs/heads/master
Commit: 1db0f0bbe8435bc0c081627857d5047ec8ee7f55
Parents: f97ef83
Author: reta <drreta@gmail.com>
Authored: Wed Jun 3 21:30:59 2015 -0400
Committer: reta <drreta@gmail.com>
Committed: Wed Jun 3 21:30:59 2015 -0400

----------------------------------------------------------------------
 .../jaxrs/tracing/htrace/HTraceProvider.java    |  46 +++++++--
 .../org/apache/cxf/tracing/TracerHeaders.java   |   7 +-
 .../htrace/HTraceTracingCustomHeadersTest.java  | 102 +++++++++++++++++++
 .../jaxrs/tracing/htrace/HTraceTracingTest.java |  30 +++---
 4 files changed, 161 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1db0f0bb/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceProvider.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceProvider.java
b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceProvider.java
index a32f41c..5514bcc 100644
--- a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceProvider.java
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceProvider.java
@@ -30,6 +30,10 @@ 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.htrace.Sampler;
 import org.apache.htrace.Trace;
 import org.apache.htrace.TraceInfo;
@@ -37,8 +41,8 @@ import org.apache.htrace.TraceScope;
 import org.apache.htrace.Tracer;
 import org.apache.htrace.impl.NeverSampler;
 
-import static org.apache.cxf.tracing.TracerHeaders.HEADER_SPAN_ID;
-import static org.apache.cxf.tracing.TracerHeaders.HEADER_TRACE_ID;
+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 {

@@ -61,11 +65,11 @@ public class HTraceProvider implements ContainerRequestFilter, ContainerResponse
         final MultivaluedMap<String, String> headers = requestContext.getHeaders();
         
         // Try to extract the Trace Id value from the request header
-        final long traceId = getFirstValueOrDefault(headers, HEADER_TRACE_ID, 
+        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, HEADER_SPAN_ID, 
+        final long spanId = getFirstValueOrDefault(headers, getSpanIdHeader(), 
             Tracer.DONT_TRACE.spanId); 
         
         if (traceId != Tracer.DONT_TRACE.traceId && spanId != Tracer.DONT_TRACE.spanId)
{
@@ -79,10 +83,13 @@ public class HTraceProvider implements ContainerRequestFilter, ContainerResponse
             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(HEADER_TRACE_ID) && headers.containsKey(HEADER_SPAN_ID))
{
-            responseContext.getHeaders().add(HEADER_TRACE_ID, headers.getFirst(HEADER_TRACE_ID));
-            responseContext.getHeaders().add(HEADER_SPAN_ID, headers.getFirst(HEADER_SPAN_ID));
+        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);
@@ -104,4 +111,29 @@ public class HTraceProvider implements ContainerRequestFilter, ContainerResponse
         }
         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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1db0f0bb/rt/management/src/main/java/org/apache/cxf/tracing/TracerHeaders.java
----------------------------------------------------------------------
diff --git a/rt/management/src/main/java/org/apache/cxf/tracing/TracerHeaders.java b/rt/management/src/main/java/org/apache/cxf/tracing/TracerHeaders.java
index 9cbf9fa..be5de3a 100644
--- a/rt/management/src/main/java/org/apache/cxf/tracing/TracerHeaders.java
+++ b/rt/management/src/main/java/org/apache/cxf/tracing/TracerHeaders.java
@@ -19,8 +19,11 @@
 package org.apache.cxf.tracing;
 
 public final class TracerHeaders {
-    public static final String HEADER_TRACE_ID = "X-Trace-Id";
-    public static final String HEADER_SPAN_ID = "X-Span-Id";
+    public static final String HEADER_TRACE_ID = "org.apache.cxf.jaxrs.tracing.header.trace_id";
+    public static final String HEADER_SPAN_ID = "org.apache.cxf.jaxrs.tracing.header.span_id";
+    
+    public static final String DEFAULT_HEADER_TRACE_ID = "X-Trace-Id";
+    public static final String DEFAULT_HEADER_SPAN_ID = "X-Span-Id";
     
     private TracerHeaders() {
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1db0f0bb/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingCustomHeadersTest.java
----------------------------------------------------------------------
diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingCustomHeadersTest.java
b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingCustomHeadersTest.java
new file mode 100644
index 0000000..ab4f7ba
--- /dev/null
+++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingCustomHeadersTest.java
@@ -0,0 +1,102 @@
+/**
+ * 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.systest.jaxrs.tracing.htrace;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.jaxrs.tracing.htrace.HTraceFeature;
+import org.apache.cxf.systest.jaxrs.tracing.BookStore;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.tracing.TracerHeaders;
+import org.apache.htrace.HTraceConfiguration;
+import org.apache.htrace.impl.AlwaysSampler;
+import org.apache.htrace.impl.StandardOutSpanReceiver;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+
+public class HTraceTracingCustomHeadersTest extends AbstractBusClientServerTestBase {
+    public static final String PORT = allocatePort(HTraceTracingCustomHeadersTest.class);
+    
+    private static final String CUSTOM_HEADER_SPAN_ID = "My-Span-Id";
+    private static final String CUSTOM_HEADER_TRACE_ID = "My-Trace-Id";
+    
+    @Ignore
+    public static class Server extends AbstractBusTestServerBase {
+        protected void run() {
+            final Map<String, String> properties = new HashMap<String, String>();
+            properties.put("span.receiver", StandardOutSpanReceiver.class.getName());
+            properties.put("sampler", AlwaysSampler.class.getName());
+            
+            final Map<String, Object> headers = new HashMap<String, Object>();
+            headers.put(TracerHeaders.HEADER_SPAN_ID, CUSTOM_HEADER_SPAN_ID);
+            headers.put(TracerHeaders.HEADER_TRACE_ID, CUSTOM_HEADER_TRACE_ID);
+            
+            final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+            sf.setResourceClasses(BookStore.class);
+            sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore()));
+            sf.setAddress("http://localhost:" + PORT);
+            sf.setProvider(new JacksonJsonProvider());
+            sf.setFeatures(Arrays.asList(new HTraceFeature(HTraceConfiguration.fromMap(properties))));
+            sf.setProperties(headers);
+            sf.create();
+        }
+    }
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        //keep out of process due to stack traces testing failures
+        assertTrue("server did not launch correctly", launchServer(Server.class, true));
+        createStaticBus();
+    }
+    
+    @Test
+    public void testThatNewInnerSpanIsCreated() {
+        final Response r = createWebClient("/bookstore/books")
+            .header(CUSTOM_HEADER_TRACE_ID, 10L)
+            .header(CUSTOM_HEADER_SPAN_ID, 20L)
+            .get();
+        assertEquals(Status.OK.getStatusCode(), r.getStatus());
+        
+        assertThat((String)r.getHeaders().getFirst(CUSTOM_HEADER_TRACE_ID), equalTo("10"));
+        assertThat((String)r.getHeaders().getFirst(CUSTOM_HEADER_SPAN_ID), equalTo("20"));
+    }
+
+    protected WebClient createWebClient(final String url) {
+        return WebClient
+            .create("http://localhost:" + PORT + url)
+            .accept(MediaType.APPLICATION_JSON);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/1db0f0bb/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingTest.java
----------------------------------------------------------------------
diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingTest.java
b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingTest.java
index 9866608..588bc4a 100644
--- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingTest.java
+++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/HTraceTracingTest.java
@@ -56,7 +56,7 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase {
             properties.put("span.receiver", TestSpanReceiver.class.getName());
             properties.put("sampler", AlwaysSampler.class.getName());
             
-            JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+            final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
             sf.setResourceClasses(BookStore.class);
             sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore()));
             sf.setAddress("http://localhost:" + PORT);
@@ -87,15 +87,15 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase
{
         assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(1));
         assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books"));
         
-        assertFalse(r.getHeaders().containsKey(TracerHeaders.HEADER_TRACE_ID));
-        assertFalse(r.getHeaders().containsKey(TracerHeaders.HEADER_SPAN_ID));
+        assertFalse(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_TRACE_ID));
+        assertFalse(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_SPAN_ID));
     }
     
     @Test
     public void testThatNewInnerSpanIsCreated() {
         final Response r = createWebClient("/bookstore/books")
-            .header(TracerHeaders.HEADER_TRACE_ID, 10L)
-            .header(TracerHeaders.HEADER_SPAN_ID, 20L)
+            .header(TracerHeaders.DEFAULT_HEADER_TRACE_ID, 10L)
+            .header(TracerHeaders.DEFAULT_HEADER_SPAN_ID, 20L)
             .get();
         assertEquals(Status.OK.getStatusCode(), r.getStatus());
         
@@ -103,15 +103,15 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase
{
         assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books"));
         assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("bookstore/books"));
         
-        assertThat((String)r.getHeaders().getFirst(TracerHeaders.HEADER_TRACE_ID), equalTo("10"));
-        assertThat((String)r.getHeaders().getFirst(TracerHeaders.HEADER_SPAN_ID), equalTo("20"));
+        assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_TRACE_ID),
equalTo("10"));
+        assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_SPAN_ID),
equalTo("20"));
     }
     
     @Test
     public void testThatCurrentSpanIsAnnotatedWithKeyValue() {
         final Response r = createWebClient("/bookstore/book/1")
-            .header(TracerHeaders.HEADER_TRACE_ID, 10L)
-            .header(TracerHeaders.HEADER_SPAN_ID, 20L)
+            .header(TracerHeaders.DEFAULT_HEADER_TRACE_ID, 10L)
+            .header(TracerHeaders.DEFAULT_HEADER_SPAN_ID, 20L)
             .get();
         assertEquals(Status.OK.getStatusCode(), r.getStatus());
         
@@ -119,15 +119,15 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase
{
         assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("bookstore/book/1"));
         assertThat(TestSpanReceiver.getAllSpans().get(0).getKVAnnotations().size(), equalTo(1));
         
-        assertThat((String)r.getHeaders().getFirst(TracerHeaders.HEADER_TRACE_ID), equalTo("10"));
-        assertThat((String)r.getHeaders().getFirst(TracerHeaders.HEADER_SPAN_ID), equalTo("20"));
+        assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_TRACE_ID),
equalTo("10"));
+        assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_SPAN_ID),
equalTo("20"));
     }
     
     @Test
     public void testThatParallelSpanIsAnnotatedWithTimeline() {
         final Response r = createWebClient("/bookstore/process")
-            .header(TracerHeaders.HEADER_TRACE_ID, 10L)
-            .header(TracerHeaders.HEADER_SPAN_ID, 20L)
+            .header(TracerHeaders.DEFAULT_HEADER_TRACE_ID, 10L)
+            .header(TracerHeaders.DEFAULT_HEADER_SPAN_ID, 20L)
             .put("");
         assertEquals(Status.OK.getStatusCode(), r.getStatus());
         
@@ -137,8 +137,8 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase
{
         assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("Processing
books"));
         assertThat(TestSpanReceiver.getAllSpans().get(1).getTimelineAnnotations().size(),
equalTo(1));
         
-        assertThat((String)r.getHeaders().getFirst(TracerHeaders.HEADER_TRACE_ID), equalTo("10"));
-        assertThat((String)r.getHeaders().getFirst(TracerHeaders.HEADER_SPAN_ID), equalTo("20"));
+        assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_TRACE_ID),
equalTo("10"));
+        assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_SPAN_ID),
equalTo("20"));
     }
     
     protected WebClient createWebClient(final String url) {


Mime
View raw message