Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide spring autoconfiguration for the API extensions tracer #11

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions opentracing-api-extensions-tracer-spring-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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.

-->
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>opentracing-api-extensions-parent</artifactId>
<groupId>io.opentracing.contrib</groupId>
<version>0.0.3-SNAPSHOT</version>
</parent>

<artifactId>opentracing-api-extensions-tracer-spring-autoconfigure</artifactId>

<dependencies>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-api-extensions</artifactId>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-api-extensions-tracer</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${version.org.springframework.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${version.org.springframework.boot}</version>
</dependency>

<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${version.org.springframework.boot}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.org.springframework.boot}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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<TracerObserver> 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC spring does not autowire null so this might be not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the produces @bean method returns a null, then it does get included in the set unfortunately. I did have a look for a solution, but couldn't find one. If one is found later then it can be updated in a separate PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it works when autowire uses required = false. https://stackoverflow.com/a/26483356/4158442

I was wondering because some time ago I wanted to create a null bean a spring was complaining...

observerFound = true;
tracer.addTracerObserver(observer);
}
}
if (observerFound) {
log.info("Use extensions API tracer to wrap tracer=" + bean + " with observers=" + tracerObservers);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't know how toString on this set is defined, maybe just add a log statement on each observer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It currently just outputs a comma separate list of the toString for each observer - so depends whether the observer reports something meaningful - but it would have the same situation if having separate log statements, so not sure it would be a benefit.

return tracer;
}
}
return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.opentracing.contrib.api.tracer.spring.autoconfigure.TracerBeanPostProcessor
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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));
}

}
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<modules>
<module>opentracing-api-extensions</module>
<module>opentracing-api-extensions-tracer</module>
<module>opentracing-api-extensions-tracer-spring-autoconfigure</module>
</modules>

<properties>
Expand All @@ -69,6 +70,7 @@
<version.io.opentracing>0.30.0</version.io.opentracing>
<version.junit>4.12</version.junit>
<version.org.mockito-mockito-all>1.10.19</version.org.mockito-mockito-all>
<version.org.springframework.boot>1.4.1.RELEASE</version.org.springframework.boot>

<version.io.takari-maven>0.3.4</version.io.takari-maven>
<version.io.zikin.centralsync-maven-plugin>0.1.0</version.io.zikin.centralsync-maven-plugin>
Expand All @@ -86,12 +88,27 @@
<artifactId>opentracing-api</artifactId>
<version>${version.io.opentracing}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<version>${version.io.opentracing}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>${version.io.opentracing}</version>
</dependency>

<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-api-extensions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-api-extensions-tracer</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -165,6 +182,7 @@
<exclude>mvnw.cmd</exclude>
<exclude>travis/publish.sh</exclude>
<exclude>.mvn/wrapper/maven-wrapper.properties</exclude>
<exclude>**/*.factories</exclude>
</excludes>
</configuration>
<executions>
Expand Down