Repository: cxf Updated Branches: refs/heads/master 892a4ca84 -> fd77d3f33
[CXF-7164] Support tracing using Zipkin Brave Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/fd77d3f3 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/fd77d3f3 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/fd77d3f3 Branch: refs/heads/master Commit: fd77d3f3380f805db3547d37cbb2ff72bd033ad0 Parents: 892a4ca Author: Christian Schneider <[email protected]> Authored: Tue Dec 6 14:35:35 2016 +0100 Committer: Christian Schneider <[email protected]> Committed: Tue Dec 6 14:36:01 2016 +0100 ---------------------------------------------------------------------- rt/features/pom.xml | 1 + rt/features/tracing-brave/pom.xml | 87 +++++++++++++ .../brave/soap/CxfHttpClientRequest.java | 49 ++++++++ .../tracing/brave/soap/CxfServerRequest.java | 48 ++++++++ .../cxf/tracing/brave/soap/HttpResponse200.java | 28 +++++ .../cxf/tracing/brave/soap/ParsedMessage.java | 121 +++++++++++++++++++ .../cxf/tracing/brave/soap/TraceFeature.java | 50 ++++++++ .../tracing/brave/soap/TraceInInterceptor.java | 58 +++++++++ .../tracing/brave/soap/TraceOutInterceptor.java | 57 +++++++++ .../cxf/tracing/brave/soap/BraveTraceTest.java | 99 +++++++++++++++ .../brave/soap/LoggingSpanNameProvider.java | 31 +++++ .../cxf/tracing/brave/soap/MyService.java | 26 ++++ .../cxf/tracing/brave/soap/MyServiceImpl.java | 28 +++++ 13 files changed, 683 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/pom.xml ---------------------------------------------------------------------- diff --git a/rt/features/pom.xml b/rt/features/pom.xml index 0365bf6..a4cfdff 100644 --- a/rt/features/pom.xml +++ b/rt/features/pom.xml @@ -34,5 +34,6 @@ <module>logging</module> <module>metrics</module> <module>throttling</module> + <module>tracing-brave</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/pom.xml ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/pom.xml b/rt/features/tracing-brave/pom.xml new file mode 100644 index 0000000..9af22e4 --- /dev/null +++ b/rt/features/tracing-brave/pom.xml @@ -0,0 +1,87 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>cxf-rt-features-tracing-brave</artifactId> + <packaging>jar</packaging> + <name>Apache CXF Tracing using Zipkin Brave</name> + <url>http://cxf.apache.org</url> + <parent> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-parent</artifactId> + <version>3.2.0-SNAPSHOT</version> + <relativePath>../../../parent/pom.xml</relativePath> + </parent> + + <properties> + <cxf.osgi.export> + org.apache.cxf.tracing.brave.soap + </cxf.osgi.export> + <brave.version>3.15.1</brave.version> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-core</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-features-logging</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>io.zipkin.brave</groupId> + <artifactId>brave-core</artifactId> + <version>${brave.version}</version> + </dependency> + <dependency> + <groupId>io.zipkin.brave</groupId> + <artifactId>brave-http</artifactId> + <version>${brave.version}</version> + </dependency> + + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jdk14</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-transports-http-jetty</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxws</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfHttpClientRequest.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfHttpClientRequest.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfHttpClientRequest.java new file mode 100644 index 0000000..78a40fd --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfHttpClientRequest.java @@ -0,0 +1,49 @@ +/** + * 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.brave.soap; + +import java.net.URI; + +import com.github.kristofa.brave.http.HttpClientRequest; +import org.apache.cxf.message.Message; + +public class CxfHttpClientRequest implements HttpClientRequest { + + private ParsedMessage message; + + public CxfHttpClientRequest(Message message) { + this.message = new ParsedMessage(message); + } + + @Override + public URI getUri() { + return message.getUri(); + } + + @Override + public String getHttpMethod() { + return message.getHttpMethod(); + } + + @Override + public void addHeader(String header, String value) { + message.addHeader(header, value); + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfServerRequest.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfServerRequest.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfServerRequest.java new file mode 100644 index 0000000..fcc8797 --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/CxfServerRequest.java @@ -0,0 +1,48 @@ +/** + * 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.brave.soap; + +import java.net.URI; + +import com.github.kristofa.brave.http.HttpServerRequest; + +public class CxfServerRequest implements HttpServerRequest { + + private ParsedMessage message; + + public CxfServerRequest(ParsedMessage message) { + this.message = message; + } + + @Override + public URI getUri() { + return message.getUri(); + } + + @Override + public String getHttpMethod() { + return message.getHttpMethod(); + } + + @Override + public String getHttpHeaderValue(String headerName) { + return message.getHeaders().get(headerName); + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/HttpResponse200.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/HttpResponse200.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/HttpResponse200.java new file mode 100644 index 0000000..f50dbde --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/HttpResponse200.java @@ -0,0 +1,28 @@ +/** + * 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.brave.soap; + +import com.github.kristofa.brave.http.HttpResponse; + +final class HttpResponse200 implements HttpResponse { + @Override + public int getHttpStatusCode() { + return 200; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/ParsedMessage.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/ParsedMessage.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/ParsedMessage.java new file mode 100644 index 0000000..8967375 --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/ParsedMessage.java @@ -0,0 +1,121 @@ +/** + * 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.brave.soap; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.cxf.helpers.CastUtils; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageUtils; + +public class ParsedMessage { + + private Message message; + + public ParsedMessage(Message message) { + this.message = message; + } + + String safeGet(String key) { + if (!message.containsKey(key)) { + return null; + } + Object value = message.get(key); + return (value instanceof String) ? value.toString() : null; + } + + public String getUriSt() { + String uri = safeGet(Message.REQUEST_URL); + if (uri == null) { + String address = safeGet(Message.ENDPOINT_ADDRESS); + uri = safeGet(Message.REQUEST_URI); + if (uri != null && uri.startsWith("/")) { + if (address != null && !address.startsWith(uri)) { + if (address.endsWith("/") && address.length() > 1) { + address = address.substring(0, address.length()); + } + uri = address + uri; + } + } else { + uri = address; + } + } + String query = safeGet(Message.QUERY_STRING); + if (query != null) { + return uri + "?" + query; + } else { + return uri; + } + } + + public URI getUri() { + try { + String uriSt = getUriSt(); + return uriSt != null ? new URI(uriSt) : new URI(""); + } catch (URISyntaxException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + Message getEffectiveMessage() { + boolean isRequestor = MessageUtils.isRequestor(message); + boolean isOutbound = MessageUtils.isOutbound(message); + if (isRequestor) { + return isOutbound ? message : message.getExchange().getOutMessage(); + } else { + return isOutbound ? message.getExchange().getInMessage() : message; + } + } + + Map<String, String> getHeaders() { + Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS)); + Map<String, String> result = new HashMap<>(); + if (headers == null) { + return result; + } + for (Map.Entry<String, List<String>> entry : headers.entrySet()) { + if (entry.getValue().size() == 1) { + result.put(entry.getKey(), entry.getValue().get(0)); + } else { + String[] valueAr = entry.getValue().toArray(new String[] {}); + result.put(entry.getKey(), valueAr.toString()); + } + } + return result; + } + + void addHeader(String key, String value) { + Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS)); + if (headers == null) { + headers = new HashMap<String, List<String>>(); + message.put(Message.PROTOCOL_HEADERS, headers); + } + headers.put(key, Arrays.asList(value)); + } + + public String getHttpMethod() { + ParsedMessage eMessage = new ParsedMessage(getEffectiveMessage()); + return eMessage.safeGet(Message.HTTP_REQUEST_METHOD); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceFeature.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceFeature.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceFeature.java new file mode 100644 index 0000000..fbaca07 --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceFeature.java @@ -0,0 +1,50 @@ +/** + * 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.brave.soap; + +import com.github.kristofa.brave.Brave; +import com.github.kristofa.brave.http.DefaultSpanNameProvider; +import org.apache.cxf.Bus; +import org.apache.cxf.annotations.Provider; +import org.apache.cxf.annotations.Provider.Type; +import org.apache.cxf.common.injection.NoJSR250Annotations; +import org.apache.cxf.feature.AbstractFeature; +import org.apache.cxf.interceptor.InterceptorProvider; + +@NoJSR250Annotations +@Provider(value = Type.Feature) +public class TraceFeature extends AbstractFeature { + private TraceInInterceptor in; + private TraceOutInterceptor out; + + public TraceFeature(Brave brave) { + DefaultSpanNameProvider nameProvider = new DefaultSpanNameProvider(); + in = new TraceInInterceptor(brave, nameProvider); + out = new TraceOutInterceptor(brave, nameProvider); + } + + @Override + protected void initializeProvider(InterceptorProvider provider, Bus bus) { + provider.getInInterceptors().add(in); + provider.getInFaultInterceptors().add(in); + + provider.getOutInterceptors().add(out); + provider.getOutFaultInterceptors().add(out); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceInInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceInInterceptor.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceInInterceptor.java new file mode 100644 index 0000000..4b84eb3 --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceInInterceptor.java @@ -0,0 +1,58 @@ +/** + * 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.brave.soap; + +import com.github.kristofa.brave.Brave; +import com.github.kristofa.brave.http.HttpClientResponseAdapter; +import com.github.kristofa.brave.http.HttpServerRequestAdapter; +import com.github.kristofa.brave.http.SpanNameProvider; +import org.apache.cxf.common.injection.NoJSR250Annotations; +import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageUtils; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.phase.Phase; + +/** + * + */ +@NoJSR250Annotations +public class TraceInInterceptor extends AbstractPhaseInterceptor<Message> { + + private Brave brave; + private SpanNameProvider spanNameProvider; + + public TraceInInterceptor(Brave brave, SpanNameProvider spanNameProvider) { + super(Phase.PRE_INVOKE); + this.brave = brave; + this.spanNameProvider = spanNameProvider; + } + + public void handleMessage(Message cxfMessage) throws Fault { + ParsedMessage message = new ParsedMessage(cxfMessage); + if (MessageUtils.isRequestor(cxfMessage)) { + brave.clientResponseInterceptor().handle(new HttpClientResponseAdapter(new HttpResponse200())); + } else { + HttpServerRequestAdapter adapter = + new HttpServerRequestAdapter(new CxfServerRequest(message), spanNameProvider); + brave.serverRequestInterceptor().handle(adapter); + } + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceOutInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceOutInterceptor.java b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceOutInterceptor.java new file mode 100644 index 0000000..17f76d0 --- /dev/null +++ b/rt/features/tracing-brave/src/main/java/org/apache/cxf/tracing/brave/soap/TraceOutInterceptor.java @@ -0,0 +1,57 @@ +/** + * 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.brave.soap; + +import com.github.kristofa.brave.Brave; +import com.github.kristofa.brave.http.HttpClientRequest; +import com.github.kristofa.brave.http.HttpClientRequestAdapter; +import com.github.kristofa.brave.http.HttpServerResponseAdapter; +import com.github.kristofa.brave.http.SpanNameProvider; +import org.apache.cxf.common.injection.NoJSR250Annotations; +import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageUtils; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.phase.Phase; + +/** + * + */ +@NoJSR250Annotations +public class TraceOutInterceptor extends AbstractPhaseInterceptor<Message> { + + private Brave brave; + private SpanNameProvider spanNameProvider; + + public TraceOutInterceptor(Brave brave, SpanNameProvider spanNameProvider) { + super(Phase.PRE_PROTOCOL); + this.brave = brave; + this.spanNameProvider = spanNameProvider; + } + + public void handleMessage(Message message) throws Fault { + if (MessageUtils.isRequestor(message)) { + final HttpClientRequest req = new CxfHttpClientRequest(message); + brave.clientRequestInterceptor().handle(new HttpClientRequestAdapter(req, spanNameProvider)); + } else { + brave.serverResponseInterceptor().handle(new HttpServerResponseAdapter(new HttpResponse200())); + } + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/BraveTraceTest.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/BraveTraceTest.java b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/BraveTraceTest.java new file mode 100644 index 0000000..5bac5f7 --- /dev/null +++ b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/BraveTraceTest.java @@ -0,0 +1,99 @@ +/** + * 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.brave.soap; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.github.kristofa.brave.Brave; +import org.apache.cxf.endpoint.Server; +import org.apache.cxf.ext.logging.LoggingFeature; +import org.apache.cxf.feature.Feature; +import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; +import org.apache.cxf.jaxws.JaxWsServerFactoryBean; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import zipkin.Span; +import zipkin.reporter.Reporter; + +public class BraveTraceTest { + + private static final String ADDRESS = "http://localhost:8182"; + private Server server; + private TraceFeature logging; + private Localreporter localReporter; + + @Before + public void startServer() { + localReporter = new Localreporter(); + logging = createLoggingFeature(localReporter); + server = createServer(logging); + } + + @Test + public void testMyService() { + MyService myService = createProxy(logging); + myService.echo("test"); + for (Span span : localReporter.spans) { + System.out.println(span); + } + Assert.assertEquals(2, localReporter.spans.size()); + + } + + @After + public void stopServer() { + server.destroy(); + } + + private static Server createServer(Feature logging) { + JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); + factory.setAddress(ADDRESS); + factory.setServiceBean(new MyServiceImpl()); + factory.setFeatures(Arrays.asList(logging)); + return factory.create(); + } + + private static MyService createProxy(Feature trace) { + JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); + factory.setServiceClass(MyService.class); + factory.setAddress(ADDRESS); + factory.setFeatures(Arrays.asList(trace, new LoggingFeature())); + return (MyService)factory.create(); + } + + private static TraceFeature createLoggingFeature(Reporter<Span> reporter) { + Brave brave = new Brave.Builder("myservice").reporter(reporter).build(); + return new TraceFeature(brave); + } + + static final class Localreporter implements Reporter<Span> { + List<Span> spans = new ArrayList<Span>(); + + @Override + public void report(Span span) { + spans.add(span); + } + + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/LoggingSpanNameProvider.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/LoggingSpanNameProvider.java b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/LoggingSpanNameProvider.java new file mode 100644 index 0000000..86fa25a --- /dev/null +++ b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/LoggingSpanNameProvider.java @@ -0,0 +1,31 @@ +/** + * 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.brave.soap; + +import com.github.kristofa.brave.http.HttpRequest; +import com.github.kristofa.brave.http.SpanNameProvider; + +public class LoggingSpanNameProvider implements SpanNameProvider { + + @Override + public String spanName(HttpRequest request) { + return (request instanceof SpanNameProvider) ? ((SpanNameProvider)request).spanName(request) : ""; + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyService.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyService.java b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyService.java new file mode 100644 index 0000000..2001164 --- /dev/null +++ b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyService.java @@ -0,0 +1,26 @@ +/** + * 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.brave.soap; + +import javax.jws.WebService; + +@WebService +public interface MyService { + String echo(String msg); +} http://git-wip-us.apache.org/repos/asf/cxf/blob/fd77d3f3/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyServiceImpl.java ---------------------------------------------------------------------- diff --git a/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyServiceImpl.java b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyServiceImpl.java new file mode 100644 index 0000000..e9f901a --- /dev/null +++ b/rt/features/tracing-brave/src/test/java/org/apache/cxf/tracing/brave/soap/MyServiceImpl.java @@ -0,0 +1,28 @@ +/** + * 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.brave.soap; + +public class MyServiceImpl implements MyService { + + @Override + public String echo(String msg) { + return msg; + } + +}
