Skip to content

Commit

Permalink
FIxes #64 - wires Rest, Http, Metric modules through MacWire. Cake Pa…
Browse files Browse the repository at this point in the history
…ttern version with traits.
  • Loading branch information
Alexander Weber committed Apr 25, 2016
1 parent 5798ab6 commit dd02108
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mesosphere.chaos.examples

import mesosphere.chaos.http.HttpConf
import mesosphere.chaos.{ AppConfiguration, ChaosModule, App }
import mesosphere.chaos.{ AppConfiguration, App }
import mesosphere.chaos.examples.persons.PersonsModule
import org.rogach.scallop.ScallopConf

Expand All @@ -10,9 +10,7 @@ object Main extends App {
conf.afterInit()

// Creating chaos module, which contains ScallopConf, MetricsModule and HttpModule.
val chaosModule = new ChaosModule(conf)
// Derives from RestModule adding some example resources to it.
val exampleModule = new PersonsModule(chaosModule)
val chaosModule = new PersonsModule { def httpConf = conf }

run(chaosModule.httpModule.httpService)
run(chaosModule.httpService)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package mesosphere.chaos.examples.persons

import mesosphere.chaos.ChaosModule
import mesosphere.chaos.examples.persons.impl.PersonsResource
import mesosphere.chaos.rest.RestModule
import com.softwaremill.macwire._

class PersonsModule(chaosModule: ChaosModule) extends RestModule(chaosModule) {
@Module
trait PersonsModule extends RestModule {
lazy val persons = Seq("Alice", "Bob", "Christoph", "Dimitri")
lazy val personsResource = wire[PersonsResource]
lazy val personsResource = new PersonsResource(persons)

httpService.registerResources(personsResource)
}
}
11 changes: 3 additions & 8 deletions src/main/scala/mesosphere/chaos/ChaosModule.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package mesosphere.chaos

import mesosphere.chaos.http.{ HttpConf, HttpModule }
import com.softwaremill.macwire.Module
import mesosphere.chaos.http.HttpModule
import mesosphere.chaos.metrics.MetricsModule
import com.softwaremill.macwire._

/**
* Wraps up the metrics and http modules.
* @param conf configuration used to set up the http server.
*/
@Module
class ChaosModule(conf: HttpConf) {
lazy val httpModule = wire[HttpModule]
lazy val metricsModule = wire[MetricsModule]
// Needed for http module.
lazy val metricsRegistry = metricsModule.registry
trait ChaosModule extends MetricsModule with HttpModule {
}
10 changes: 7 additions & 3 deletions src/main/scala/mesosphere/chaos/http/HttpModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package mesosphere.chaos.http
import com.codahale.metrics.MetricRegistry
import mesosphere.chaos.http.impl.{ HttpServer, HttpServiceImpl }
import com.softwaremill.macwire._
import mesosphere.chaos.metrics.MetricsModule

/**
* Provides a http service, which in it's turn contains a Jetty http server.
* @param httpConf configuration needed in order to set up the Jetty server.
*/
@Module
class HttpModule(httpConf: HttpConf, metricsRegistry: MetricRegistry) {
lazy val httpServer = wire[HttpServer]
trait HttpModule extends MetricsModule {
// configuration needed in order to set up the Jetty server.
def httpConf: HttpConf
def reg: MetricRegistry = new MetricRegistry

lazy val httpServer = new HttpServer(httpConf, reg)
lazy val httpService = wire[HttpServiceImpl]
}
7 changes: 4 additions & 3 deletions src/main/scala/mesosphere/chaos/metrics/MetricsModule.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mesosphere.chaos.metrics

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.jvm._
import mesosphere.chaos.metrics.impl.{ DefaultBufferPoolMetricSet, DefaultMetricRegistry }
import org.slf4j.LoggerFactory
Expand All @@ -15,12 +16,12 @@ import com.softwaremill.macwire._
* - a set of gauges for the number of threads in their various states and deadlock detection.
*/
@Module
class MetricsModule {
trait MetricsModule {
private[this] val log = LoggerFactory.getLogger(getClass.getName)

lazy val garbageCollector = wire[GarbageCollectorMetricSet]
lazy val threadState = wire[ThreadStatesGaugeSet]
lazy val memoryUsage = wire[MemoryUsageGaugeSet]
lazy val bufferPool = wire[DefaultBufferPoolMetricSet]
lazy val registry = wire[DefaultMetricRegistry]
}
lazy val registry: MetricRegistry = new DefaultMetricRegistry(garbageCollector, bufferPool, memoryUsage, threadState)
}
8 changes: 3 additions & 5 deletions src/main/scala/mesosphere/chaos/rest/RestModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package mesosphere.chaos.rest
import javax.servlet.http.HttpServlet

import com.codahale.metrics.servlets.MetricsServlet
import mesosphere.chaos.ChaosModule
import mesosphere.chaos.http.HttpModule
import mesosphere.chaos.metrics.MetricsModule
import mesosphere.chaos.rest.impl.{ LogConfigServlet, PingServlet }
import com.softwaremill.macwire._

Expand All @@ -12,23 +13,20 @@ import com.softwaremill.macwire._
* - ping servlet. Replies to a GET by printing "pong".
* - metrics servlet. Lists all metrics included in the metrics registry.
* - logging servlet. Allows dynamic adjustments of http configuration.
* @param chaosModule module including the metrics and http modules.
*/
@Module
class RestModule(chaosModule: ChaosModule) {
trait RestModule extends HttpModule with MetricsModule {
// Override these in a subclass to mount resources at a different path
val pingUrl = "/ping"
val metricsUrl = "/metrics"
val loggingUrl = "/logging"

// Initializing servlets
lazy val pingServlet: HttpServlet = wire[PingServlet]
lazy val registry = chaosModule.metricsModule.registry
lazy val metricsServlet: HttpServlet = new MetricsServlet(registry)
lazy val logConfigServlet: HttpServlet = wire[LogConfigServlet]

// Binding servlets
val httpService = chaosModule.httpModule.httpService
httpService.registerServlet(pingServlet, pingUrl)
httpService.registerServlet(metricsServlet, metricsUrl)
httpService.registerServlet(logConfigServlet, loggingUrl)
Expand Down

0 comments on commit dd02108

Please sign in to comment.