Repository: cxf Updated Branches: refs/heads/master 3520a87c8 -> 12d7631bb
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/12d7631b Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/12d7631b Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/12d7631b Branch: refs/heads/master Commit: 12d7631bb74f7034c2635f1f253eef79b0fae0ca Parents: 3520a87 Author: reta <[email protected]> Authored: Wed May 20 21:00:25 2015 -0400 Committer: reta <[email protected]> Committed: Wed May 20 21:00:25 2015 -0400 ---------------------------------------------------------------------- integration/tracing/tracing-core/pom.xml | 5 ++++ .../org/apache/cxf/tracing/TracerContext.java | 23 ---------------- .../jaxrs/tracing/htrace/HTraceProvider.java | 29 ++++++++++++++++---- .../org/apache/cxf/tracing/TracerContext.java | 23 ++++++++++++++++ .../org/apache/cxf/tracing/TracerHeaders.java | 27 ++++++++++++++++++ .../jaxrs/tracing/htrace/HTraceTracingTest.java | 22 ++++++++++++++- .../jaxrs/tracing/htrace/TestSpanReceiver.java | 5 ++-- 7 files changed, 101 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/integration/tracing/tracing-core/pom.xml ---------------------------------------------------------------------- diff --git a/integration/tracing/tracing-core/pom.xml b/integration/tracing/tracing-core/pom.xml index e405ebb..66439b1 100644 --- a/integration/tracing/tracing-core/pom.xml +++ b/integration/tracing/tracing-core/pom.xml @@ -50,6 +50,11 @@ </dependency> <dependency> <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-management</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${project.version}</version> </dependency> http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/integration/tracing/tracing-core/src/main/java/org/apache/cxf/tracing/TracerContext.java ---------------------------------------------------------------------- diff --git a/integration/tracing/tracing-core/src/main/java/org/apache/cxf/tracing/TracerContext.java b/integration/tracing/tracing-core/src/main/java/org/apache/cxf/tracing/TracerContext.java deleted file mode 100644 index 2e1f37e..0000000 --- a/integration/tracing/tracing-core/src/main/java/org/apache/cxf/tracing/TracerContext.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * 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 TracerContext { - <T> T startSpan(final String desription); -} http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/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 a66d93b..4c0977c 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 @@ -34,18 +34,29 @@ import org.apache.cxf.common.logging.LogUtils; 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.HEADER_SPAN_ID; +import static org.apache.cxf.tracing.TracerHeaders.HEADER_TRACE_ID; + @Provider public class HTraceProvider implements ContainerRequestFilter, ContainerResponseFilter { private static final Logger LOG = LogUtils.getL7dLogger(HTraceProvider.class); - - private static final String HEADER_TRACE_ID = "X-Trace-Id"; - private static final String HEADER_SPAN_ID = "X-Span-Id"; - + private final Sampler< ? > sampler; + // Keep the parent spans in the thread-local storage. The span created during request + // phase should be closed during the response phase in order to mark the start/end + // bounds + private final ThreadLocal<TraceScope> parent = new ThreadLocal<TraceScope>() { + @Override + protected TraceScope initialValue() { + return null; + } + }; + public HTraceProvider() { this(NeverSampler.INSTANCE); } @@ -68,8 +79,8 @@ public class HTraceProvider implements ContainerRequestFilter, ContainerResponse Tracer.DONT_TRACE.spanId); if (traceId != Tracer.DONT_TRACE.traceId && spanId != Tracer.DONT_TRACE.spanId) { - Trace.startSpan(requestContext.getUriInfo().getPath(), (Sampler< TraceInfo >)sampler, - new TraceInfo(traceId, spanId)); + parent.set(Trace.startSpan(requestContext.getUriInfo().getPath(), (Sampler< TraceInfo >)sampler, + new TraceInfo(traceId, spanId))); } } @@ -83,6 +94,12 @@ public class HTraceProvider implements ContainerRequestFilter, ContainerResponse responseContext.getHeaders().add(HEADER_TRACE_ID, headers.getFirst(HEADER_TRACE_ID)); responseContext.getHeaders().add(HEADER_SPAN_ID, headers.getFirst(HEADER_SPAN_ID)); } + + try (final TraceScope span = parent.get()) { + if (span != null) { + parent.remove(); + } + } } private static Long getFirstValueOrDefault(final MultivaluedMap<String, String> headers, http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/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 new file mode 100644 index 0000000..2e1f37e --- /dev/null +++ b/rt/management/src/main/java/org/apache/cxf/tracing/TracerContext.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 TracerContext { + <T> T startSpan(final String desription); +} http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/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 new file mode 100644 index 0000000..9cbf9fa --- /dev/null +++ b/rt/management/src/main/java/org/apache/cxf/tracing/TracerHeaders.java @@ -0,0 +1,27 @@ +/** + * 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 final class TracerHeaders { + public static final String HEADER_TRACE_ID = "X-Trace-Id"; + public static final String HEADER_SPAN_ID = "X-Span-Id"; + + private TracerHeaders() { + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/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 7591345..0c754b9 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 @@ -36,6 +36,7 @@ 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.junit.Before; @@ -79,12 +80,31 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase { } @Test - public void testThatPatternValidationFails() { + public void testThatNewSpanIsCreatedWhenNotProvided() { final Response r = createWebClient("/bookstore/books").get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); 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)); + } + + @Test + public void testThatNewInnerSpanIsCreated() { + final Response r = createWebClient("/bookstore/books") + .header(TracerHeaders.HEADER_TRACE_ID, 10L) + .header(TracerHeaders.HEADER_SPAN_ID, 20L) + .get(); + assertEquals(Status.OK.getStatusCode(), r.getStatus()); + + assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(2)); + 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")); } protected WebClient createWebClient(final String url) { http://git-wip-us.apache.org/repos/asf/cxf/blob/12d7631b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/TestSpanReceiver.java ---------------------------------------------------------------------- diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/TestSpanReceiver.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/TestSpanReceiver.java index 1f533a7..fd39123 100644 --- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/TestSpanReceiver.java +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/htrace/TestSpanReceiver.java @@ -21,7 +21,6 @@ package org.apache.cxf.systest.jaxrs.tracing.htrace; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.List; import org.apache.htrace.HTraceConfiguration; @@ -32,7 +31,7 @@ import org.apache.htrace.SpanReceiver; * Test HTrace Span receiver */ public class TestSpanReceiver implements SpanReceiver { - private static Collection<Span> spans = new HashSet<Span>(); + private static List<Span> spans = new ArrayList<Span>(); public TestSpanReceiver(final HTraceConfiguration conf) { } @@ -55,7 +54,7 @@ public class TestSpanReceiver implements SpanReceiver { } public static List<Span> getAllSpans() { - return new ArrayList< Span >(spans); + return spans; } } \ No newline at end of file
