aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/Kamon.scala
diff options
context:
space:
mode:
Diffstat (limited to 'kamon-core/src/main/scala/kamon/Kamon.scala')
-rw-r--r--kamon-core/src/main/scala/kamon/Kamon.scala84
1 files changed, 20 insertions, 64 deletions
diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala
index ecbc796e..b1490e32 100644
--- a/kamon-core/src/main/scala/kamon/Kamon.scala
+++ b/kamon-core/src/main/scala/kamon/Kamon.scala
@@ -16,24 +16,23 @@
package kamon
import com.typesafe.config.{Config, ConfigFactory}
-import io.opentracing.propagation.Format
-import io.opentracing.{ActiveSpan, Span, SpanContext}
import kamon.metric._
-import kamon.trace.Tracer
+import kamon.trace._
import kamon.util.{Filters, MeasurementUnit, Registration}
import scala.concurrent.Future
import java.time.Duration
import java.util.concurrent.{Executors, ScheduledExecutorService, ScheduledThreadPoolExecutor}
-import io.opentracing.ActiveSpan.Continuation
+import kamon.context.{Context, Storage}
import org.slf4j.LoggerFactory
import scala.util.Try
-object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Tracer {
+object Kamon extends MetricLookup with ReporterRegistry with Tracer {
private val logger = LoggerFactory.getLogger("kamon.Kamon")
+
@volatile private var _config = ConfigFactory.load()
@volatile private var _environment = Environment.fromConfig(_config)
@volatile private var _filters = Filters.fromConfig(_config)
@@ -41,7 +40,8 @@ object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Trac
private val _scheduler = Executors.newScheduledThreadPool(schedulerPoolSize(_config), numberedThreadFactory("kamon-scheduler"))
private val _metrics = new MetricRegistry(_config, _scheduler)
private val _reporters = new ReporterRegistryImpl(_metrics, _config)
- private val _tracer = new Tracer(Kamon, _reporters, _config)
+ private val _tracer = Tracer.Default(Kamon, _reporters, _config)
+ private val _contextStorage = Storage.ThreadLocal()
private var _onReconfigureHooks = Seq.empty[OnReconfigureHook]
def environment: Environment =
@@ -56,6 +56,7 @@ object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Trac
_filters = Filters.fromConfig(config)
_metrics.reconfigure(config)
_reporters.reconfigure(config)
+ _tracer.reconfigure(config)
_onReconfigureHooks.foreach(hook => {
Try(hook.onReconfigure(config)).failed.foreach(error =>
@@ -90,73 +91,28 @@ object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Trac
def tracer: Tracer =
_tracer
- override def buildSpan(operationName: String): io.opentracing.Tracer.SpanBuilder =
+ override def buildSpan(operationName: String): Tracer.SpanBuilder =
_tracer.buildSpan(operationName)
- override def extract[C](format: Format[C], carrier: C): SpanContext =
- _tracer.extract(format, carrier)
-
- override def inject[C](spanContext: SpanContext, format: Format[C], carrier: C): Unit =
- _tracer.inject(spanContext, format, carrier)
-
- override def activeSpan(): ActiveSpan =
- _tracer.activeSpan()
- override def makeActive(span: Span): ActiveSpan =
- _tracer.makeActive(span)
+ override def identityProvider: IdentityProvider =
+ _tracer.identityProvider
+ def currentContext(): Context =
+ _contextStorage.current()
- /**
- * Makes the provided Span active before code is evaluated and deactivates it afterwards.
- */
- def withSpan[T](span: Span)(code: => T): T = {
- val activeSpan = makeActive(span)
- val evaluatedCode = code
- activeSpan.deactivate()
- evaluatedCode
- }
+ def storeContext(context: Context): Storage.Scope =
+ _contextStorage.store(context)
- /**
- * Actives the provided Continuation before code is evaluated and deactivates it afterwards.
- */
- def withContinuation[T](continuation: Continuation)(code: => T): T = {
- if(continuation == null)
- code
- else {
- val activeSpan = continuation.activate()
- val evaluatedCode = code
- activeSpan.deactivate()
- evaluatedCode
+ def withContext[T](context: Context)(f: => T): T = {
+ val scope = _contextStorage.store(context)
+ try {
+ f
+ } finally {
+ scope.close()
}
}
- /**
- * 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 =
_reporters.loadReportersFromConfig()