diff --git a/README.md b/README.md index 7598ba3..88f1a90 100644 --- a/README.md +++ b/README.md @@ -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. - 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: diff --git a/pom.xml b/pom.xml index f795c26..19267c9 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ 1.6 0.22.0 + 0.1.0 4.12 1.3 1.10.19 @@ -105,6 +106,11 @@ opentracing-util ${opentracing-api.version} + + io.opentracing.contrib + opentracing-tracerresolver + ${tracerresolver.version} + io.opentracing diff --git a/src/main/java/io/opentracing/contrib/global/GlobalTracer.java b/src/main/java/io/opentracing/contrib/global/GlobalTracer.java index 919befd..ff9e5ec 100644 --- a/src/main/java/io/opentracing/contrib/global/GlobalTracer.java +++ b/src/main/java/io/opentracing/contrib/global/GlobalTracer.java @@ -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; @@ -27,14 +25,13 @@ * Forwards all methods to another tracer that can be configured in one of two ways: *
    *
  1. Explicitly, calling {@link #register(Tracer)} with a configured tracer, or:
  2. - *
  3. Automatically using the Java {@link ServiceLoader} SPI mechanism to load a {@link Tracer} from the classpath.
  4. + *
  5. Automatically using the {@link TracerResolver} to load a {@link Tracer} from the classpath.
  6. *
*

* When the tracer is needed it is lazily looked up using the following rules: *

    *
  1. The {@link #register(Tracer) registered} tracer always takes precedence.
  2. - *
  3. If no tracer was registered, one is looked up from the {@link ServiceLoader}.
    - * The {@linkplain GlobalTracer} will not attempt to choose between implementations:
  4. + *
  5. If no tracer was registered, it is resolved by the {@link TracerResolver}.
  6. *
  7. If no single tracer service is found, the {@link io.opentracing.NoopTracer NoopTracer} will be used.
  8. *
* @@ -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); } } } @@ -68,11 +64,8 @@ private static void lazyInit() { * Returns the constant {@linkplain GlobalTracer}. *

* All methods are forwarded to the currently configured tracer.
- * 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}.
- * 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}.
* * @return The global tracer constant. * @see io.opentracing.util.GlobalTracer#get() @@ -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 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(); - } - }