Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions opentracing-api-extensions-tracer-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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-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>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${version.commons-logging}</version>
<scope>provided</scope>
</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.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 with observers=" + tracerObservers);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe log original tracer somewhere in that message as well, as it might be the last chance to do so 😉 ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BTW: why is commons logging used? Is it an integral dependency already available from Spring?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Its the preferred logger for spring (boot).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Regarding 'spring' in the name - possibly but just made it longer - not sure.

return tracer;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a chance that this tracer is different than the one currently available via GlobalTracer.get(), right? Is that on purpose?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Its possible - but I think we need to separate the stages - tracer resolution using whatever mechanism is appropriate for the runtime (so Bean in spring), and the next step is to set that in the GlobalTracer for global access.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@objectiser I think @pavolloffay has a point in case there's a pre-registered GlobalTracer.
I agree with him that that should always take precedence in this case.
I added PR for review: opentracing-contrib/java-tracerresolver#16

As it happens I also agree with you that registering the GlobalTracer should happen after the resolving (and potential wrapping).

}
}
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.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.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.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));
}

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

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<version.commons-logging>1.2</version.commons-logging>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be consumed from spring?

<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.3.RELEASE</version.org.springframework.boot>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

minor thing could we use 1.4.1?


<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 +89,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 +183,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