diff options
Diffstat (limited to 'kamon-core/src/main/scala')
-rw-r--r-- | kamon-core/src/main/scala/kamon/Kamon.scala | 87 |
1 files changed, 61 insertions, 26 deletions
diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala index 46e75b4e..24b7c07c 100644 --- a/kamon-core/src/main/scala/kamon/Kamon.scala +++ b/kamon-core/src/main/scala/kamon/Kamon.scala @@ -24,23 +24,61 @@ import kamon.util.logger.LazyLogger import _root_.scala.util.control.NonFatal import _root_.scala.util.{Failure, Success, Try} +trait ConfigProvider { + def config: Config + + final def patchedConfig: Config = { + val internalConfig = config.getConfig("kamon.internal-config") + config + .withoutPath("akka") + .withoutPath("spray") + .withFallback(internalConfig) + } +} + object Kamon { private val log = LazyLogger("Kamon") trait Extension extends actor.Extension - val config = resolveConfiguration - val metrics = MetricsModuleImpl(config) - val tracer = TracerModuleImpl(metrics, config) + def defaultConfig = ConfigFactory.load(this.getClass.getClassLoader, ConfigParseOptions.defaults(), ConfigResolveOptions.defaults().setAllowUnresolved(true)) - private lazy val _system = { - val internalConfig = config.getConfig("kamon.internal-config") + class KamonDefaultConfigProvider extends ConfigProvider { + def config = resolveConfiguration - val patchedConfig = config - .withoutPath("akka") - .withoutPath("spray") - .withFallback(internalConfig) + private def resolveConfiguration: Config = { + val defaultConf = defaultConfig + + defaultConf.getString("kamon.config-provider") match { + case "default" ⇒ defaultConf + case fqcn ⇒ + val dynamic = new ReflectiveDynamicAccess(getClass.getClassLoader) + dynamic.createInstanceFor[ConfigProvider](fqcn, Nil).get.config + } + } + } + + class KamonConfigProvider(_config: Config) extends ConfigProvider { + def config = _config + } + + private[kamon] var configProvider: Option[ConfigProvider] = None + def config: Config = + configProvider match { + case Some(provider) ⇒ provider.config + case None ⇒ throw new Exception("Kamon.start() not called yet") + } + lazy val metrics = MetricsModuleImpl(config) + lazy val tracer = TracerModuleImpl(metrics, config) + + private lazy val _system = { + val patchedConfig = + configProvider match { + case Some(provider) ⇒ provider.patchedConfig + case None ⇒ + throw new Exception("Kamon.start() not called yet") + } log.info("Initializing Kamon...") @@ -55,7 +93,20 @@ object Kamon { _system.registerExtension(ModuleLoader) } - def start(): Unit = _start + def start(): Unit = { + configProvider = Some(new KamonDefaultConfigProvider()) + _start + } + + def start(conf: Config): Unit = { + configProvider = Some(new KamonConfigProvider(conf)) + _start + } + + def start(provider: ConfigProvider): Unit = { + configProvider = Some(provider) + _start + } def shutdown(): Unit = { _system.shutdown() @@ -74,20 +125,4 @@ object Kamon { case Failure(NonFatal(reason)) ⇒ log.debug(s"Kamon-autoweave failed to load. Reason: ${reason.getMessage}.") } } - - private def resolveConfiguration: Config = { - val defaultConfig = ConfigFactory.load(this.getClass.getClassLoader, ConfigParseOptions.defaults(), ConfigResolveOptions.defaults().setAllowUnresolved(true)) - - defaultConfig.getString("kamon.config-provider") match { - case "default" ⇒ defaultConfig.resolve() - case fqcn ⇒ - val dynamic = new ReflectiveDynamicAccess(getClass.getClassLoader) - dynamic.createInstanceFor[ConfigProvider](fqcn, Nil).get.config - } - } } - -trait ConfigProvider { - def config: Config -} - |