Skip to content

Commit

Permalink
Replace ServiceLoader code by TracerResolver call. (#12)
Browse files Browse the repository at this point in the history
* Replace ServiceLoader code by TracerResolver call.

* Lazy initialization only if util-GlobalTracer isn't already registered.

Since this requires OT-api 0.22.0:
Merge remote-tracking branch 'remotes/upstream/master' into tracerresolver

# Conflicts:
#	pom.xml
  • Loading branch information
sjoerdtalsma authored May 5, 2017
1 parent b99b817 commit 9cd8a16
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Provides the `GlobalTracer.get()` method that returns the singleton _global trac

When the tracer is needed it is lazily looked up using the following rules:
1. The tracer from the last `register(tracer)` call always takes precedence.</li>
2. If no tracer was registered, one resolved by the [TracerResolver].
3. If no single implementation is found, the `NoopTracer` will be used.
2. If no tracer was registered, one is resolved by the [TracerResolver].
3. If no Tracer is resolved, the `NoopTracer` will be used.

## How to use this library
Some examples on how this library can be used:
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<build.java.version>1.6</build.java.version>

<opentracing-api.version>0.22.0</opentracing-api.version>
<tracerresolver.version>0.1.0</tracerresolver.version>
<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
<mockito.version>1.10.19</mockito.version>
Expand Down Expand Up @@ -105,6 +106,11 @@
<artifactId>opentracing-util</artifactId>
<version>${opentracing-api.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-tracerresolver</artifactId>
<version>${tracerresolver.version}</version>
</dependency>

<dependency>
<groupId>io.opentracing</groupId>
Expand Down
49 changes: 11 additions & 38 deletions src/main/java/io/opentracing/contrib/global/GlobalTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
package io.opentracing.contrib.global;

import io.opentracing.NoopTracer;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import io.opentracing.contrib.tracerresolver.TracerResolver;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -27,14 +25,13 @@
* Forwards all methods to another tracer that can be configured in one of two ways:
* <ol>
* <li>Explicitly, calling {@link #register(Tracer)} with a configured tracer, or:</li>
* <li>Automatically using the Java {@link ServiceLoader} SPI mechanism to load a {@link Tracer} from the classpath.</li>
* <li>Automatically using the {@link TracerResolver} to load a {@link Tracer} from the classpath.</li>
* </ol>
* <p>
* When the tracer is needed it is lazily looked up using the following rules:
* <ol type="a">
* <li>The {@link #register(Tracer) registered} tracer always takes precedence.</li>
* <li>If no tracer was registered, one is looked up from the {@link ServiceLoader}.<br>
* The {@linkplain GlobalTracer} will not attempt to choose between implementations:</li>
* <li>If no tracer was registered, it is resolved by the {@link TracerResolver}.</li>
* <li>If no single tracer service is found, the {@link io.opentracing.NoopTracer NoopTracer} will be used.</li>
* </ol>
*
Expand All @@ -50,16 +47,15 @@ private GlobalTracer() {
}

private static void lazyInit() {
if (SINGLE_INIT.compareAndSet(false, true)) {
final Tracer resolved = loadSingleSpiImplementation();
if (!(resolved instanceof NoopTracer)) {
try {
if (SINGLE_INIT.compareAndSet(false, true) && !io.opentracing.util.GlobalTracer.isRegistered()) {
try {
final Tracer resolved = TracerResolver.resolveTracer();
if (resolved != null && !(resolved instanceof NoopTracer)) {
io.opentracing.util.GlobalTracer.register(resolved);
LOGGER.log(Level.INFO, "Using GlobalTracer: {0}.", resolved);
} catch (RuntimeException alreadyRegistered) {
LOGGER.log(Level.WARNING, "Could not automatically register " + resolved + " because: "
+ alreadyRegistered.getMessage(), alreadyRegistered);
}
} catch (RuntimeException rte) {
LOGGER.log(Level.WARNING, "Exception resolving and registering the GlobalTracer: " + rte.getMessage(), rte);
}
}
}
Expand All @@ -68,11 +64,8 @@ private static void lazyInit() {
* Returns the constant {@linkplain GlobalTracer}.
* <p>
* All methods are forwarded to the currently configured tracer.<br>
* Until a tracer is {@link #register(Tracer) explicitly configured},
* one is looked up from the {@link ServiceLoader},
* falling back to the {@link io.opentracing.NoopTracer NoopTracer}.<br>
* A tracer can be re-configured at any time.
* For example, the tracer used to extract a span may be different than the one that injects it.
* Unless a tracer is {@link #register(Tracer) explicitly configured},
* one is resolved by the {@link TracerResolver}.<br>
*
* @return The global tracer constant.
* @see io.opentracing.util.GlobalTracer#get()
Expand All @@ -98,24 +91,4 @@ public static Tracer register(final Tracer tracer) {
return null; // no way to return the previous instance
}

/**
* Loads a single service implementation from {@link ServiceLoader}.
*
* @return The single service or a NoopTracer.
*/
private static Tracer loadSingleSpiImplementation() {
// Use the ServiceLoader to find the declared Tracer implementation.
Iterator<Tracer> spiImplementations =
ServiceLoader.load(Tracer.class, Tracer.class.getClassLoader()).iterator();
if (spiImplementations.hasNext()) {
Tracer foundImplementation = spiImplementations.next();
if (!spiImplementations.hasNext()) {
return foundImplementation;
}
LOGGER.log(Level.WARNING, "More than one Tracer service found. " +
"Falling back to NoopTracer implementation.");
}
return NoopTracerFactory.create();
}

}

0 comments on commit 9cd8a16

Please sign in to comment.