blob: 47d0b56733b6cf2f64f715b221f9f1b602f6dd3a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package kamon
import kamon.context.{Context, Storage}
import kamon.trace.Span
import scala.util.control.NonFatal
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 NonFatal(t) =>
span.addError(t.getMessage, t)
throw t
} finally {
if(finishSpan)
span.finish()
}
}
}
|