Repository: cxf Updated Branches: refs/heads/master bcb43c388 -> cb2160fee
CXF-6360: Integration with Apache HTrace. Added basic JAX-WS test case for HTrace tracing. Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/cb2160fe Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/cb2160fe Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/cb2160fe Branch: refs/heads/master Commit: cb2160fee6782b8638477ae7af5ad4cd82d6b88f Parents: bcb43c3 Author: reta <[email protected]> Authored: Tue Sep 22 21:35:16 2015 -0400 Committer: reta <[email protected]> Committed: Tue Sep 22 21:35:16 2015 -0400 ---------------------------------------------------------------------- systests/tracing/pom.xml | 5 + .../test/java/org/apache/cxf/systest/Book.java | 52 ++++++++ .../apache/cxf/systest/jaxrs/tracing/Book.java | 52 -------- .../cxf/systest/jaxrs/tracing/BookStore.java | 1 + .../jaxrs/tracing/htrace/TestSpanReceiver.java | 6 + .../cxf/systest/jaxws/tracing/BookStore.java | 43 +++++++ .../systest/jaxws/tracing/BookStoreService.java | 30 +++++ .../jaxws/tracing/htrace/HTraceTracingTest.java | 118 +++++++++++++++++++ 8 files changed, 255 insertions(+), 52 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/systests/tracing/pom.xml ---------------------------------------------------------------------- diff --git a/systests/tracing/pom.xml b/systests/tracing/pom.xml index d9518df..40e0096 100644 --- a/systests/tracing/pom.xml +++ b/systests/tracing/pom.xml @@ -70,6 +70,11 @@ </dependency> <dependency> <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxws</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>${project.version}</version> </dependency> http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/systests/tracing/src/test/java/org/apache/cxf/systest/Book.java ---------------------------------------------------------------------- diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/Book.java b/systests/tracing/src/test/java/org/apache/cxf/systest/Book.java new file mode 100644 index 0000000..6f4abd9 --- /dev/null +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/Book.java @@ -0,0 +1,52 @@ +/** + * 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; + +public class Book { + private String title; + private String id; + + public Book() { + } + + public Book(String id) { + this.id = id; + } + + public Book(String title, String id) { + this.title = title; + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setId(String i) { + id = i; + } + + public String getId() { + return id; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/Book.java ---------------------------------------------------------------------- diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/Book.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/Book.java deleted file mode 100644 index baa499c..0000000 --- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/Book.java +++ /dev/null @@ -1,52 +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.systest.jaxrs.tracing; - -public class Book { - private String title; - private String id; - - public Book() { - } - - public Book(String id) { - this.id = id; - } - - public Book(String title, String id) { - this.title = title; - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setId(String i) { - id = i; - } - - public String getId() { - return id; - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/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 c88d4ba..a177852 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 @@ -36,6 +36,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.cxf.systest.Book; import org.apache.cxf.tracing.Traceable; import org.apache.cxf.tracing.TracerContext; import org.apache.htrace.TraceScope; http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/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 fd39123..6567422 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 @@ -26,14 +26,20 @@ import java.util.List; import org.apache.htrace.HTraceConfiguration; import org.apache.htrace.Span; import org.apache.htrace.SpanReceiver; +import org.apache.htrace.Trace; /** * Test HTrace Span receiver */ public class TestSpanReceiver implements SpanReceiver { private static List<Span> spans = new ArrayList<Span>(); + private static TestSpanReceiver instance; public TestSpanReceiver(final HTraceConfiguration conf) { + if (instance != null) { + Trace.removeReceiver(instance); + } + instance = this; } public Collection<Span> getSpans() { http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStore.java ---------------------------------------------------------------------- diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStore.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStore.java new file mode 100644 index 0000000..35baec7 --- /dev/null +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStore.java @@ -0,0 +1,43 @@ +/** + * 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.jaxws.tracing; + +import java.util.Arrays; +import java.util.Collection; +import java.util.UUID; + +import javax.jws.WebMethod; +import javax.jws.WebService; + +import org.apache.cxf.systest.Book; +import org.apache.htrace.Trace; +import org.apache.htrace.TraceScope; + +@WebService(endpointInterface = "org.apache.cxf.systest.jaxws.tracing.BookStoreService", serviceName = "BookStore") +public class BookStore implements BookStoreService { + @WebMethod + public Collection< Book > getBooks() { + try (final TraceScope span = Trace.startSpan("Get Books")) { + return Arrays.asList( + new Book("Apache CXF in Action", UUID.randomUUID().toString()), + new Book("Mastering Apache CXF", UUID.randomUUID().toString()) + ); + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStoreService.java ---------------------------------------------------------------------- diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStoreService.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStoreService.java new file mode 100644 index 0000000..635eb6c --- /dev/null +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/BookStoreService.java @@ -0,0 +1,30 @@ +/** + * 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.jaxws.tracing; + +import java.util.Collection; + +import javax.jws.WebService; + +import org.apache.cxf.systest.Book; + +@WebService +public interface BookStoreService { + Collection< Book > getBooks(); +} http://git-wip-us.apache.org/repos/asf/cxf/blob/cb2160fe/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/htrace/HTraceTracingTest.java ---------------------------------------------------------------------- diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/htrace/HTraceTracingTest.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/htrace/HTraceTracingTest.java new file mode 100644 index 0000000..0e6f297 --- /dev/null +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxws/tracing/htrace/HTraceTracingTest.java @@ -0,0 +1,118 @@ +/** + * 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.jaxws.tracing.htrace; + +import java.net.MalformedURLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.cxf.endpoint.Client; +import org.apache.cxf.frontend.ClientProxy; +import org.apache.cxf.interceptor.LoggingInInterceptor; +import org.apache.cxf.interceptor.LoggingOutInterceptor; +import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; +import org.apache.cxf.jaxws.JaxWsServerFactoryBean; +import org.apache.cxf.message.Message; +import org.apache.cxf.phase.Phase; +import org.apache.cxf.systest.jaxrs.tracing.htrace.TestSpanReceiver; +import org.apache.cxf.systest.jaxws.tracing.BookStore; +import org.apache.cxf.systest.jaxws.tracing.BookStoreService; +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.HTraceStartInterceptor; +import org.apache.cxf.tracing.htrace.HTraceStopInterceptor; +import org.apache.htrace.HTraceConfiguration; +import org.apache.htrace.Trace; +import org.apache.htrace.impl.AlwaysSampler; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class HTraceTracingTest extends AbstractBusClientServerTestBase { + public static final String PORT = allocatePort(HTraceTracingTest.class); + + @Ignore + public static class Server extends AbstractBusTestServerBase { + protected void run() { + final Map<String, String> properties = new HashMap<String, String>(); + final HTraceConfiguration conf = HTraceConfiguration.fromMap(properties); + Trace.addReceiver(new TestSpanReceiver(conf)); + + final JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean(); + sf.setServiceClass(BookStore.class); + sf.setAddress("http://localhost:" + PORT); + sf.getInInterceptors().add(new HTraceStartInterceptor(Phase.PRE_INVOKE, new AlwaysSampler(conf))); + sf.getOutInterceptors().add(new HTraceStopInterceptor(Phase.POST_MARSHAL)); + sf.create(); + } + } + + @BeforeClass + public static void startServers() throws Exception { + //keep out of process due to stack traces testing failures + assertTrue("server did not launch correctly", launchServer(Server.class, true)); + createStaticBus(); + } + + @Before + public void setUp() { + TestSpanReceiver.clear(); + } + + @Test + public void testThatNewSpanIsCreatedWhenNotProvided() throws MalformedURLException { + final BookStoreService service = createJaxWsService(); + assertThat(service.getBooks().size(), equalTo(2)); + + assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(2)); + assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books")); + assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("POST /BookStore")); + } + + @Test + public void testThatNewInnerSpanIsCreated() throws MalformedURLException { + final BookStoreService service = createJaxWsService(); + final Client proxy = ClientProxy.getClient(service); + + final Map<String, List<String>> headers = new HashMap<String, List<String>>(); + headers.put(TracerHeaders.DEFAULT_HEADER_TRACE_ID, Arrays.asList("10L")); + headers.put(TracerHeaders.DEFAULT_HEADER_SPAN_ID, Arrays.asList("20L")); + proxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers); + assertThat(service.getBooks().size(), equalTo(2)); + + assertThat(TestSpanReceiver.getAllSpans().size(), equalTo(2)); + assertThat(TestSpanReceiver.getAllSpans().get(0).getDescription(), equalTo("Get Books")); + assertThat(TestSpanReceiver.getAllSpans().get(1).getDescription(), equalTo("POST /BookStore")); + } + + protected BookStoreService createJaxWsService() throws MalformedURLException { + JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); + factory.getOutInterceptors().add(new LoggingOutInterceptor()); + factory.getInInterceptors().add(new LoggingInInterceptor()); + factory.setServiceClass(BookStoreService.class); + factory.setAddress("http://localhost:" + PORT + "/BookStore"); + return (BookStoreService) factory.create(); + } +}
