Skip to content

Commit 53315a0

Browse files
committed
fallback to reference configuration when the application config fails to load, related to #564
1 parent 19d2846 commit 53315a0

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package kamon
2+
3+
import java.io._
4+
import java.util.concurrent.TimeUnit
5+
6+
import org.scalatest.{Matchers, WordSpec}
7+
import org.scalatest.concurrent.Eventually
8+
import org.scalatest.time.SpanSugar._
9+
10+
import scala.util.{Failure, Success, Try}
11+
12+
class InitialConfigLoadingSpec extends WordSpec with Matchers with Eventually {
13+
14+
"the initial config loading" should {
15+
"fallback to using reference configuration only when application.conf files are malformed" in {
16+
val process = Runtime.getRuntime.exec(createProcessWithConfig("kamon.KamonWithCustomConfig", "{This is a bad config}"))
17+
val processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()))
18+
19+
eventually(timeout(10 seconds)) {
20+
val outputLine = processOutputReader.readLine()
21+
outputLine shouldBe "All Good"
22+
}
23+
24+
if(process.isAlive) {
25+
process.destroyForcibly().waitFor(5, TimeUnit.SECONDS)
26+
}
27+
}
28+
}
29+
30+
def createProcessWithConfig(mainClass: String, configContent: String): String = {
31+
val tempConfigFile = File.createTempFile("bad-config", ".conf")
32+
val writer = new BufferedWriter(new FileWriter(tempConfigFile))
33+
writer.write(configContent)
34+
writer.flush()
35+
writer.close()
36+
37+
val configOptions = "-Dconfig.trace=loads -Dconfig.file=" + tempConfigFile.getAbsolutePath()
38+
System.getProperty("java.home") + File.separator + "bin" + File.separator + "java " + configOptions +
39+
" -cp " + System.getProperty("java.class.path") + " " + mainClass
40+
}
41+
}
42+
43+
object KamonWithCustomConfig extends App {
44+
Try {
45+
Kamon.counter("test").increment()
46+
} match {
47+
case Success(_) => println("All Good")
48+
case Failure(_) => println("All Bad")
49+
}
50+
51+
}

kamon-core/src/main/scala/kamon/Kamon.scala

+22-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import scala.util.Try
3232
object Kamon extends MetricLookup with ReporterRegistry with Tracer {
3333
private val logger = LoggerFactory.getLogger("kamon.Kamon")
3434

35-
@volatile private var _config = ConfigFactory.load()
35+
@volatile private var _config = loadInitialConfiguration()
3636
@volatile private var _environment = Environment.fromConfig(_config)
3737
@volatile private var _filters = Filters.fromConfig(_config)
3838

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

190+
private def loadInitialConfiguration(): Config = {
191+
Try {
192+
ConfigFactory.load(ConfigFactory.load())
193+
194+
} recoverWith {
195+
case t: Throwable =>
196+
logger.warn("Failed to load the default configuration, attempting to load the reference configuration", t)
197+
198+
Try {
199+
val referenceConfig = ConfigFactory.defaultReference()
200+
logger.warn("Initializing with the default reference configuration, none of the user settings might be in effect", t)
201+
referenceConfig
202+
}
203+
204+
} recover {
205+
case t: Throwable =>
206+
logger.error("Failed to load the reference configuration, please check your reference.conf files for errors", t)
207+
ConfigFactory.empty()
208+
} get
209+
}
210+
190211
}
191212

192213
trait OnReconfigureHook {

0 commit comments

Comments
 (0)