Skip to content

Instantly share code, notes, and snippets.

@wu-sheng
Created December 21, 2016 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wu-sheng/8625316f549e084904c84ab11a6dd7f0 to your computer and use it in GitHub Desktop.
Save wu-sheng/8625316f549e084904c84ab11a6dd7f0 to your computer and use it in GitHub Desktop.
ServiceLoader of OT tracer
package io.opentracing.contrib;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by wusheng on 2016/12/21.
*/
public class TracerLoader {
private static final Logger LOGGER = Logger.getLogger(TracerLoader.class.getName());
/**
* Use ServiceLoader to get a tracer instance.
* The mechanism comes from many rpc framework and globalTracer-java,
* if more than one implementations of tracer, would not choose any of them.
*
* @return
*/
public static Tracer load() {
Iterator<Tracer> tracerIterator = ServiceLoader.load(Tracer.class).iterator();
Tracer tracer = null;
if (tracerIterator.hasNext()) {
tracer = tracerIterator.next();
if (tracerIterator.hasNext()) {
LOGGER.log(Level.WARNING, "More than one Tracer service implementation found. " + "Falling back to NoopTracer implementation.");
tracer = NoopTracerFactory.create();
}
} else {
tracer = NoopTracerFactory.create();
}
return tracer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment