Skip to content

Commit

Permalink
fallback to reference configuration when the application config fails…
Browse files Browse the repository at this point in the history
… to load, related to #564
  • Loading branch information
ivantopo committed Apr 11, 2019
1 parent 19d2846 commit 53315a0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kamon

import java.io._
import java.util.concurrent.TimeUnit

import org.scalatest.{Matchers, WordSpec}
import org.scalatest.concurrent.Eventually
import org.scalatest.time.SpanSugar._

import scala.util.{Failure, Success, Try}

class InitialConfigLoadingSpec extends WordSpec with Matchers with Eventually {

"the initial config loading" should {
"fallback to using reference configuration only when application.conf files are malformed" in {
val process = Runtime.getRuntime.exec(createProcessWithConfig("kamon.KamonWithCustomConfig", "{This is a bad config}"))
val processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()))

eventually(timeout(10 seconds)) {
val outputLine = processOutputReader.readLine()
outputLine shouldBe "All Good"
}

if(process.isAlive) {
process.destroyForcibly().waitFor(5, TimeUnit.SECONDS)
}
}
}

def createProcessWithConfig(mainClass: String, configContent: String): String = {
val tempConfigFile = File.createTempFile("bad-config", ".conf")
val writer = new BufferedWriter(new FileWriter(tempConfigFile))
writer.write(configContent)
writer.flush()
writer.close()

val configOptions = "-Dconfig.trace=loads -Dconfig.file=" + tempConfigFile.getAbsolutePath()
System.getProperty("java.home") + File.separator + "bin" + File.separator + "java " + configOptions +
" -cp " + System.getProperty("java.class.path") + " " + mainClass
}
}

object KamonWithCustomConfig extends App {
Try {
Kamon.counter("test").increment()
} match {
case Success(_) => println("All Good")
case Failure(_) => println("All Bad")
}

}
23 changes: 22 additions & 1 deletion kamon-core/src/main/scala/kamon/Kamon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import scala.util.Try
object Kamon extends MetricLookup with ReporterRegistry with Tracer {
private val logger = LoggerFactory.getLogger("kamon.Kamon")

@volatile private var _config = ConfigFactory.load()
@volatile private var _config = loadInitialConfiguration()
@volatile private var _environment = Environment.fromConfig(_config)
@volatile private var _filters = Filters.fromConfig(_config)

Expand Down Expand Up @@ -187,6 +187,27 @@ object Kamon extends MetricLookup with ReporterRegistry with Tracer {
private def schedulerPoolSize(config: Config): Int =
config.getInt("kamon.scheduler-pool-size")

private def loadInitialConfiguration(): Config = {
Try {
ConfigFactory.load(ConfigFactory.load())

} recoverWith {
case t: Throwable =>
logger.warn("Failed to load the default configuration, attempting to load the reference configuration", t)

Try {
val referenceConfig = ConfigFactory.defaultReference()
logger.warn("Initializing with the default reference configuration, none of the user settings might be in effect", t)
referenceConfig
}

} recover {
case t: Throwable =>
logger.error("Failed to load the reference configuration, please check your reference.conf files for errors", t)
ConfigFactory.empty()
} get
}

}

trait OnReconfigureHook {
Expand Down

0 comments on commit 53315a0

Please sign in to comment.