Repository: cxf Updated Branches: refs/heads/master 646ec10d1 -> 7f7cc3c39
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/7f7cc3c3 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7f7cc3c3 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7f7cc3c3 Branch: refs/heads/master Commit: 7f7cc3c390b95f9d7589eb192538551416c313db Parents: 646ec10 Author: reta <[email protected]> Authored: Tue Jun 9 21:24:21 2015 -0400 Committer: reta <[email protected]> Committed: Tue Jun 9 21:24:21 2015 -0400 ---------------------------------------------------------------------- .../tracing/htrace/AbstractHTraceProvider.java | 16 ++- .../htrace/jaxrs/HTraceClientProvider.java | 100 +++++++++++++++++++ parent/pom.xml | 2 +- .../jaxrs/tracing/htrace/HTraceTracingTest.java | 26 ++++- 4 files changed, 134 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/7f7cc3c3/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 index 724f472..29cedb0 100644 --- 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 @@ -34,6 +34,7 @@ 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.MilliSpan; import static org.apache.cxf.tracing.TracerHeaders.DEFAULT_HEADER_SPAN_ID; import static org.apache.cxf.tracing.TracerHeaders.DEFAULT_HEADER_TRACE_ID; @@ -59,11 +60,16 @@ public abstract class AbstractHTraceProvider { 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; + if (traceId == Tracer.DONT_TRACE.traceId || spanId == Tracer.DONT_TRACE.spanId) { + return Trace.startSpan(path, (Sampler< TraceInfo >)sampler, + new TraceInfo(traceId, spanId)); + } + + return Trace.startSpan(path, new MilliSpan + .Builder() + .spanId(spanId) + .traceId(traceId) + .build()); } protected void stopTraceSpan(final Map<String, List<String>> requestHeaders, http://git-wip-us.apache.org/repos/asf/cxf/blob/7f7cc3c3/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 new file mode 100644 index 0000000..ff483e2 --- /dev/null +++ b/integration/tracing/tracing-htrace/src/main/java/org/apache/cxf/tracing/htrace/jaxrs/HTraceClientProvider.java @@ -0,0 +1,100 @@ +/** + * 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.jaxrs; + +import java.io.IOException; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.Provider; + +import org.apache.cxf.tracing.TracerHeaders; +import org.apache.htrace.Sampler; +import org.apache.htrace.Span; +import org.apache.htrace.Trace; +import org.apache.htrace.TraceScope; +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 HTraceClientProvider implements ClientRequestFilter, ClientResponseFilter { + private static final String TRACE_SPAN = "org.apache.cxf.tracing.client.htrace.span"; + + private final Sampler< ? > sampler; + + public HTraceClientProvider() { + this(NeverSampler.INSTANCE); + } + + public HTraceClientProvider(final Sampler< ? > sampler) { + this.sampler = sampler; + } + + @Override + public void filter(final ClientRequestContext requestContext) throws IOException { + Span span = Trace.currentSpan(); + + if (span == null) { + final TraceScope scope = Trace.startSpan(requestContext.getUri().toString(), sampler); + span = scope.getSpan(); + + if (span != null) { + requestContext.setProperty(TRACE_SPAN, scope); + } + } + + if (span != null) { + final MultivaluedMap<String, Object> requestHeaders = requestContext.getHeaders(); + + final String traceIdHeader = getTraceIdHeader(); + final String spanIdHeader = getSpanIdHeader(); + + // Transfer tracing headers into the response headers + requestHeaders.putSingle(traceIdHeader, Long.toString(span.getTraceId())); + requestHeaders.putSingle(spanIdHeader, Long.toString(span.getSpanId())); + } + } + + @Override + public void filter(final ClientRequestContext requestContext, + final ClientResponseContext responseContext) throws IOException { + + final TraceScope scope = (TraceScope)requestContext.getProperty(TRACE_SPAN); + if (scope != null) { + scope.close(); + } + } + + 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) { + return fallback; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/7f7cc3c3/parent/pom.xml ---------------------------------------------------------------------- diff --git a/parent/pom.xml b/parent/pom.xml index 3b7a815..4c132cb 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -190,7 +190,7 @@ <cxf.olingo.version>1.2.0</cxf.olingo.version> <cxf.tika.version>1.6</cxf.tika.version> <cxf.jexl.version>2.1.1</cxf.jexl.version> - <cxf.htrace.version>3.1.0-incubating</cxf.htrace.version> + <cxf.htrace.version>3.2.0-incubating</cxf.htrace.version> <cxf.checkstyle.extension /> <cxf.jaxb.context.class /> <cxf.jaxb.context.class.property>none</cxf.jaxb.context.class.property> http://git-wip-us.apache.org/repos/asf/cxf/blob/7f7cc3c3/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 68e5c24..4e9ff85 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,10 +36,10 @@ 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.cxf.tracing.htrace.jaxrs.HTraceClientProvider; import org.apache.cxf.tracing.htrace.jaxrs.HTraceFeature; import org.apache.htrace.HTraceConfiguration; import org.apache.htrace.impl.AlwaysSampler; - import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; @@ -49,6 +49,8 @@ import static org.hamcrest.CoreMatchers.equalTo; public class HTraceTracingTest extends AbstractBusClientServerTestBase { public static final String PORT = allocatePort(HTraceTracingTest.class); + + private HTraceClientProvider htraceClientProvider; @Ignore public static class Server extends AbstractBusTestServerBase { @@ -78,6 +80,9 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase { @Before public void setUp() { TestSpanReceiver.clear(); + + htraceClientProvider = new HTraceClientProvider( + new AlwaysSampler(HTraceConfiguration.EMPTY)); } @Test @@ -85,7 +90,7 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase { final Response r = createWebClient("/bookstore/books").get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); - assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(1)); + assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(2)); assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books")); assertFalse(r.getHeaders().containsKey(TracerHeaders.DEFAULT_HEADER_TRACE_ID)); @@ -142,9 +147,22 @@ public class HTraceTracingTest extends AbstractBusClientServerTestBase { assertThat((String)r.getHeaders().getFirst(TracerHeaders.DEFAULT_HEADER_SPAN_ID), equalTo("20")); } - protected WebClient createWebClient(final String url) { + @Test + public void testThatNewChildSpanIsCreatedWhenParentIsProvided() { + final Response r = createWebClient("/bookstore/books", htraceClientProvider).get(); + 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(0).getParents().length, equalTo(1)); + + 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) + .create("http://localhost:" + PORT + url, Arrays.asList(providers)) .accept(MediaType.APPLICATION_JSON); } }
