diff --git a/thread-context/build.sbt b/thread-context/build.sbt index a1260ff..944b369 100644 --- a/thread-context/build.sbt +++ b/thread-context/build.sbt @@ -1,5 +1,5 @@ libraryDependencies ++= Seq( - "com.lucidchart" % "thread-context" % "0.5", + "com.lucidchart" % "thread-context" % "0.7", "io.opentracing" % "opentracing-api" % "0.20.7", "io.opentracing" % "opentracing-noop" % "0.20.7" ) diff --git a/thread-context/src/main/java/io/opentracing/threadcontext/ContextSpan.java b/thread-context/src/main/java/io/opentracing/threadcontext/ContextSpan.java new file mode 100644 index 0000000..101325e --- /dev/null +++ b/thread-context/src/main/java/io/opentracing/threadcontext/ContextSpan.java @@ -0,0 +1,37 @@ +package io.opentracing.threadcontext; + +import com.github.threadcontext.Context; +import com.github.threadcontext.MutableContextSupplier; +import com.github.threadcontext.ThreadLocalContextSupplier; +import com.github.threadcontext.control.TryFinallyContext; +import io.opentracing.NoopSpan; +import io.opentracing.Span; + +public class ContextSpan { + + public static final ContextSpan DEFAULT = new ContextSpan(Context.DEFAULT); + + private final ThreadLocal span; + + public ContextSpan(MutableContextSupplier contextSupplier) { + span = new ThreadLocal() { + protected Span initialValue() { + return NoopSpan.INSTANCE; + } + }; + contextSupplier.suppliers.add(new ThreadLocalContextSupplier<>(span)); + } + + public Span get() { + return span.get(); + } + + public Context set(Span span) { + return new TryFinallyContext(() -> { + Span oldValue = this.span.get(); + this.span.set(span); + return () -> this.span.set(oldValue); + }); + } + +} diff --git a/thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java b/thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java deleted file mode 100644 index 932e652..0000000 --- a/thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java +++ /dev/null @@ -1,53 +0,0 @@ -package io.opentracing.threadcontext; - -import com.github.threadcontext.Saver; -import com.github.threadcontext.ThreadContext; -import com.github.threadcontext.ThreadLocalSaver; -import io.opentracing.NoopSpan; -import io.opentracing.Span; -import java.util.concurrent.Callable; -import java.util.function.Supplier; - -public final class ThreadContextSpan { - - private static final ThreadLocal span = new ThreadLocal() { - protected Span initialValue() { - return defaultSpan.get(); - } - }; - private static final Saver saver = new ThreadLocalSaver<>(span); - static { - ThreadContext.savers.add(saver); - } - - public static Supplier defaultSpan = () -> NoopSpan.INSTANCE; - - private ThreadContextSpan() { - } - - public static Span get() { - return span.get(); - } - - public static void withSpan(Span span, Runnable runnable) { - saver.runAndRestore(() -> { - ThreadContextSpan.span.set(span); - runnable.run(); - }); - } - - public static T withSpan(Span span, Callable callable) throws Exception { - return saver.runAndRestore(() -> { - ThreadContextSpan.span.set(span); - return callable.call(); - }); - } - - public static T withSpan(Span span, Supplier supplier) throws Exception { - return saver.runAndRestore(() -> { - ThreadContextSpan.span.set(span); - return supplier.get(); - }); - } - -}