Skip to content

Commit

Permalink
FIxes #64 - wires Rest, Http, Metric modules through MacWire.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Weber committed Apr 21, 2016
1 parent 7e2aaf0 commit fe17b1d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ 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) {
httpService.registerResources(new PersonsResource(Seq("Alice", "Bob", "Christoph", "Dimitri")))
lazy val persons = Seq("Alice", "Bob", "Christoph", "Dimitri")
lazy val personsResource = wire[PersonsResource]

httpService.registerResources(personsResource)
}
8 changes: 6 additions & 2 deletions src/main/scala/mesosphere/chaos/ChaosModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package mesosphere.chaos

import mesosphere.chaos.http.{ HttpConf, 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 metricsModule = new MetricsModule
lazy val httpModule = new HttpModule(conf, metricsModule.registry)
lazy val httpModule = wire[HttpModule]
lazy val metricsModule = wire[MetricsModule]
// Needed for http module in order
lazy val metricsRegistry = metricsModule.registry
}
11 changes: 6 additions & 5 deletions src/main/scala/mesosphere/chaos/http/HttpModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package mesosphere.chaos.http

import com.codahale.metrics.MetricRegistry
import mesosphere.chaos.http.impl.{ HttpServer, HttpServiceImpl }
import com.softwaremill.macwire._

/**
* Provides a http service, which in it's turn contains a Jetty http server.
* @param conf configuration needed in order to set up the Jetty server.
* @param registry a metric registry.
* @param httpConf configuration needed in order to set up the Jetty server.
*/
class HttpModule(conf: HttpConf,
registry: MetricRegistry) {
lazy val httpService: HttpService = new HttpServiceImpl(new HttpServer(conf, registry))
@Module
class HttpModule(httpConf: HttpConf, metricsRegistry: MetricRegistry) {
lazy val httpServer = wire[HttpServer]
lazy val httpService = wire[HttpServiceImpl]
}
25 changes: 12 additions & 13 deletions src/main/scala/mesosphere/chaos/metrics/MetricsModule.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package mesosphere.chaos.metrics

import java.lang.management.ManagementFactory

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.jvm.{ BufferPoolMetricSet, GarbageCollectorMetricSet, MemoryUsageGaugeSet, ThreadStatesGaugeSet }
import com.codahale.metrics.jvm._
import mesosphere.chaos.metrics.impl.{ DefaultBufferPoolMetricSet, DefaultMetricRegistry }
import org.slf4j.LoggerFactory

import com.softwaremill.macwire._

/**
* Includes a registry of metric instances. Those contain:
* - a set of gauges for the counts and elapsed times of garbage collections.
Expand All @@ -14,14 +14,13 @@ import org.slf4j.LoggerFactory
* GC-specific memory pools.
* - a set of gauges for the number of threads in their various states and deadlock detection.
*/
@Module
class MetricsModule {
private[this] val log = LoggerFactory.getLogger(getClass.getName)
lazy val registry: MetricRegistry = {
val registry = new MetricRegistry
registry.register("jvm.gc", new GarbageCollectorMetricSet())
registry.register("jvm.buffers", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer))
registry.register("jvm.memory", new MemoryUsageGaugeSet())
registry.register("jvm.threads", new ThreadStatesGaugeSet())
registry
}
}

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]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mesosphere.chaos.metrics.impl

import java.lang.management.ManagementFactory
import com.codahale.metrics.jvm.BufferPoolMetricSet

class DefaultBufferPoolMetricSet extends BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mesosphere.chaos.metrics.impl

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.jvm.ThreadStatesGaugeSet
import com.codahale.metrics.jvm.MemoryUsageGaugeSet
import com.codahale.metrics.jvm.BufferPoolMetricSet
import com.codahale.metrics.jvm.GarbageCollectorMetricSet

class DefaultMetricRegistry(garbageCollectorMetricSet: GarbageCollectorMetricSet,
bufferPoolMetricSet: BufferPoolMetricSet,
memoryUsageGaugeSet: MemoryUsageGaugeSet,
threadStatesGaugeSet: ThreadStatesGaugeSet) extends MetricRegistry {
register("jvm.gc", garbageCollectorMetricSet)
register("jvm.buffers", bufferPoolMetricSet)
register("jvm.memory", memoryUsageGaugeSet)
register("jvm.threads", threadStatesGaugeSet)
}
12 changes: 8 additions & 4 deletions src/main/scala/mesosphere/chaos/rest/RestModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import javax.servlet.http.HttpServlet
import com.codahale.metrics.servlets.MetricsServlet
import mesosphere.chaos.ChaosModule
import mesosphere.chaos.rest.impl.{ LogConfigServlet, PingServlet }
import com.softwaremill.macwire._

/**
* Registers a couple of default servlets to the Jetty server. Those are:
Expand All @@ -13,20 +14,23 @@ import mesosphere.chaos.rest.impl.{ LogConfigServlet, PingServlet }
* - logging servlet. Allows dynamic adjustments of http configuration.
* @param chaosModule module including the metrics and http modules.
*/
@Module
class RestModule(chaosModule: ChaosModule) {
// 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 = new PingServlet
lazy val metricsServlet: HttpServlet = new MetricsServlet(chaosModule.metricsModule.registry)
lazy val logConfigServlet: HttpServlet = new LogConfigServlet
lazy val pingServlet: HttpServlet = wire[PingServlet]
lazy val registry = chaosModule.metricsModule.registry
lazy val metricsServlet: HttpServlet = new MetricsServlet(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)
}
}

0 comments on commit fe17b1d

Please sign in to comment.