diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2019-03-18 13:44:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-18 13:44:50 +0100 |
commit | 8efb3b408a876a3dfdac79580773279125cb4135 (patch) | |
tree | 96fd746fc13f4ffb914d8e59c4ea074513877556 /kamon-status-page/src/main/scala/kamon/status/page/StatusPage.scala | |
parent | e311df4b3a272f4f160857f718a96ec316a2fc06 (diff) | |
parent | ec83a72879378bc9eedea24f828e4d30fed95e92 (diff) | |
download | Kamon-8efb3b408a876a3dfdac79580773279125cb4135.tar.gz Kamon-8efb3b408a876a3dfdac79580773279125cb4135.tar.bz2 Kamon-8efb3b408a876a3dfdac79580773279125cb4135.zip |
Merge pull request #569 from ivantopo/status-page
Status page
Diffstat (limited to 'kamon-status-page/src/main/scala/kamon/status/page/StatusPage.scala')
-rw-r--r-- | kamon-status-page/src/main/scala/kamon/status/page/StatusPage.scala | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/kamon-status-page/src/main/scala/kamon/status/page/StatusPage.scala b/kamon-status-page/src/main/scala/kamon/status/page/StatusPage.scala new file mode 100644 index 00000000..3a195e28 --- /dev/null +++ b/kamon-status-page/src/main/scala/kamon/status/page/StatusPage.scala @@ -0,0 +1,69 @@ +package kamon +package status +package page + +import com.typesafe.config.Config +import kamon.module.Module +import org.slf4j.LoggerFactory + +import scala.util.{Failure, Success, Try} + +/** + * Uses an embedded web server to publish a simple status page with information about Kamon's internal status. + */ +class StatusPage extends Module { + private val _logger = LoggerFactory.getLogger(classOf[StatusPage]) + @volatile private var _statusPageServer: Option[StatusPageServer] = None + + override def start(): Unit = + init(Kamon.config()) + + override def stop(): Unit = + stopServer() + + override def reconfigure(newConfig: Config): Unit = + init(newConfig) + + + private def init(config: Config): Unit = synchronized { + val listenConfig = config.getConfig("kamon.status-page.listen") + val hostname = listenConfig.getString("hostname") + val port = listenConfig.getInt("port") + + _statusPageServer.fold { + // Starting a new server on the configured hostname/port + startServer(hostname, port, Kamon.classLoader()) + + }(existentServer => { + // If the configuration has changed we will stop the previous version + // and start a new one with the new hostname/port. + + if(existentServer.getHostname != hostname || existentServer.getListeningPort != port) { + stopServer() + startServer(hostname, port, Kamon.classLoader()) + } + }) + } + + private def startServer(hostname: String, port: Int, resourceLoader: ClassLoader): Unit = { + Try { + val server = new StatusPageServer(hostname, port, resourceLoader, Kamon.status()) + server.start() + server + + } match { + case Success(server) => + _logger.info(s"Status page started on http://$hostname:$port/") + _statusPageServer = Some(server) + + case Failure(t) => + _logger.error("Failed to start the status page embedded server", t) + } + } + + private def stopServer(): Unit = { + _statusPageServer.foreach(_.stop()) + _statusPageServer = None + } + +} |