diff --git a/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml b/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml new file mode 100644 index 0000000..39da53c --- /dev/null +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml @@ -0,0 +1,73 @@ + + + + 4.0.0 + + opentracing-api-extensions-parent + io.opentracing.contrib + 0.0.3-SNAPSHOT + + + opentracing-api-extensions-tracer-spring-autoconfigure + + + + io.opentracing.contrib + opentracing-api-extensions + + + io.opentracing.contrib + opentracing-api-extensions-tracer + + + + org.springframework.boot + spring-boot-autoconfigure + ${version.org.springframework.boot} + + + org.springframework.boot + spring-boot + ${version.org.springframework.boot} + + + + 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-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessor.java b/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessor.java new file mode 100644 index 0000000..6b64829 --- /dev/null +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/main/java/io/opentracing/contrib/api/tracer/spring/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.spring.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 to wrap tracer=" + bean + " 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-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-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorNoObserversTest.java b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorNoObserversTest.java new file mode 100644 index 0000000..76b48af --- /dev/null +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/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.spring.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-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorTest.java b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/autoconfigure/TracerBeanPostProcessorTest.java new file mode 100644 index 0000000..792f6db --- /dev/null +++ b/opentracing-api-extensions-tracer-spring-autoconfigure/src/test/java/io/opentracing/contrib/api/tracer/spring/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.spring.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..bcfe05f 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,7 @@ opentracing-api-extensions opentracing-api-extensions-tracer + opentracing-api-extensions-tracer-spring-autoconfigure @@ -69,6 +70,7 @@ 0.30.0 4.12 1.10.19 + 1.4.1.RELEASE 0.3.4 0.1.0 @@ -86,12 +88,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 +182,7 @@ mvnw.cmd travis/publish.sh .mvn/wrapper/maven-wrapper.properties + **/*.factories