-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
observerFound = true; | ||
tracer.addTracerObserver(observer); | ||
} | ||
} | ||
if (observerFound) { | ||
log.info("Use extensions API tracer to wrap tracer=" + bean + " with observers=" + tracerObservers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I don't know how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It currently just outputs a comma separate list of the |
||
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)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
usesrequired = false
. https://stackoverflow.com/a/26483356/4158442I was wondering because some time ago I wanted to create a null bean a spring was complaining...