diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-07-25 16:21:13 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-07-25 16:21:13 +0200 |
commit | 0930e36def6ce62c55d30d744b41ef475374a541 (patch) | |
tree | b3699d8932608ad48cdc8e91a16b0d5ee7541e4d /kamon-core/src/main/scala/kamon/trace/ActiveSpanSource.scala | |
parent | 5c8a8d169858b83a059c89e48cb43a41040788b8 (diff) | |
download | Kamon-0930e36def6ce62c55d30d744b41ef475374a541.tar.gz Kamon-0930e36def6ce62c55d30d744b41ef475374a541.tar.bz2 Kamon-0930e36def6ce62c55d30d744b41ef475374a541.zip |
try an alternative approach to active span management
Diffstat (limited to 'kamon-core/src/main/scala/kamon/trace/ActiveSpanSource.scala')
-rw-r--r-- | kamon-core/src/main/scala/kamon/trace/ActiveSpanSource.scala | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/trace/ActiveSpanSource.scala b/kamon-core/src/main/scala/kamon/trace/ActiveSpanSource.scala new file mode 100644 index 00000000..f4a363a6 --- /dev/null +++ b/kamon-core/src/main/scala/kamon/trace/ActiveSpanSource.scala @@ -0,0 +1,46 @@ +package kamon.trace + + +trait Scope extends AutoCloseable { + def close(): Unit +} + +trait ActiveSpanSource { + def activeSpan(): Span + + def activate(span: Span): Scope + def activate(span: Span, finishOnClose: Boolean): Scope +} + +object ActiveSpanSource { + + final class ThreadLocalBased extends ActiveSpanSource { + private val emptySpan = Span.Empty(this) + private val storage: ThreadLocal[Span] = new ThreadLocal[Span] { + override def initialValue(): Span = emptySpan + } + + override def activeSpan(): Span = + storage.get() + + override def activate(span: Span): Scope = + activate(span, finishOnClose = false) + + override def activate(span: Span, finishOnClose: Boolean): Scope = { + val previouslyActiveSpan = storage.get() + storage.set(span) + + new Scope { + override def close(): Unit = { + storage.set(previouslyActiveSpan) + if (finishOnClose && span != null) + span.finish() + } + } + } + } + + object ThreadLocalBased { + def apply(): ThreadLocalBased = new ThreadLocalBased() + } +}
\ No newline at end of file |