Skip to content

Commit

Permalink
add kamon.init.attach-instrumentation setting (#1278)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantopo authored May 19, 2023
1 parent 4f18bba commit c3f0655
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions core/kamon-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ kamon {

# Hides the Kamon banner shown during Kamon's init process
hide-banner = no

# Controls whether Kamon should try to use the runtime attacher during initialization. It is safe to disable
# the runtime attacher if you don't need instrumentation at all or if you are applying instrumentation via the
# -javaagent JVM option.
attach-instrumentation = yes
}

environment {
Expand Down
8 changes: 7 additions & 1 deletion core/kamon-core/src/main/scala/kamon/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package kamon

import com.typesafe.config.{Config, ConfigFactory}
import kamon.Configuration.EnabledConfigurationName
import kamon.Configuration.{InitAttachInstrumentationConfigurationName, EnabledConfigurationName}
import org.slf4j.LoggerFactory

import scala.util.control.NonFatal
Expand All @@ -30,6 +30,7 @@ trait Configuration {
private var _currentConfig: Config = loadInitialConfiguration()
private var _onReconfigureHooks = Seq.empty[Configuration.OnReconfigureHook]
@volatile private var _enabled: Boolean = _currentConfig.getBoolean(EnabledConfigurationName)
@volatile private var _shouldAttachInstrumentation: Boolean = _currentConfig.getBoolean(InitAttachInstrumentationConfigurationName)


/**
Expand All @@ -41,12 +42,14 @@ trait Configuration {
def enabled(): Boolean =
_enabled


/**
* Supply a new Config instance to rule Kamon's world.
*/
def reconfigure(newConfig: Config): Unit = synchronized {
_currentConfig = newConfig
_enabled = newConfig.getBoolean(EnabledConfigurationName)
_shouldAttachInstrumentation = newConfig.getBoolean(InitAttachInstrumentationConfigurationName)
_onReconfigureHooks.foreach(hook => {
try {
hook.onReconfigure(newConfig)
Expand Down Expand Up @@ -74,6 +77,8 @@ trait Configuration {
})
}

protected def shouldAttachInstrumentation(): Boolean =
_shouldAttachInstrumentation

private def loadInitialConfiguration(): Config = {
import System.{err, lineSeparator}
Expand Down Expand Up @@ -104,6 +109,7 @@ trait Configuration {
object Configuration {

private val EnabledConfigurationName = "kamon.enabled"
private val InitAttachInstrumentationConfigurationName = "kamon.init.attach-instrumentation"

trait OnReconfigureHook {
def onReconfigure(newConfig: Config): Unit
Expand Down
10 changes: 8 additions & 2 deletions core/kamon-core/src/main/scala/kamon/Init.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ trait Init { self: ModuleManagement with Configuration with CurrentStatus with M
*/
def init(): Unit = {
if(enabled()) {
self.attachInstrumentation()
if(shouldAttachInstrumentation()) {
self.attachInstrumentation()
}

self.initScheduler()
self.loadModules()
self.moduleRegistry().init()
Expand All @@ -58,7 +61,10 @@ trait Init { self: ModuleManagement with Configuration with CurrentStatus with M
self.reconfigure(config)

if(enabled()) {
self.attachInstrumentation()
if (shouldAttachInstrumentation()) {
self.attachInstrumentation()
}

self.initScheduler()
self.loadModules()
self.moduleRegistry().init()
Expand Down

0 comments on commit c3f0655

Please sign in to comment.