aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/StatusPage.scala
blob: 1ac60c1b8eb3dfd16900ac76434b5be6f431c94c (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
package kamon

import com.typesafe.config.Config
import kamon.status.{Status, StatusPageServer}
import org.slf4j.LoggerFactory

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

trait StatusPage { self: Configuration with ClassLoading with ModuleLoading with Metrics with Configuration =>
  private val _log = LoggerFactory.getLogger(classOf[StatusPage])
  @volatile private var _statusPageServer: Option[StatusPageServer] = None
  private val _status = new Status(self._moduleRegistry, self._metricsRegistry, self)

  // Initial configuration and reconfigures
  init(self.config())
  self.onReconfigure(newConfig => self.init(newConfig))


  /**
    * Allows to access internal Kamon status for troubleshooting and displaying purposes. All information returned
    * by the Status instance is a immutable snapshot of the current state of a given component.
    */
  def status(): Status =
    _status


  private def init(config: Config): Unit = synchronized {
    val isStatusPageEnabled = config.getBoolean("kamon.status.enabled")

    if(isStatusPageEnabled) {
      val hostname = config.getString("kamon.status.listen.hostname")
      val port = config.getInt("kamon.status.listen.port")

      _statusPageServer.fold {
        // Starting a new server on the configured hostname/port
        startServer(hostname, port, self.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, self.classLoader())
        }
      })

    } else {
      _statusPageServer.foreach(_ => stopServer())
    }
  }

  private def startServer(hostname: String, port: Int, resourceLoader: ClassLoader): Unit = {
    Try {

      val server = new StatusPageServer(hostname, port, resourceLoader, _status)
      server.start()
      server

    } match {
      case Success(server) =>
        _log.info(s"Status page started on http://$hostname:$port/")
        _statusPageServer = Some(server)

      case Failure(t) =>
        _log.error("Failed to start the status page embedded server", t)
    }
  }

  private def stopServer(): Unit = {
    _statusPageServer.foreach(_.stop())
    _statusPageServer = None
  }

}