Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'scala' of code.corp.twilio.com:kelvin/shadow into scala

  • Loading branch information...
commit 0f8b02cf15f033ba5d4f515ada24219ae98515be 2 parents d83279a + e696056
@kelvl kelvl authored
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
12 src/main/scala/com/twilio/shadow/Boot.scala
@@ -1,14 +1,12 @@
package com.twilio.shadow
-import akka.actor.{Actor, ActorSystem, Props}
+import akka.actor.{ActorSystem, Props}
import spray.io.{SingletonHandler, IOExtension}
import spray.can.server.{ServerSettings, HttpServer}
-import spray.io.IOServer.{Bound, Bind}
-import spray.can.client.{HttpClient, ClientSettings, DefaultHttpClient}
-import com.yammer.metrics.Metrics
-import com.yammer.metrics.core.MetricsRegistry
+import spray.io.IOServer.Bind
+import spray.can.client.{HttpClient, ClientSettings}
import com.typesafe.config.ConfigFactory
-import spray.http.HttpResponse
+import com.codahale.metrics.MetricRegistry
object Boot extends App{
@@ -16,7 +14,7 @@ object Boot extends App{
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
79 src/main/scala/com/twilio/shadow/HubActor.scala
@@ -1,79 +0,0 @@
-package com.twilio.shadow
-
-import akka.actor.{ActorRef, Actor}
-import spray.util.SprayActorLogging
-import scala.collection.mutable
-import spray.http.{FormData, HttpRequest, HttpResponse}
-import spray.httpx.unmarshalling._
-import org.json4s.JsonDSL._
-import org.json4s.native.JsonMethods._
-
-case class Register(eventSourceActor: ActorRef)
-case class UnRegister(eventSourceActor: ActorRef)
-case class Broadcast(message: AnyRef)
-
-class HubActor extends Actor with SprayActorLogging {
-
- val eventSources = mutable.HashSet[ActorRef]()
-
- def receive = {
- case Register(sseActor) => {
- eventSources += sseActor
-
- log.info(s"Registering actor $sseActor")
- }
- case UnRegister(sseActor) => {
- eventSources -= sseActor
- log.info(s"UnRegistering actor $sseActor")
- }
-
- case Broadcast(entry: ShadowEntry) => {
- val jsonStr = compact(render(shadowEntryJson(entry)))
- eventSources.foreach( _ ! jsonStr )
- }
- }
-
-
- def shadowEntryJson(entry: ShadowEntry) = {
- val req = httpRequestToJson(entry.request)
-
- val json =
- ("request" -> (
- ("original" -> req) ~
- ("modified" -> req)
- )) ~
- ("results" -> List(entry.responses._1, entry.responses._2).map( x => httpResponseToJson(x._1) ~ ("elapsed_time" -> x._2) ))
-
- json
- }
-
- def httpRequestToJson(httpRequest: HttpRequest) = {
-
- val request = httpRequest.parseAll
-
- val formParams = request.entity.as[FormData].fold( fa => Map[String, String](), fb => fb.fields)
-
- val queryParams = request.queryParams
-
- val json =
- ("url" -> request.path) ~
- ("headers" -> request.headers.map { x => x.name -> x.value }.toMap ) ~
- ("post" -> formParams) ~
- ("method" -> request.method.value) ~
- ("get" -> queryParams)
- json
- }
-
- def httpResponseToJson(httpResponse: HttpResponse) = {
-
- val json =
- ("headers" -> httpResponse.headers.map { x => (
- (x.name -> x.value)
- ) }.toMap) ~
- ("status_code" -> httpResponse.status.value) ~
- ("type" -> "http_response") ~
- ("body" -> httpResponse.entity.asString)
- json
- }
-
-}
View
17 src/main/scala/com/twilio/shadow/ProxyService.scala
@@ -2,17 +2,15 @@ package com.twilio.shadow
import akka.actor.{ActorSystem, ActorRef, Actor}
import spray.routing._
-import spray.routing.directives.LoggingMagnet
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
@@ -30,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
29 src/main/scala/com/twilio/shadow/UIService.scala
@@ -1,6 +1,6 @@
package com.twilio.shadow
-import akka.actor.{Props, ActorRef, Actor}
+import akka.actor.{Props, Actor}
import spray.routing._
import spray.http._
import spray.util.SprayActorLogging
@@ -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
6 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
@@ -17,7 +16,7 @@ import spray.httpx.RequestBuilding.Get
import scala.concurrent.Future
import java.util.concurrent.TimeUnit
import org.scalatest.concurrent.Futures
-import org.scalatest.time.{Span, SpanSugar}
+import org.scalatest.time.SpanSugar
import spray.io.SingletonHandler
import scala.util.Failure
import scala.util.Success
@@ -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) {
Please sign in to comment.
Something went wrong with that request. Please try again.