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 Sun, 31 May 2015 22:53:33 GMT
Repository: cxf
Updated Branches:
  refs/heads/master f4aafc6f8 -> afd70f562


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/afd70f56
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/afd70f56
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/afd70f56

Branch: refs/heads/master
Commit: afd70f562f769c1f1ed26a275cbb3763b24cba43
Parents: f4aafc6
Author: reta <drreta@gmail.com>
Authored: Sun May 31 18:53:13 2015 -0400
Committer: reta <drreta@gmail.com>
Committed: Sun May 31 18:53:13 2015 -0400

----------------------------------------------------------------------
 .../tracing/htrace/HTraceTracerContext.java     | 39 ++++++++++++++++++++
 .../java/org/apache/cxf/tracing/Traceable.java  | 23 ++++++++++++
 .../org/apache/cxf/tracing/TracerContext.java   |  8 ++++
 .../cxf/systest/jaxrs/tracing/BookStore.java    | 32 ++++++++++++++++
 .../jaxrs/tracing/htrace/HTraceTracingTest.java | 34 +++++++++++++++++
 5 files changed, 136 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/afd70f56/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceTracerContext.java
----------------------------------------------------------------------
diff --git a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceTracerContext.java
b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceTracerContext.java
index 4bb6c73..5694795 100644
--- a/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceTracerContext.java
+++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/jaxrs/tracing/htrace/HTraceTracerContext.java
@@ -18,10 +18,15 @@
  */
 package org.apache.cxf.jaxrs.tracing.htrace;
 
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.Callable;
+
+import org.apache.cxf.tracing.Traceable;
 import org.apache.cxf.tracing.TracerContext;
 import org.apache.htrace.Sampler;
 import org.apache.htrace.Trace;
 import org.apache.htrace.TraceScope;
+import org.apache.htrace.wrappers.TraceCallable;
 
 public class HTraceTracerContext implements TracerContext {
     private final Sampler< ? > sampler;
@@ -35,4 +40,38 @@ public class HTraceTracerContext implements TracerContext {
     public TraceScope startSpan(final String description) {
         return Trace.startSpan(description, sampler);
     }
+    
+    @Override
+    public <T> Callable<T> wrap(final String desription, final Traceable<T>
traceable) {
+        final Callable<T> callable = new Callable<T>() {
+            @Override
+            public T call() throws Exception {
+                return traceable.call(new HTraceTracerContext(sampler));
+            }
+        };
+        
+        // TODO: Replace with HTrace's wrap() method once the version with
+        // callable and description becomes available.
+        if (Trace.isTracing()) {
+            return new TraceCallable<T>(Trace.currentSpan(), callable, desription);
+        } else {
+            return callable;
+        }
+    }
+
+    @Override
+    public void annotate(byte[] key, byte[] value) {
+        Trace.addKVAnnotation(key, value);
+    }
+    
+    @Override
+    public void annotate(String key, String value) {
+        annotate(key.getBytes(StandardCharsets.UTF_8), 
+            value.getBytes(StandardCharsets.UTF_8));
+    }
+    
+    @Override
+    public void timeline(String message) {
+        Trace.addTimelineAnnotation(message);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/afd70f56/rt/management/src/main/java/org/apache/cxf/tracing/Traceable.java
----------------------------------------------------------------------
diff --git a/rt/management/src/main/java/org/apache/cxf/tracing/Traceable.java b/rt/management/src/main/java/org/apache/cxf/tracing/Traceable.java
new file mode 100644
index 0000000..815b1af
--- /dev/null
+++ b/rt/management/src/main/java/org/apache/cxf/tracing/Traceable.java
@@ -0,0 +1,23 @@
+/**
+ * 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;
+
+public interface Traceable<T> {
+    T call(final TracerContext context) throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/afd70f56/rt/management/src/main/java/org/apache/cxf/tracing/TracerContext.java
----------------------------------------------------------------------
diff --git a/rt/management/src/main/java/org/apache/cxf/tracing/TracerContext.java b/rt/management/src/main/java/org/apache/cxf/tracing/TracerContext.java
index 2e1f37e..9ab9f58 100644
--- a/rt/management/src/main/java/org/apache/cxf/tracing/TracerContext.java
+++ b/rt/management/src/main/java/org/apache/cxf/tracing/TracerContext.java
@@ -18,6 +18,14 @@
  */
 package org.apache.cxf.tracing;
 
+import java.util.concurrent.Callable;
+
 public interface TracerContext {
     <T> T startSpan(final String desription);
+    <T> Callable<T> wrap(final String desription, final Traceable<T> traceable);
+    
+    void annotate(byte[] key, byte[] value);
+    void annotate(String key, String value);
+    
+    void timeline(String message);
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/afd70f56/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
index d7949dc..161202c 100644
--- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
+++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
@@ -21,19 +21,26 @@ package org.apache.cxf.systest.jaxrs.tracing;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 
+import org.apache.cxf.tracing.Traceable;
 import org.apache.cxf.tracing.TracerContext;
 import org.apache.htrace.TraceScope;
 
 @Path("/bookstore/")
 public class BookStore {
     @Context private TracerContext tracer;
+    private ExecutorService executor = Executors.newSingleThreadExecutor();
         
     @GET
     @Path("/books")
@@ -46,4 +53,29 @@ public class BookStore {
             );
         }
     }
+    
+    @GET
+    @Path("/book/{id}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Book getBook(@PathParam("id") final String id) {
+        tracer.annotate("book-id", id);
+        return new Book("Apache CXF in Action", id);
+    }
+    
+    @PUT
+    @Path("/process")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response processBooks() {
+        executor.submit(
+            tracer.wrap("Processing books", new Traceable<Void>() {
+                @Override
+                public Void call(final TracerContext context) throws Exception {
+                    context.timeline("Processing started");
+                    return null;
+                }
+            })
+        );
+        
+        return Response.ok().build();
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/afd70f56/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 0c754b9..9866608 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
@@ -107,6 +107,40 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase
{
         assertThat((String)r.getHeaders().getFirst(TracerHeaders.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)
+            .get();
+        assertEquals(Status.OK.getStatusCode(), r.getStatus());
+        
+        assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(1));
+        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"));
+    }
+    
+    @Test
+    public void testThatParallelSpanIsAnnotatedWithTimeline() {
+        final Response r = createWebClient("/bookstore/process")
+            .header(TracerHeaders.HEADER_TRACE_ID, 10L)
+            .header(TracerHeaders.HEADER_SPAN_ID, 20L)
+            .put("");
+        assertEquals(Status.OK.getStatusCode(), r.getStatus());
+        
+        assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(2));
+        assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("bookstore/process"));
+        assertThat(TestSpanReceiver.getAllSpans().get(0).getTimelineAnnotations().size(),
equalTo(0));
+        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"));
+    }
+    
     protected WebClient createWebClient(final String url) {
         return WebClient
             .create("http://localhost:" + PORT + url)


Mime
View raw message