Browse files

Moving to codahale metrics 3.0.0-BETA2 and refactoring json stats

  • Loading branch information...
1 parent 6bdbdb8 commit e696056cbf5fa015b6be5761d550d8d679707b23 @kelvl kelvl committed Apr 29, 2013
View
14 project/ShadowBuild.scala
@@ -19,12 +19,14 @@ object ShadowBuild extends Build {
assemblyCacheOutput in assembly := true,
libraryDependencies ++= List(
- "io.spray" % "spray-can" % "1.1-M7",
- "io.spray" % "spray-routing" % "1.1-M7",
- "io.spray" % "spray-testkit" % "1.1-M7",
- "com.typesafe.akka" %% "akka-actor" % "2.1.0",
- "org.json4s" %% "json4s-native" % "3.2.4",
- "nl.grons" %% "metrics-scala" % "2.2.0",
+ "io.spray" % "spray-can" % "1.1-M7",
+ "io.spray" % "spray-routing" % "1.1-M7",
+ "io.spray" % "spray-testkit" % "1.1-M7",
+ "com.typesafe.akka" %% "akka-actor" % "2.1.0",
+ "org.json4s" %% "json4s-native" % "3.2.4",
+ "org.json4s" %% "json4s-jackson"% "3.2.4",
+ "com.codahale.metrics" % "metrics-core" % "3.0.0-BETA2",
+ "com.codahale.metrics" % "metrics-json" % "3.0.0-BETA2",
// test
"org.scalatest" %% "scalatest" % "2.0.M5b" % "test",
View
4 src/main/scala/com/twilio/shadow/Boot.scala
@@ -5,16 +5,16 @@ import spray.io.{SingletonHandler, IOExtension}
import spray.can.server.{ServerSettings, HttpServer}
import spray.io.IOServer.Bind
import spray.can.client.{HttpClient, ClientSettings}
-import com.yammer.metrics.core.MetricsRegistry
import com.typesafe.config.ConfigFactory
+import com.codahale.metrics.MetricRegistry
object Boot extends App{
val system = ActorSystem("shadow-system")
val ioBridge = IOExtension(system).ioBridge()
val config = ConfigFactory.load()
- val metricsRegistry = new MetricsRegistry()
+ val metricsRegistry = new MetricRegistry()
val httpClient = system.actorOf(Props(new HttpClient(ioBridge, ClientSettings(config))), "client")
View
16 src/main/scala/com/twilio/shadow/ProxyService.scala
@@ -3,15 +3,14 @@ package com.twilio.shadow
import akka.actor.{ActorSystem, ActorRef, Actor}
import spray.routing._
import spray.can.client.HttpDialog
-import com.yammer.metrics.core.{MetricsRegistry, Clock, Timer}
import scala.concurrent.{Promise, Future}
import spray.http.{HttpResponse, HttpRequest}
-import java.util.concurrent.TimeUnit
+import com.codahale.metrics.{Timer, MetricRegistry}
class ProxyActor(
val httpClient: ActorRef,
- val metricsRegistry: MetricsRegistry,
+ val metricsRegistry: MetricRegistry,
val shadowConfig: ShadowConfig) extends Actor with ProxyService {
def actorRefFactory = context
@@ -29,20 +28,19 @@ case class ShadowConfig(trueHost: String, truePort: Int, shadowHost: String, sha
trait ProxyService extends HttpService {
val httpClient: ActorRef
- val metricsRegistry: MetricsRegistry
+ val metricsRegistry: MetricRegistry
val shadowConfig: ShadowConfig
def actorSystem: ActorSystem
- val trueTimer: Timer = metricsRegistry.newTimer(getClass, "true-server-resp")
- val shadowTimer: Timer = metricsRegistry.newTimer(getClass, "shadow-server-resp")
+ val trueTimer: Timer = metricsRegistry.timer(MetricRegistry.name(getClass, "true-server-resp"))
+ val shadowTimer: Timer = metricsRegistry.timer(MetricRegistry.name(getClass, "shadow-server-resp"))
def time[A](timer: Timer, future: Future[A]): Future[Long] = {
- val start = Clock.defaultClock().tick()
val ctx = timer.time()
val promise = Promise[Long]()
future.onComplete { x =>
- ctx.stop()
- promise.success(TimeUnit.NANOSECONDS.toMillis(Clock.defaultClock().tick() - start))
+ val timeElapsed = ctx.stop()
+ promise.success(timeElapsed)
}
promise.future
}
View
27 src/main/scala/com/twilio/shadow/UIService.scala
@@ -11,10 +11,13 @@ import spray.routing.RequestContext
import spray.http.ChunkedResponseStart
import org.json4s.native.JsonMethods._
import org.json4s.JsonDSL._
-import com.yammer.metrics.core.{Timer, MetricsRegistry}
+import com.codahale.metrics.MetricRegistry
+import com.codahale.metrics.json.MetricsModule
+import java.util.concurrent.TimeUnit
+import com.fasterxml.jackson.databind.ObjectMapper
-class UIActor(val metricsRegistry: MetricsRegistry) extends Actor with UIService {
+class UIActor(val metricRegistry: MetricRegistry) extends Actor with UIService {
def actorRefFactory = context
@@ -93,7 +96,14 @@ class ResponseStreamActor(val ctx: RequestContext) extends Actor with SprayActor
trait UIService extends HttpService {
- val metricsRegistry: MetricsRegistry
+ val metricRegistry: MetricRegistry
+
+ val objectMapper = new ObjectMapper()
+
+ objectMapper.registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false))
+
+ val prettyJsonPrinter = objectMapper.writerWithDefaultPrettyPrinter()
+
val myRoute =
path("stream") {
@@ -104,16 +114,7 @@ trait UIService extends HttpService {
path("stats") {
get {
complete {
- import scala.collection.JavaConversions._
-
- pretty(render("metrics" -> metricsRegistry.allMetrics().map { case (name, metric) =>
- name.getName -> (metric match {
- case t: Timer => {
- Map("mean" -> t.mean(), "stddev" -> t.stdDev())
- }
- })
- }.toMap
- ))
+ prettyJsonPrinter.writeValueAsString(metricRegistry)
}
}
} ~
View
4 src/test/scala/com/twilio/shadow/ProxySuite.scala
@@ -9,7 +9,6 @@ import spray.can.client.{HttpDialog, HttpClient}
import com.xebialabs.restito.builder.stub.StubHttp.whenHttp
import com.xebialabs.restito.semantics.Action.{custom => customAction, _}
import com.xebialabs.restito.semantics.Condition._
-import com.yammer.metrics.core.MetricsRegistry
import spray.can.server.HttpServer
import akka.testkit.TestProbe
import spray.io.IOServer.Bind
@@ -26,6 +25,7 @@ import org.glassfish.grizzly.http.util.HttpStatus
import com.xebialabs.restito.semantics.Action
import org.glassfish.grizzly.http.server.Response
import scala.concurrent.duration.Duration
+import com.codahale.metrics.MetricRegistry
trait ScalaFutures extends Futures {
implicit class ScalaFutureConcept[T](fut: Future[T]) extends FutureConcept[T] {
@@ -65,7 +65,7 @@ class ProxySuite extends FunSpec with SpanSugar with ShouldMatchers with BeforeA
trait EverythingFixture extends IOBridgeFixture {
val httpClient = system.actorOf(Props(new HttpClient(ioBridge)), "client")
- val metricsRegistry = new MetricsRegistry()
+ val metricsRegistry = new MetricRegistry()
}
def withStubServer(doTest: (StubServer, Int) => Any) {

0 comments on commit e696056

Please sign in to comment.