From f1c6ceffa22c59a463d6d8cd2ca77e2b440eb450 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Mon, 29 Oct 2018 17:45:57 +0100 Subject: Implement a module registry that supports loading from configuration (#559) --- kamon-core/src/main/scala/kamon/Kamon.scala | 109 +++------------------------- 1 file changed, 9 insertions(+), 100 deletions(-) (limited to 'kamon-core/src/main/scala/kamon/Kamon.scala') diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala index a6cc7bf4..ab95d773 100644 --- a/kamon-core/src/main/scala/kamon/Kamon.scala +++ b/kamon-core/src/main/scala/kamon/Kamon.scala @@ -15,113 +15,22 @@ package kamon -import java.time.Duration -import java.util.concurrent.{Executors, ScheduledExecutorService, ScheduledThreadPoolExecutor} +object Kamon extends ClassLoading + with Configuration + with Utilities + with Metrics + with Tracing + with ModuleLoading + with ContextPropagation + with ContextStorage { -import com.typesafe.config.{Config, ConfigFactory} -import kamon.metric._ -import kamon.trace._ -import kamon.util.{Clock, Filters, Matcher, Registration} -import org.slf4j.LoggerFactory -import scala.concurrent.Future -import scala.util.Try - - -object Kamon extends MetricLookup with ClassLoading with Configuration with ReporterRegistry with Tracer with ContextPropagation with ContextStorage { - 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) - - private val _clock = new Clock.Default() - private val _scheduler = Executors.newScheduledThreadPool(schedulerPoolSize(_config), numberedThreadFactory("kamon-scheduler", daemon = true)) - private val _metrics = new MetricRegistry(_config, _scheduler) - private val _reporterRegistry = new ReporterRegistry.Default(_metrics, _config, _clock) - private val _tracer = Tracer.Default(Kamon, _reporterRegistry, _config, _clock) - //private var _onReconfigureHooks = Seq.empty[OnReconfigureHook] - - sys.addShutdownHook(() => _scheduler.shutdown()) + @volatile private var _environment = Environment.fromConfig(config()) def environment: Environment = _environment onReconfigure(newConfig => { - _config = config _environment = Environment.fromConfig(config) - _filters = Filters.fromConfig(config) - _metrics.reconfigure(config) - _reporterRegistry.reconfigure(config) - _tracer.reconfigure(config) - - _scheduler match { - case stpe: ScheduledThreadPoolExecutor => stpe.setCorePoolSize(schedulerPoolSize(config)) - case other => logger.error("Unexpected scheduler [{}] found when reconfiguring Kamon.", other) - } }) - - override def histogram(name: String, unit: MeasurementUnit, dynamicRange: Option[DynamicRange]): HistogramMetric = - _metrics.histogram(name, unit, dynamicRange) - - override def counter(name: String, unit: MeasurementUnit): CounterMetric = - _metrics.counter(name, unit) - - override def gauge(name: String, unit: MeasurementUnit): GaugeMetric = - _metrics.gauge(name, unit) - - override def rangeSampler(name: String, unit: MeasurementUnit, sampleInterval: Option[Duration], - dynamicRange: Option[DynamicRange]): RangeSamplerMetric = - _metrics.rangeSampler(name, unit, dynamicRange, sampleInterval) - - override def timer(name: String, dynamicRange: Option[DynamicRange]): TimerMetric = - _metrics.timer(name, dynamicRange) - - - def tracer: Tracer = - _tracer - - override def buildSpan(operationName: String): Tracer.SpanBuilder = - _tracer.buildSpan(operationName) - - - override def identityProvider: IdentityProvider = - _tracer.identityProvider - - override def loadReportersFromConfig(): Unit = - _reporterRegistry.loadReportersFromConfig() - - override def addReporter(reporter: MetricReporter): Registration = - _reporterRegistry.addReporter(reporter) - - override def addReporter(reporter: MetricReporter, name: String): Registration = - _reporterRegistry.addReporter(reporter, name) - - override def addReporter(reporter: MetricReporter, name: String, filter: String): Registration = - _reporterRegistry.addReporter(reporter, name, filter) - - override def addReporter(reporter: SpanReporter): Registration = - _reporterRegistry.addReporter(reporter) - - override def addReporter(reporter: SpanReporter, name: String): Registration = - _reporterRegistry.addReporter(reporter, name) - - override def stopAllReporters(): Future[Unit] = - _reporterRegistry.stopAllReporters() - - def filter(filterName: String, pattern: String): Boolean = - _filters.accept(filterName, pattern) - - def filter(filterName: String): Matcher = - _filters.get(filterName) - - def clock(): Clock = - _clock - - def scheduler(): ScheduledExecutorService = - _scheduler - - private def schedulerPoolSize(config: Config): Int = - config.getInt("kamon.scheduler-pool-size") - } -- cgit v1.2.3