From 2da06d181a5db09435bd2eda90b70dfbb373f841 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Mon, 7 Aug 2017 17:07:15 +0100 Subject: [PATCH 1/3] Provide spring autoconfiguration for the API extensions tracer --- .../pom.xml | 75 ++++++++++++++++++ .../TracerBeanPostProcessor.java | 61 +++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + ...racerBeanPostProcessorNoObserversTest.java | 51 +++++++++++++ .../TracerBeanPostProcessorTest.java | 76 +++++++++++++++++++ pom.xml | 19 +++++ 6 files changed, 284 insertions(+) create mode 100644 opentracing-api-extensions-tracer-autoconfigure/pom.xml create mode 100644 opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java create mode 100644 opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories create mode 100644 opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java create mode 100644 opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java diff --git a/opentracing-api-extensions-tracer-autoconfigure/pom.xml b/opentracing-api-extensions-tracer-autoconfigure/pom.xml new file mode 100644 index 0000000..8d6e83c --- /dev/null +++ b/opentracing-api-extensions-tracer-autoconfigure/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + opentracing-api-extensions-parent + io.opentracing.contrib + 0.0.3-SNAPSHOT + + + opentracing-api-extensions-tracer-autoconfigure + + + + io.opentracing.contrib + opentracing-api-extensions + + + io.opentracing.contrib + opentracing-api-extensions-tracer + + + + org.springframework.boot + spring-boot-autoconfigure + ${version.org.springframework.boot} + + + + commons-logging + commons-logging + ${version.commons-logging} + provided + + + + io.opentracing + opentracing-mock + test + + + io.opentracing + opentracing-util + test + + + org.springframework.boot + spring-boot-starter-test + ${version.org.springframework.boot} + test + + + org.springframework.boot + spring-boot-starter-web + ${version.org.springframework.boot} + test + + + diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java b/opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java new file mode 100644 index 0000000..be1d50b --- /dev/null +++ b/opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java @@ -0,0 +1,61 @@ +/** + * Copyright 2017 The OpenTracing Authors + * + * Licensed 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 io.opentracing.contrib.api.tracer.autoconfigure; + +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.annotation.Configuration; + +import io.opentracing.Tracer; +import io.opentracing.contrib.api.TracerObserver; +import io.opentracing.contrib.api.tracer.APIExtensionsTracer; + +@Configuration +public class TracerBeanPostProcessor implements BeanPostProcessor { + + private static final Log log = LogFactory.getLog(TracerBeanPostProcessor.class); + + @Autowired(required=false) + private Set tracerObservers; + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (tracerObservers != null && bean instanceof Tracer) { + boolean observerFound = false; + APIExtensionsTracer tracer = new APIExtensionsTracer((Tracer)bean); + for (TracerObserver observer : tracerObservers) { + if (observer != null) { + observerFound = true; + tracer.addTracerObserver(observer); + } + } + if (observerFound) { + log.info("Use extensions API tracer with observers=" + tracerObservers); + return tracer; + } + } + return bean; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; + } + +} diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories b/opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..645650c --- /dev/null +++ b/opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.opentracing.contrib.api.tracer.autoconfigure.TracerBeanPostProcessor diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java b/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java new file mode 100644 index 0000000..c48facb --- /dev/null +++ b/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java @@ -0,0 +1,51 @@ +/** + * Copyright 2017 The OpenTracing Authors + * + * Licensed 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 io.opentracing.contrib.api.tracer.autoconfigure; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import io.opentracing.Tracer; +import io.opentracing.mock.MockTracer; +import io.opentracing.util.ThreadLocalActiveSpanSource; + +@SpringBootTest( + classes = {TracerBeanPostProcessorNoObserversTest.SpringConfiguration.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class TracerBeanPostProcessorNoObserversTest { + + @Configuration + @EnableAutoConfiguration + public static class SpringConfiguration { + @Bean + public MockTracer tracer() { + return new MockTracer(new ThreadLocalActiveSpanSource()); + } + } + + @Autowired + protected Tracer tracer; + + @Test + public void testTracerNotWrapped() { + assertEquals(MockTracer.class, tracer.getClass()); + } +} diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java b/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java new file mode 100644 index 0000000..9886a1a --- /dev/null +++ b/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java @@ -0,0 +1,76 @@ +/** + * Copyright 2017 The OpenTracing Authors + * + * Licensed 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 io.opentracing.contrib.api.tracer.autoconfigure; + +import static org.junit.Assert.assertNotEquals; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import io.opentracing.Tracer; +import io.opentracing.contrib.api.SpanData; +import io.opentracing.contrib.api.TracerObserver; +import io.opentracing.mock.MockTracer; +import io.opentracing.util.ThreadLocalActiveSpanSource; + +@SpringBootTest( + classes = {TracerBeanPostProcessorTest.SpringConfiguration.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class TracerBeanPostProcessorTest { + + private static final MockTracer mockTracer = new MockTracer(new ThreadLocalActiveSpanSource()); + + private static final TracerObserver tracerObserver = Mockito.mock(TracerObserver.class); + + @Configuration + @EnableAutoConfiguration + public static class SpringConfiguration { + @Bean + public Tracer tracer() { + return mockTracer; + } + + @Bean + public TracerObserver observer() { + return tracerObserver; + } + } + + @Autowired + protected Tracer tracer; + + @Before + public void before() { + mockTracer.reset(); + } + + @Test + public void testTracerWrapped() { + assertNotEquals(MockTracer.class, tracer.getClass()); + + tracer.buildSpan("testop").startManual(); + + Mockito.verify(tracerObserver).onStart(Matchers.any(SpanData.class)); + } + +} diff --git a/pom.xml b/pom.xml index c4d3bb7..696717e 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,7 @@ opentracing-api-extensions opentracing-api-extensions-tracer + opentracing-api-extensions-tracer-autoconfigure @@ -66,9 +67,11 @@ 1.7 UTF-8 + 1.2 0.30.0 4.12 1.10.19 + 1.4.3.RELEASE 0.3.4 0.1.0 @@ -86,12 +89,27 @@ opentracing-api ${version.io.opentracing} + + io.opentracing + opentracing-mock + ${version.io.opentracing} + + + io.opentracing + opentracing-util + ${version.io.opentracing} + io.opentracing.contrib opentracing-api-extensions ${project.version} + + io.opentracing.contrib + opentracing-api-extensions-tracer + ${project.version} + @@ -165,6 +183,7 @@ mvnw.cmd travis/publish.sh .mvn/wrapper/maven-wrapper.properties + **/*.factories From d46f11ae3cc70ef5dcf775283ac73c68d0bdf6ee Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Thu, 10 Aug 2017 11:18:58 +0100 Subject: [PATCH 2/3] Updated the log message to include the tracer bean being wrapped, and added 'spring' to autoconfigure artifact id and package --- .../src/main/resources/META-INF/spring.factories | 2 -- .../pom.xml | 2 +- .../tracer/spring}/autoconfigure/TracerBeanPostProcessor.java | 4 ++-- .../src/main/resources/META-INF/spring.factories | 2 ++ .../autoconfigure/TracerBeanPostProcessorNoObserversTest.java | 2 +- .../spring}/autoconfigure/TracerBeanPostProcessorTest.java | 2 +- pom.xml | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories rename {opentracing-api-extensions-tracer-autoconfigure => opentracing-api-extensions-tracer-spring-autoconfigure}/pom.xml (96%) rename {opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer => opentracing-api-extensions-tracer-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring}/autoconfigure/TracerBeanPostProcessor.java (92%) create mode 100644 opentracing-api-extensions-tracer-spring-autoconfigure/src/main/resources/META-INF/spring.factories rename {opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer => opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring}/autoconfigure/TracerBeanPostProcessorNoObserversTest.java (96%) rename {opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer => opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring}/autoconfigure/TracerBeanPostProcessorTest.java (97%) diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories b/opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 645650c..0000000 --- a/opentracing-api-extensions-tracer-autoconfigure/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -io.opentracing.contrib.api.tracer.autoconfigure.TracerBeanPostProcessor diff --git a/opentracing-api-extensions-tracer-autoconfigure/pom.xml b/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml similarity index 96% rename from opentracing-api-extensions-tracer-autoconfigure/pom.xml rename to opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml index 8d6e83c..a868f5d 100644 --- a/opentracing-api-extensions-tracer-autoconfigure/pom.xml +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml @@ -24,7 +24,7 @@ 0.0.3-SNAPSHOT - opentracing-api-extensions-tracer-autoconfigure + opentracing-api-extensions-tracer-spring-autoconfigure diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java b/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessor.java similarity index 92% rename from opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java rename to opentracing-api-extensions-tracer-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessor.java index be1d50b..6b64829 100644 --- a/opentracing-api-extensions-tracer-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessor.java +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessor.java @@ -11,7 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package io.opentracing.contrib.api.tracer.autoconfigure; +package io.opentracing.contrib.api.tracer.spring.autoconfigure; import java.util.Set; @@ -46,7 +46,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw } } if (observerFound) { - log.info("Use extensions API tracer with observers=" + tracerObservers); + log.info("Use extensions API tracer to wrap tracer=" + bean + " with observers=" + tracerObservers); return tracer; } } diff --git a/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/resources/META-INF/spring.factories b/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..2218b4c --- /dev/null +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.opentracing.contrib.api.tracer.spring.autoconfigure.TracerBeanPostProcessor diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorNoObserversTest.java similarity index 96% rename from opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java rename to opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorNoObserversTest.java index c48facb..76b48af 100644 --- a/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorNoObserversTest.java +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorNoObserversTest.java @@ -11,7 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package io.opentracing.contrib.api.tracer.autoconfigure; +package io.opentracing.contrib.api.tracer.spring.autoconfigure; import static org.junit.Assert.assertEquals; diff --git a/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorTest.java similarity index 97% rename from opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java rename to opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorTest.java index 9886a1a..792f6db 100644 --- a/opentracing-api-extensions-tracer-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/autoconfigure/TracerBeanPostProcessorTest.java +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorTest.java @@ -11,7 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package io.opentracing.contrib.api.tracer.autoconfigure; +package io.opentracing.contrib.api.tracer.spring.autoconfigure; import static org.junit.Assert.assertNotEquals; diff --git a/pom.xml b/pom.xml index 696717e..717fe55 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ opentracing-api-extensions opentracing-api-extensions-tracer - opentracing-api-extensions-tracer-autoconfigure + opentracing-api-extensions-tracer-spring-autoconfigure From d41742f04a71287887a0c385e3a3db5358030392 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Thu, 10 Aug 2017 15:52:09 +0100 Subject: [PATCH 3/3] Remove direct dependency on commons-logging and align with spring boot 1.4.1.RELEASE --- .../pom.xml | 8 +++----- pom.xml | 3 +-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml b/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml index a868f5d..39da53c 100644 --- a/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml @@ -41,12 +41,10 @@ spring-boot-autoconfigure ${version.org.springframework.boot} - - commons-logging - commons-logging - ${version.commons-logging} - provided + org.springframework.boot + spring-boot + ${version.org.springframework.boot} diff --git a/pom.xml b/pom.xml index 717fe55..bcfe05f 100644 --- a/pom.xml +++ b/pom.xml @@ -67,11 +67,10 @@ 1.7 UTF-8 - 1.2 0.30.0 4.12 1.10.19 - 1.4.3.RELEASE + 1.4.1.RELEASE 0.3.4 0.1.0