diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2018-08-30 10:40:53 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2018-08-30 10:40:53 +0200 |
commit | e4abea098ef4d6e71a805812bfa95c14bd9002b5 (patch) | |
tree | f5fcb8222e293f420a9e7c06953805a7428d0f0e /kamon-core/src/main/scala/kamon/ContextStorage.scala | |
parent | 794fbf02664ac8c31072d8b955d897901f1f22e0 (diff) | |
download | Kamon-e4abea098ef4d6e71a805812bfa95c14bd9002b5.tar.gz Kamon-e4abea098ef4d6e71a805812bfa95c14bd9002b5.tar.bz2 Kamon-e4abea098ef4d6e71a805812bfa95c14bd9002b5.zip |
working on context tags and http propagation improvements
Diffstat (limited to 'kamon-core/src/main/scala/kamon/ContextStorage.scala')
-rw-r--r-- | kamon-core/src/main/scala/kamon/ContextStorage.scala | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/ContextStorage.scala b/kamon-core/src/main/scala/kamon/ContextStorage.scala new file mode 100644 index 00000000..ee35264a --- /dev/null +++ b/kamon-core/src/main/scala/kamon/ContextStorage.scala @@ -0,0 +1,47 @@ +package kamon + +import kamon.context.{Context, Storage} +import kamon.trace.Span + +trait ContextStorage { + private val _contextStorage = Storage.ThreadLocal() + + def currentContext(): Context = + _contextStorage.current() + + def currentSpan(): Span = + _contextStorage.current().get(Span.ContextKey) + + def storeContext(context: Context): Storage.Scope = + _contextStorage.store(context) + + def withContext[T](context: Context)(f: => T): T = { + val scope = _contextStorage.store(context) + try { + f + } finally { + scope.close() + } + } + + def withContextKey[T, K](key: Context.Key[K], value: K)(f: => T): T = + withContext(currentContext().withKey(key, value))(f) + + def withSpan[T](span: Span)(f: => T): T = + withSpan(span, true)(f) + + def withSpan[T](span: Span, finishSpan: Boolean)(f: => T): T = { + try { + withContextKey(Span.ContextKey, span)(f) + } catch { + case t: Throwable => + span.addError(t.getMessage, t) + throw t + + } finally { + if(finishSpan) + span.finish() + } + } + +} |