aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2017-06-14 11:49:17 +0200
committerIvan Topolnjak <ivantopo@gmail.com>2017-06-14 11:49:17 +0200
commit9cb3d749e9e46f68e76da26fd394a7ac098461cf (patch)
treea0689d220ffc3ced8090aa9393eac4b5fc6cf307
parent92f6674e46fef301526bd7ab9cc6e59f0b7804bf (diff)
downloadKamon-9cb3d749e9e46f68e76da26fd394a7ac098461cf.tar.gz
Kamon-9cb3d749e9e46f68e76da26fd394a7ac098461cf.tar.bz2
Kamon-9cb3d749e9e46f68e76da26fd394a7ac098461cf.zip
add convenience function that handle null active spans and continuations
-rw-r--r--kamon-core/src/main/scala/kamon/Kamon.scala39
1 files changed, 35 insertions, 4 deletions
diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala
index dff143c4..0502d28b 100644
--- a/kamon-core/src/main/scala/kamon/Kamon.scala
+++ b/kamon-core/src/main/scala/kamon/Kamon.scala
@@ -95,12 +95,43 @@ object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Trac
* Actives the provided Continuation before code is evaluated and deactivates it afterwards.
*/
def withContinuation[T](continuation: Continuation)(code: => T): T = {
- val activeSpan = continuation.activate()
- val evaluatedCode = code
- activeSpan.deactivate()
- evaluatedCode
+ if(continuation == null)
+ code
+ else {
+ val activeSpan = continuation.activate()
+ val evaluatedCode = code
+ activeSpan.deactivate()
+ evaluatedCode
+ }
+ }
+
+ /**
+ * Captures a continuation from the currently active Span (if any).
+ */
+ def activeSpanContinuation(): Continuation = {
+ val activeSpan = Kamon.activeSpan()
+ if(activeSpan == null)
+ null
+ else
+ activeSpan.capture()
}
+ /**
+ * Runs the provided closure with the currently active Span (if any).
+ */
+ def onActiveSpan[T](code: ActiveSpan => T): Unit = {
+ val activeSpan = Kamon.activeSpan()
+ if(activeSpan != null)
+ code(activeSpan)
+ }
+
+ /**
+ * Evaluates the provided closure with the currently active Span (if any) and returns the evaluation result. If there
+ * was no active Span then the provided fallback value
+ */
+ def fromActiveSpan[T](code: ActiveSpan => T): Option[T] =
+ Option(activeSpan()).map(code)
+
override def loadReportersFromConfig(): Unit =
reporterRegistry.loadReportersFromConfig()