diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2015-08-28 13:02:09 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2015-08-28 13:02:09 +0200 |
commit | ec91eede73e20fef3b694fa45009ddd0ef0b345b (patch) | |
tree | b3aa59dfaa1da122072864b1b343a1ea9835a57b /kamon-core/src/main/scala/kamon | |
parent | df3711e177b58ce752592736f0a327c920adb3f0 (diff) | |
download | Kamon-ec91eede73e20fef3b694fa45009ddd0ef0b345b.tar.gz Kamon-ec91eede73e20fef3b694fa45009ddd0ef0b345b.tar.bz2 Kamon-ec91eede73e20fef3b694fa45009ddd0ef0b345b.zip |
wip, core/akka/spray are kind of migrated.
Diffstat (limited to 'kamon-core/src/main/scala/kamon')
-rw-r--r-- | kamon-core/src/main/scala/kamon/Kamon.scala | 96 | ||||
-rw-r--r-- | kamon-core/src/main/scala/kamon/ModuleLoader.scala | 32 |
2 files changed, 55 insertions, 73 deletions
diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala index 1c560d9f..f2d1e319 100644 --- a/kamon-core/src/main/scala/kamon/Kamon.scala +++ b/kamon-core/src/main/scala/kamon/Kamon.scala @@ -20,84 +20,62 @@ import _root_.akka.event.Logging import com.typesafe.config.{ ConfigFactory, Config } import kamon.metric._ import kamon.trace.{ TracerModuleImpl, TracerModule } +import org.slf4j.LoggerFactory -object Kamon { - trait Extension extends actor.Extension - - private case class KamonCoreComponents(metrics: MetricsModule, tracer: TracerModule) - - private val shouldAutoStart = isSystemPropertyEnabled("kamon.auto-start") - private val shouldWarnOnDuplicateStart = !isSystemPropertyEnabled("kamon.disable-duplicate-start-warning") +import _root_.scala.util.Try - @volatile private var _system: ActorSystem = _ - @volatile private var _coreComponents: Option[KamonCoreComponents] = None - - def start(config: Config): Unit = synchronized { - - def resolveInternalConfig: Config = { - val internalConfig = config.getConfig("kamon.internal-config") +object Kamon { + private val log = LoggerFactory.getLogger(getClass) - config - .withoutPath("akka") - .withoutPath("spray") - .withFallback(internalConfig) - } + trait Extension extends actor.Extension - if (_coreComponents.isEmpty) { - val metrics = MetricsModuleImpl(config) - val tracer = TracerModuleImpl(metrics, config) + val config = resolveConfiguration + val metrics = MetricsModuleImpl(config) + val tracer = TracerModuleImpl(metrics, config) - _coreComponents = Some(KamonCoreComponents(metrics, tracer)) - _system = ActorSystem("kamon", resolveInternalConfig) + private lazy val _system = { + val internalConfig = config.getConfig("kamon.internal-config") + val patchedConfig = config + .withoutPath("akka") + .withoutPath("spray") + .withFallback(internalConfig) - metrics.start(_system) - tracer.start(_system) - _system.registerExtension(ModuleLoader) + log.info("Initializing KAMON DUUUDEEE") - } else if (shouldWarnOnDuplicateStart) { - val logger = Logging(_system, "Kamon") - logger.warning("An attempt to start Kamon has been made, but Kamon has already been started.") - } + ActorSystem("kamon", patchedConfig) } - private def isSystemPropertyEnabled(propertyName: String): Boolean = - sys.props.get(propertyName).map(_.equals("true")).getOrElse(false) + private lazy val _start = { + metrics.start(_system) + tracer.start(_system) + _system.registerExtension(ModuleLoader) + } - def start(): Unit = - start(ConfigFactory.load) + def start(): Unit = _start def shutdown(): Unit = { - _coreComponents = None - _system.shutdown() - _system = null + // TODO: Define what a proper shutdown should be like. } - def metrics: MetricsModule = - ifStarted(_.metrics) - - def tracer: TracerModule = - ifStarted(_.tracer) - - def apply[T <: Kamon.Extension](key: ExtensionId[T]): T = - ifStarted { _ ⇒ - if (_system ne null) - key(_system) - else - sys.error("Cannot retrieve extensions while Kamon is being initialized.") - } + /* def apply[T <: Kamon.Extension](key: ExtensionId[T]): T = + key(_system) def extension[T <: Kamon.Extension](key: ExtensionId[T]): T = - apply(key) + apply(key)*/ - private def ifStarted[T](thunk: KamonCoreComponents ⇒ T): T = - _coreComponents.map(thunk(_)) getOrElse { - if (shouldAutoStart) { - start() - thunk(_coreComponents.get) + private def resolveConfiguration: Config = { + val defaultConfig = ConfigFactory.load() - } else sys.error("Kamon has not been started yet. You must either explicitlt call Kamon.start(...) or enable " + - "automatic startup by adding -Dkamon.auto-start=true to your JVM options.") + defaultConfig.getString("kamon.config-provider") match { + case "default" ⇒ defaultConfig + case fqcn ⇒ + val dynamic = new ReflectiveDynamicAccess(getClass.getClassLoader) + dynamic.createInstanceFor[ConfigProvider](fqcn, Nil).get.config } + } +} +trait ConfigProvider { + def config: Config } diff --git a/kamon-core/src/main/scala/kamon/ModuleLoader.scala b/kamon-core/src/main/scala/kamon/ModuleLoader.scala index a26fd339..f240f6ce 100644 --- a/kamon-core/src/main/scala/kamon/ModuleLoader.scala +++ b/kamon-core/src/main/scala/kamon/ModuleLoader.scala @@ -21,6 +21,7 @@ import _root_.akka.actor._ import _root_.akka.event.Logging import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation.{ Around, Pointcut, Aspect } +import org.slf4j.LoggerFactory private[kamon] object ModuleLoader extends ExtensionId[ModuleLoaderExtension] with ExtensionIdProvider { def lookup(): ExtensionId[_ <: actor.Extension] = ModuleLoader @@ -28,23 +29,22 @@ private[kamon] object ModuleLoader extends ExtensionId[ModuleLoaderExtension] wi } private[kamon] class ModuleLoaderExtension(system: ExtendedActorSystem) extends Kamon.Extension { - val log = Logging(system, "ModuleLoader") + val log = LoggerFactory.getLogger(getClass) val settings = ModuleLoaderSettings(system) if (settings.modulesRequiringAspectJ.nonEmpty && !isAspectJPresent && settings.showAspectJMissingWarning) logAspectJWeaverMissing(settings.modulesRequiringAspectJ) // Force initialization of all modules marked with auto-start. - settings.availableModules.filter(_.autoStart).foreach { module ⇒ - if (module.extensionClass == "none") - log.debug("Ignoring auto start of the [{}] module with no extension class.", module.name) - else - system.dynamicAccess.getObjectFor[ExtensionId[Kamon.Extension]](module.extensionClass).map { moduleID ⇒ - log.debug("Auto starting the [{}] module.", module.name) + settings.availableModules.filter(_.startInfo.nonEmpty).foreach { + case AvailableModuleInfo(name, requiresAJ, Some(ModuleStartInfo(autoStart, extensionClass))) if autoStart ⇒ + + system.dynamicAccess.getObjectFor[ExtensionId[Kamon.Extension]](extensionClass).map { moduleID ⇒ + log.debug(s"Auto starting the [$name] module.") moduleID.get(system) } recover { - case th: Throwable ⇒ log.error(th, "Failed to auto start the [{}] module.", module.name) + case th: Throwable ⇒ log.error(s"Failed to auto start the [$name] module.", th) } } @@ -82,7 +82,8 @@ private[kamon] class ModuleLoaderExtension(system: ExtendedActorSystem) extends } } -private[kamon] case class AvailableModuleInfo(name: String, extensionClass: String, requiresAspectJ: Boolean, autoStart: Boolean) +private[kamon] case class AvailableModuleInfo(name: String, requiresAspectJ: Boolean, startInfo: Option[ModuleStartInfo]) +private[kamon] case class ModuleStartInfo(autoStart: Boolean, extensionClass: String) private[kamon] case class ModuleLoaderSettings(showAspectJMissingWarning: Boolean, availableModules: List[AvailableModuleInfo]) { val modulesRequiringAspectJ = availableModules.filter(_.requiresAspectJ) } @@ -98,12 +99,15 @@ private[kamon] object ModuleLoaderSettings { val modules = config.firstLevelKeys val availableModules = modules.map { moduleName ⇒ val moduleConfig = config.getConfig(moduleName) + val requiresAspectJ = moduleConfig.getBoolean("requires-aspectj") + + val startInfo = + if (moduleConfig.hasPath("auto-start") && moduleConfig.hasPath("extension-class")) + Some(ModuleStartInfo(moduleConfig.getBoolean("auto-start"), moduleConfig.getString("extension-class"))) + else + None - AvailableModuleInfo( - moduleName, - moduleConfig.getString("extension-id"), - moduleConfig.getBoolean("requires-aspectj"), - moduleConfig.getBoolean("auto-start")) + AvailableModuleInfo(moduleName, requiresAspectJ, startInfo) } toList |