-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TracerConverter (Wrapper) support (#8)
* Check license header as part of build. * Initial rough version of Wrapper support (without @priority order). * Isolate Resolver concept from Wrapper changes. * Add a short notice about tracer wrappers. * Move warning (about wrapping) from TracerResolver into Wrappers class. * Add missing license header. * Add braces around if. * Rename TracerWrapperProvider to TracerConverter. * Outline potential 'wrapper' use of the tracer converter * Update JavaDoc example when converter can recieve a null tracer. * Don't call converter if resolved Tracer is null. * Just break out of the loop (thanks @objectiser) * Document priority of TracerResolver. * Document priority concept in javadoc of TracerConverter as well. * Don't begin loop in the first place if resolved == null. * Fix illegal JavaDoc. * Add unit-tests for TracerConverter. Including priority-order, returning nulls and throwing exceptions.
- Loading branch information
1 parent
01c4718
commit 8eb083e
Showing
6 changed files
with
244 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/io/opentracing/contrib/tracerresolver/TracerConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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.tracerresolver; | ||
|
||
import io.opentracing.Tracer; | ||
|
||
/** | ||
* Function converting an existing {@link Tracer}. | ||
* <p> | ||
* This can be useful for <em>wrapping</em> tracers: | ||
* <pre><code> | ||
* public final class FooWrapperConverter implements TracerConverter { | ||
* public Tracer convert(Tracer existingTracer) { | ||
* return new FooTracerWrapper(existingTracer); | ||
* } | ||
* } | ||
* </code></pre> | ||
* <p> | ||
* If there are multiple {@linkplain TracerConverter} implementations resolved, | ||
* they will be applied in the order of their {@literal @}Priority annotation: | ||
* <ol> | ||
* <li>First, non-negative priority is applied in natural order (e.g. {@code 0}, {@code 1}, {@code 2}, ...).</li> | ||
* <li>Next, objects without <code>{@literal @}Priority</code> annotation are applied | ||
* by assigning a <em>default priority</em> of {@link Integer#MAX_VALUE}.</li> | ||
* <li>Finally, negative priority is applied in reverse-natural order (e.g. {@code -1}, {@code -2}, {@code -3}, ...).</li> | ||
* </ol> | ||
* <p> | ||
* The order of objects with equal (implicit) priority is undefined. | ||
* | ||
* @author Sjoerd Talsma | ||
*/ | ||
public interface TracerConverter { | ||
|
||
/** | ||
* Function that converts a {@link Tracer}. | ||
* <p> | ||
* It may either manipulate the tracer or return an entirely new {@linkplain Tracer} instance. | ||
* | ||
* @param existingTracer The existing tracer to be converted. | ||
* @return The converted tracer. | ||
*/ | ||
Tracer convert(Tracer existingTracer); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/test/java/io/opentracing/contrib/tracerresolver/TracerConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.tracerresolver; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static io.opentracing.contrib.tracerresolver.TracerResolverTest.writeServiceFile; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
public class TracerConverterTest { | ||
private static final File SERVICES_DIR = new File("target/test-classes/META-INF/services/"); | ||
|
||
@After | ||
public void cleanServiceFiles() throws IOException { | ||
new File(SERVICES_DIR, TracerResolver.class.getName()).delete(); | ||
new File(SERVICES_DIR, TracerConverter.class.getName()).delete(); | ||
} | ||
|
||
@Before | ||
@After | ||
public void resetTracerResolver() { | ||
Mocks.calledConverterTypes.clear(); | ||
TracerResolver.reload(); | ||
} | ||
|
||
@Test | ||
public void testConverterNotCalledWhenNothingRegistered() throws IOException { | ||
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class); | ||
assertThat("No tracer should be resolved", TracerResolver.resolveTracer(), is(nullValue())); | ||
assertThat("No converter should be called", Mocks.calledConverterTypes, hasSize(0)); | ||
} | ||
|
||
@Test | ||
public void testTrivialConverterExample() throws IOException { | ||
writeServiceFile(TracerResolver.class, Mocks.MockTracerResolver.class); | ||
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class); | ||
|
||
assertThat("Tracer must be resolved", TracerResolver.resolveTracer(), instanceOf(Mocks.ResolvedTracer.class)); | ||
assertThat("Converter must be called", Mocks.calledConverterTypes, contains((Class) Mocks.IdentityConverter.class)); | ||
} | ||
|
||
@Test | ||
public void testConverterNotCalledWhenProviderReturnsNull() throws IOException { | ||
writeServiceFile(TracerResolver.class, Mocks.NullTracerResolver.class); | ||
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class); | ||
|
||
assertThat("Resolved tracer", TracerResolver.resolveTracer(), is(nullValue())); | ||
assertThat("No converter should be called", Mocks.calledConverterTypes, hasSize(0)); | ||
} | ||
|
||
@Test | ||
public void testConverterThrowingException() throws IOException { | ||
writeServiceFile(TracerResolver.class, Mocks.MockTracerResolver.class); | ||
// although identity converter is listed first, the @Priority(5) should let throwing converter be called first! | ||
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class, Mocks.Prio5_ThrowingConverter.class); | ||
|
||
assertThat("Resolved tracer", TracerResolver.resolveTracer(), instanceOf(Mocks.ResolvedTracer.class)); | ||
assertThat("Continue after exception", Mocks.calledConverterTypes, hasSize(2)); | ||
assertThat("Predictable order", Mocks.calledConverterTypes, | ||
contains((Class) Mocks.Prio5_ThrowingConverter.class, Mocks.IdentityConverter.class)); | ||
} | ||
|
||
@Test | ||
public void testconvertToNull() throws IOException { | ||
writeServiceFile(TracerResolver.class, Mocks.MockTracerResolver.class); | ||
// although identity converter is listed first, the @Priority(10) should let convertToNull be called first! | ||
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class, Mocks.Prio10_ConvertToNull.class); | ||
|
||
assertThat("Tracer after conversion(s)", TracerResolver.resolveTracer(), is(nullValue())); | ||
assertThat("Second converter shouldn't be called with <null>", Mocks.calledConverterTypes, hasSize(1)); | ||
assertThat("Prioritized converter", Mocks.calledConverterTypes, contains((Class) Mocks.Prio10_ConvertToNull.class)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters