You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2018/09/23 17:51:44 UTC
[cxf] 04/05: CXF-7844: Change Zipkin/Brave XML to use
brave-spring-beans
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit f3f798e20a36244c94bd744beb6521d78bc93256
Author: reta <dr...@gmail.com>
AuthorDate: Fri Sep 21 08:16:30 2018 -0400
CXF-7844: Change Zipkin/Brave XML to use brave-spring-beans
---
.../samples/jax_rs/tracing_brave/README.txt | 8 +++
.../release/samples/jax_rs/tracing_brave/pom.xml | 12 +++-
.../java/demo/jaxrs/tracing/client/Client.java | 9 ++-
.../jaxrs/tracing/server/CatalogApplication.java | 4 +-
.../demo/jaxrs/tracing/server/CatalogTracing.java | 72 ++++++++++++++++++++++
.../samples/jax_rs/tracing_brave_osgi/README.txt | 13 +++-
.../tracing/server/CatalogTracingFactory.java} | 37 +++++------
.../main/resources/OSGI-INF/blueprint/context.xml | 11 +++-
.../samples/jaxws_tracing_brave_osgi/README.txt | 7 +++
.../tracing/server/CatalogTracingFactory.java} | 39 +++++-------
.../tracing/server/impl/CatalogServiceImpl.java | 5 +-
.../main/resources/OSGI-INF/blueprint/context.xml | 15 ++---
.../karaf/features/src/main/resources/features.xml | 1 -
13 files changed, 174 insertions(+), 59 deletions(-)
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/README.txt b/distribution/src/main/release/samples/jax_rs/tracing_brave/README.txt
index ceb779f..37a911e 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave/README.txt
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave/README.txt
@@ -13,6 +13,10 @@ following services at URL http://localhost:9000/catalog:
Building and running the demo using Maven
---------------------------------------
+Run OpenZipkin server (or point CatalogTracing to the existing one):
+
+ docker run -d -p 9411:9411 openzipkin/zipkin
+
From the base directory of this sample (i.e., where this README file is
located), the Maven pom.xml file can be used to build and run the demo.
@@ -26,5 +30,9 @@ Using either UNIX or Windows:
To remove the target dir, run mvn clean".
+Navigate to Zipkin UI to explore the traces (or point to existing deployment):
+
+ http://localhost:9411/
+
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/pom.xml b/distribution/src/main/release/samples/jax_rs/tracing_brave/pom.xml
index 7bb7427..2ca44c1 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave/pom.xml
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave/pom.xml
@@ -118,7 +118,7 @@
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
- </dependency>
+ </dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
@@ -127,5 +127,15 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
</dependency>
+ <dependency>
+ <groupId>io.zipkin.reporter2</groupId>
+ <artifactId>zipkin-sender-okhttp3</artifactId>
+ <version>2.7.8</version>
+ </dependency>
+ <dependency>
+ <groupId>io.zipkin.brave</groupId>
+ <artifactId>brave-context-slf4j</artifactId>
+ <version>5.3.3</version>
+ </dependency>
</dependencies>
</project>
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/client/Client.java b/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/client/Client.java
index a1fc3a6..42784c2 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/client/Client.java
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/client/Client.java
@@ -27,15 +27,15 @@ import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.tracing.brave.jaxrs.BraveClientProvider;
-import brave.Tracing;
+import demo.jaxrs.tracing.server.CatalogTracing;
public final class Client {
private Client() {
}
public static void main(final String[] args) throws Exception {
- final Tracing brave = Tracing.newBuilder().build();
- final BraveClientProvider provider = new BraveClientProvider(brave);
+ final CatalogTracing tracing = new CatalogTracing("catalog-client");
+ final BraveClientProvider provider = new BraveClientProvider(tracing.getHttpTracing());
final Response response = WebClient
.create("http://localhost:9000/catalog", Arrays.asList(provider))
@@ -44,5 +44,8 @@ public final class Client {
System.out.println(response.readEntity(String.class));
response.close();
+
+ // Give the tracer the time to flush traces (since we are using async reporter)
+ Thread.sleep(1000);
}
}
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java b/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
index f0c5032..5363a35 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
@@ -32,12 +32,14 @@ import org.apache.cxf.tracing.brave.jaxrs.BraveFeature;
@ApplicationPath("/")
public class CatalogApplication extends Application {
+ private final CatalogTracing tracing = new CatalogTracing("catalog-server");
+
@Override
public Set<Object> getSingletons() {
return new HashSet<>(
Arrays.asList(
new Catalog(),
- new BraveFeature(),
+ new BraveFeature(tracing.getHttpTracing()),
new JsrJsonpProvider()
)
);
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogTracing.java b/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogTracing.java
new file mode 100644
index 0000000..5675642
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogTracing.java
@@ -0,0 +1,72 @@
+/**
+ * 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 demo.jaxrs.tracing.server;
+
+import brave.Tracing;
+import brave.context.slf4j.MDCScopeDecorator;
+import brave.http.HttpTracing;
+import brave.propagation.ThreadLocalCurrentTraceContext;
+import zipkin2.Span;
+import zipkin2.reporter.AsyncReporter;
+import zipkin2.reporter.Sender;
+import zipkin2.reporter.okhttp3.OkHttpSender;
+
+public class CatalogTracing {
+ private volatile HttpTracing httpTracing;
+ private final String serviceName;
+
+ public CatalogTracing(final String serviceName) {
+ this.serviceName = serviceName;
+ }
+
+ public HttpTracing getHttpTracing() {
+ HttpTracing result = httpTracing;
+
+ if (result == null) {
+ synchronized(this) {
+ result = httpTracing;
+ if (result == null) {
+ httpTracing = result = createHttpTracing();
+ }
+ }
+ }
+
+ return result;
+ }
+
+ private HttpTracing createHttpTracing() {
+ final Sender sender = OkHttpSender.create("http://localhost:9411/api/v2/spans");
+ final AsyncReporter<Span> reporter = AsyncReporter.create(sender);
+
+ final Tracing tracing = Tracing
+ .newBuilder()
+ .localServiceName(serviceName)
+ .currentTraceContext(
+ ThreadLocalCurrentTraceContext
+ .newBuilder()
+ .addScopeDecorator(MDCScopeDecorator.create())
+ .build()
+ )
+ .spanReporter(reporter)
+ .build();
+
+ return HttpTracing.create(tracing);
+ }
+}
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/README.txt b/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/README.txt
index 1e2e5a1..913eaee 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/README.txt
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/README.txt
@@ -22,6 +22,10 @@ located), the Maven pom.xml file can be used to build and run the demo.
Using either UNIX or Windows:
mvn install
+
+Run OpenZipkin server (or point OSGI-INF/blueprint/context.xml to the existing one):
+
+ docker run -d -p 9411:9411 openzipkin/zipkin
Starting Karaf (refer to http://karaf.apache.org/manual/latest-4.0.x/quick-start.html)
@@ -56,6 +60,9 @@ for this demo bundle.
Install this demo bundle (using the appropriate bundle version number)
+ install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.okio/1.15.0_1
+ install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.okhttp/3.11.0_1
+ install -s mvn:io.zipkin.reporter2/zipkin-sender-okhttp3/2.7.8
install -s mvn:org.apache.cxf.samples/jax_rs_tracing_brave_osgi/3.n.m
You can verify if the CXF JAX-RS OpenZipkin Brave Blueprint Demo is installed and started.
@@ -69,4 +76,8 @@ You can verify if the CXF JAX-RS OpenZipkin Brave Blueprint Demo is installed an
Now, you will be able to access this CXF JAXRS demo service on your Karaf instance at
- http://localhost:8181/cxf/catalog
\ No newline at end of file
+ http://localhost:8181/cxf/catalog
+
+Navigate to Zipkin UI to explore the traces (or point to existing deployment):
+
+ http://localhost:9411/
\ No newline at end of file
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java b/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/java/demo/jaxrs/tracing/server/CatalogTracingFactory.java
similarity index 58%
copy from distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
copy to distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/java/demo/jaxrs/tracing/server/CatalogTracingFactory.java
index f0c5032..bb7c498 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/java/demo/jaxrs/tracing/server/CatalogTracingFactory.java
@@ -19,27 +19,22 @@
package demo.jaxrs.tracing.server;
+import brave.Tracing;
+import brave.propagation.ThreadLocalCurrentTraceContext;
+import zipkin2.reporter.AsyncReporter;
+import zipkin2.reporter.Sender;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-import javax.ws.rs.ApplicationPath;
-import javax.ws.rs.core.Application;
-
-import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
-import org.apache.cxf.tracing.brave.jaxrs.BraveFeature;
-
-@ApplicationPath("/")
-public class CatalogApplication extends Application {
- @Override
- public Set<Object> getSingletons() {
- return new HashSet<>(
- Arrays.asList(
- new Catalog(),
- new BraveFeature(),
- new JsrJsonpProvider()
- )
- );
+public class CatalogTracingFactory {
+ public static Tracing create(final String serviceName, final Sender sender) {
+ return Tracing
+ .newBuilder()
+ .localServiceName(serviceName)
+ .currentTraceContext(
+ ThreadLocalCurrentTraceContext
+ .newBuilder()
+ .build()
+ )
+ .spanReporter(AsyncReporter.create(sender))
+ .build();
}
}
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml b/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml
index 194f96e..3cfc9ca 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml
+++ b/distribution/src/main/release/samples/jax_rs/tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml
@@ -27,11 +27,20 @@
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd">
+ <bean id="sender" class="zipkin2.reporter.okhttp3.OkHttpSender" factory-method="create">
+ <argument index="0" value="http://localhost:9411/api/v2/spans" />
+ </bean>
+
+ <bean id="tracing" class="demo.jaxrs.tracing.server.CatalogTracingFactory" factory-method="create">
+ <argument index="0" value="catalog-service" />
+ <argument index="1" ref="sender" />
+ </bean>
+
<!-- JAXRS providers -->
<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider" />
<bean id="braveFeature" class="org.apache.cxf.tracing.brave.jaxrs.BraveFeature">
- <argument index="0" value="catalog-service" />
+ <argument index="0" ref="tracing" />
</bean>
<!-- Application resources -->
diff --git a/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/README.txt b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/README.txt
index 959befa..ca97a63 100644
--- a/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/README.txt
+++ b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/README.txt
@@ -22,6 +22,10 @@ Using either UNIX or Windows:
mvn install
+Run OpenZipkin server (or point OSGI-INF/blueprint/context.xml to the existing one):
+
+ docker run -d -p 9411:9411 openzipkin/zipkin
+
Starting Karaf (refer to http://karaf.apache.org/manual/latest-4.0.x/quick-start.html)
bin/karaf
@@ -55,6 +59,9 @@ for this demo bundle.
Install this demo bundle (using the appropriate bundle version number)
+ install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.okio/1.15.0_1
+ install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.okhttp/3.11.0_1
+ install -s mvn:io.zipkin.reporter2/zipkin-sender-okhttp3/2.7.8
install -s mvn:org.apache.cxf.samples/jax_ws_tracing_brave_osgi/3.n.m
You can verify if the CXF JAX-RS OpenZipkin Brave Blueprint Demo is installed and started.
diff --git a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/CatalogTracingFactory.java
similarity index 56%
copy from distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
copy to distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/CatalogTracingFactory.java
index f0c5032..34b5434 100644
--- a/distribution/src/main/release/samples/jax_rs/tracing_brave/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java
+++ b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/CatalogTracingFactory.java
@@ -17,29 +17,24 @@
* under the License.
*/
-package demo.jaxrs.tracing.server;
+package demo.jaxws.tracing.server;
+import brave.Tracing;
+import brave.propagation.ThreadLocalCurrentTraceContext;
+import zipkin2.reporter.AsyncReporter;
+import zipkin2.reporter.Sender;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-import javax.ws.rs.ApplicationPath;
-import javax.ws.rs.core.Application;
-
-import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
-import org.apache.cxf.tracing.brave.jaxrs.BraveFeature;
-
-@ApplicationPath("/")
-public class CatalogApplication extends Application {
- @Override
- public Set<Object> getSingletons() {
- return new HashSet<>(
- Arrays.asList(
- new Catalog(),
- new BraveFeature(),
- new JsrJsonpProvider()
- )
- );
+public class CatalogTracingFactory {
+ public static Tracing create(final String serviceName, final Sender sender) {
+ return Tracing
+ .newBuilder()
+ .localServiceName(serviceName)
+ .currentTraceContext(
+ ThreadLocalCurrentTraceContext
+ .newBuilder()
+ .build()
+ )
+ .spanReporter(AsyncReporter.create(sender))
+ .build();
}
}
diff --git a/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/impl/CatalogServiceImpl.java b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/impl/CatalogServiceImpl.java
index 7ec1883..b899614 100644
--- a/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/impl/CatalogServiceImpl.java
+++ b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/java/demo/jaxws/tracing/server/impl/CatalogServiceImpl.java
@@ -19,12 +19,14 @@
package demo.jaxws.tracing.server.impl;
import brave.ScopedSpan;
+
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import brave.Tracing;
+import brave.propagation.TraceContext;
import demo.jaxws.tracing.server.Book;
import demo.jaxws.tracing.server.CatalogService;
@@ -38,8 +40,9 @@ public class CatalogServiceImpl implements CatalogService {
}
public void addBook(Book book) {
+ final TraceContext parent = brave.tracer().currentSpan().context();
executor.submit(() -> {
- final ScopedSpan span = brave.tracer().startScopedSpan("Inserting New Book");
+ final ScopedSpan span = brave.tracer().startScopedSpanWithParent("Inserting New Book", parent);
try {
books.put(book.getId(), book);
} finally {
diff --git a/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml
index 95110ed..190c72b 100644
--- a/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml
+++ b/distribution/src/main/release/samples/jaxws_tracing_brave_osgi/src/main/resources/OSGI-INF/blueprint/context.xml
@@ -29,7 +29,7 @@
<!-- CXF BraveFeature -->
<bean id="braveFeature" class="org.apache.cxf.tracing.brave.BraveFeature">
- <argument index="0" ref="brave" />
+ <argument index="0" ref="tracing" />
</bean>
<cxf:bus>
@@ -39,17 +39,18 @@
</cxf:bus>
<bean id="catalogServiceImpl" class="demo.jaxws.tracing.server.impl.CatalogServiceImpl">
- <argument index="0" ref="brave" />
+ <argument index="0" ref="tracing" />
</bean>
- <bean id="braveBuilder" class="brave.Tracing" factory-method="newBuilder" />
+ <bean id="sender" class="zipkin2.reporter.okhttp3.OkHttpSender" factory-method="create">
+ <argument index="0" value="http://localhost:9411/api/v2/spans" />
+ </bean>
- <bean id="braveCatalogBuilder" factory-ref="braveBuilder" factory-method="localServiceName">
+ <bean id="tracing" class="demo.jaxws.tracing.server.CatalogTracingFactory" factory-method="create">
<argument index="0" value="catalog-service" />
+ <argument index="1" ref="sender" />
</bean>
-
- <bean id="brave" factory-ref="braveCatalogBuilder" factory-method="build" />
-
+
<jaxws:endpoint
implementor="#catalogServiceImpl"
address="/catalog"
diff --git a/osgi/karaf/features/src/main/resources/features.xml b/osgi/karaf/features/src/main/resources/features.xml
index be59c45..a5b0758 100644
--- a/osgi/karaf/features/src/main/resources/features.xml
+++ b/osgi/karaf/features/src/main/resources/features.xml
@@ -294,7 +294,6 @@
</feature>
<feature name="cxf-tracing-brave" version="${project.version}">
<feature version="${project.version}">cxf-core</feature>
- <bundle start-level="10" dependency="true">mvn:com.google.code.findbugs/jsr305/${cxf.findbugs.version}</bundle>
<bundle start-level="35">mvn:io.zipkin.zipkin2/zipkin/${cxf.brave.zipkin.version}</bundle>
<bundle start-level="35">mvn:io.zipkin.reporter2/zipkin-reporter/${cxf.brave.reporter.version}</bundle>
<bundle start-level="35">mvn:io.zipkin.brave/brave/${cxf.brave.version}</bundle>