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. Fixed asynchronous invocation flow on client-side
Date Thu, 17 Sep 2015 01:37:35 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 698227f4a -> b0097f196


CXF-6360: Integration with Apache HTrace. Fixed asynchronous invocation flow on client-side


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

Branch: refs/heads/master
Commit: b0097f19641e9c42837e87e98d37415026087640
Parents: 698227f
Author: reta <drreta@gmail.com>
Authored: Wed Sep 16 21:37:24 2015 -0400
Committer: reta <drreta@gmail.com>
Committed: Wed Sep 16 21:37:24 2015 -0400

----------------------------------------------------------------------
 .../htrace/jaxrs/HTraceClientProvider.java      | 23 ++++++++++++++++++--
 .../jaxrs/tracing/htrace/HTraceTracingTest.java | 20 +++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/b0097f19/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceClientProvider.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceClientProvider.java
b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceClientProvider.java
index fc88ec8..cfd5ca7 100644
--- a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceClientProvider.java
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceClientProvider.java
@@ -27,6 +27,7 @@ import javax.ws.rs.client.ClientResponseFilter;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.Provider;
 
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.tracing.AbstractTracingProvider;
 import org.apache.htrace.Sampler;
 import org.apache.htrace.Span;
@@ -52,9 +53,10 @@ public class HTraceClientProvider extends AbstractTracingProvider
     @Override
     public void filter(final ClientRequestContext requestContext) throws IOException {
         Span span = Trace.currentSpan();
+        TraceScope scope = null;
         
         if (span == null) {
-            final TraceScope scope = Trace.startSpan(buildSpanDescription(requestContext.getUri().toString(),

+            scope = Trace.startSpan(buildSpanDescription(requestContext.getUri().toString(),

                 requestContext.getMethod()), sampler);
             span = scope.getSpan();
             
@@ -73,6 +75,12 @@ public class HTraceClientProvider extends AbstractTracingProvider
             requestHeaders.putSingle(traceIdHeader, Long.toString(span.getTraceId()));
             requestHeaders.putSingle(spanIdHeader, Long.toString(span.getSpanId()));
         }
+        
+        // In case of asynchronous client invocation, the span should be detached as JAX-RS

+        // client request / response filters are going to be executed in different threads.
+        if (isAsyncInvocation() && scope != null) {
+            scope.detach();
+        }
     }
     
     @Override
@@ -81,7 +89,18 @@ public class HTraceClientProvider extends AbstractTracingProvider
         
         final TraceScope scope = (TraceScope)requestContext.getProperty(TRACE_SPAN);
         if (scope != null) {
-            scope.close();
+            // If the client invocation was asynchronous , the trace scope has been created

+            // in another thread and should be re-attached to the current one.
+            if (scope.isDetached()) {
+                final TraceScope continueSpan = Trace.continueSpan(scope.getSpan()); 
+                continueSpan.close();
+            } else {            
+                scope.close();
+            }
         }
     }
+    
+    private boolean isAsyncInvocation() {
+        return !JAXRSUtils.getCurrentMessage().getExchange().isSynchronous();
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/b0097f19/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 48bd0cc..11b7442 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
@@ -21,6 +21,8 @@ package org.apache.cxf.systest.jaxrs.tracing.htrace;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -92,6 +94,7 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase {
         
         assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(2));
         assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books"));
+        assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("GET bookstore/books"));
         
         assertFalse(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_TRACE_ID));
         assertFalse(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_SPAN_ID));
@@ -202,6 +205,23 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase
{
         assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("Processing
books"));
     }
     
+    @Test
+    public void testThatNewSpanIsCreatedWhenNotProvidedUsingAsyncClient() throws Exception
{
+        final WebClient client = createWebClient("/bookstore/books", htraceClientProvider);
+        final Future<Response> f = client.async().get();
+        
+        final Response r = f.get(1, TimeUnit.SECONDS);
+        assertEquals(Status.OK.getStatusCode(), r.getStatus());
+        
+        assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(3));
+        assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books"));
+        assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("GET bookstore/books"));
+        assertThat(TestSpanReceiver.getAllSpans().get(2).getDescription(), equalTo("GET "
+ client.getCurrentURI()));
+        
+        assertTrue(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_TRACE_ID));
+        assertTrue(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_SPAN_ID));
+    }
+    
     protected WebClient createWebClient(final String url, final Object ... providers) {
         return WebClient
             .create("http://localhost:" + PORT + url, Arrays.asList(providers))


Mime
View raw message