aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/Kamon.scala
blob: 24b7c07cae6d1e395be6e9303c99b6c5d671b770 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* =========================================================================================
 * Copyright © 2013-2016 the kamon project <http://kamon.io/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 * =========================================================================================
 */
package kamon

import _root_.akka.actor
import _root_.akka.actor._
import com.typesafe.config.{Config, ConfigFactory, ConfigParseOptions, ConfigResolveOptions}
import kamon.metric._
import kamon.trace.TracerModuleImpl
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

  def defaultConfig = ConfigFactory.load(this.getClass.getClassLoader, ConfigParseOptions.defaults(), ConfigResolveOptions.defaults().setAllowUnresolved(true))

  class KamonDefaultConfigProvider extends ConfigProvider {
    def config = resolveConfiguration

    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...")

    tryLoadAutoweaveModule()

    ActorSystem("kamon", patchedConfig)
  }

  private lazy val _start = {
    metrics.start(_system)
    tracer.start(_system)
    _system.registerExtension(ModuleLoader)
  }

  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()
  }

  private def tryLoadAutoweaveModule(): Unit = {
    Try {
      val autoweave = Class.forName("kamon.autoweave.Autoweave")
      autoweave.getDeclaredMethod("attach").invoke(autoweave.newInstance())
    } match {
      case Success(_) 
        val color = (msg: String)  s"""\u001B[32m${msg}\u001B[0m"""
        log.info(color("Kamon-autoweave has been successfully loaded."))
        log.info(color("The AspectJ loadtime weaving agent is now attached to the JVM (you don't need to use -javaagent)."))
        log.info(color("This offers extra flexibility but obviously any classes loaded before attachment will not be woven."))
      case Failure(NonFatal(reason))  log.debug(s"Kamon-autoweave failed to load. Reason: ${reason.getMessage}.")
    }
  }
}