Permalink
Browse files

Added HealthMetrics trait to bigtop-util

  • Loading branch information...
1 parent af01ee4 commit e5bbd20dc562d0c471b9ee0c6622eb384251f3a7 @davegurnell davegurnell committed May 11, 2012
Showing with 63 additions and 1 deletion.
  1. +3 −1 project/Build.scala
  2. +60 −0 util/src/main/scala/bigtop/util/HealthMetrics.scala
View
@@ -29,6 +29,8 @@ object BigtopBuild extends Build {
lazy val jodaConvert = "org.joda" % "joda-convert" % "1.2"
lazy val slf4s = "com.weiglewilczek.slf4s" %% "slf4s" % "1.0.7"
lazy val configrity = "org.streum" %% "configrity" % "0.9.0"
+ lazy val metricsCore = "com.yammer.metrics" % "metrics-core" % "2.1.2"
+ lazy val metricsScala = "com.yammer.metrics" %% "metrics-scala" % "2.1.2"
val blueeyesSettings = Seq(
resolvers := bigtopResolvers,
@@ -82,7 +84,7 @@ object BigtopBuild extends Build {
blueeyesSettings ++
Seq(
version := bigtopVersion,
- libraryDependencies ++= Seq(specs2)
+ libraryDependencies ++= Seq(specs2, metricsCore, metricsScala)
) : _*
).dependsOn(core)
@@ -0,0 +1,60 @@
+package bigtop
+package util
+
+// import com.weiglewilczek.slf4s.Logger
+import com.yammer.metrics.scala.Instrumented
+import akka.dispatch.{ Future, Promise }
+import blueeyes.core.http._
+// import blueeyes.core.http.HttpHeaders._
+import blueeyes.core.http.HttpStatusCodes._
+import blueeyes.core.service._
+// import blueeyes.core.data._
+// import blueeyes.json.JsonAST._
+// import blueeyes.json.JsonParser.ParseException
+// import blueeyes.persistence.mongo.Database
+// import bigtop.concurrent.FutureValidation
+// import bigtop.concurrent.FutureImplicits._
+// import bigtop.problem.Problem
+import java.util.concurrent.TimeUnit
+// import scalaz._
+// import scalaz.syntax.validation._
+
+trait HealthMetrics extends Instrumented {
+ lazy val responseTimeUnit = TimeUnit.MILLISECONDS
+ lazy val responseCountTimeUnit = TimeUnit.SECONDS
+
+ lazy val successes = metrics.meter("successes", "successes", null, responseCountTimeUnit)
+ lazy val warnings = metrics.meter("warnings", "warnings", null, responseCountTimeUnit)
+ lazy val clientErrors = metrics.meter("client-errors", "client-errors", null, responseCountTimeUnit)
+ lazy val serverErrors = metrics.meter("server-errors", "server-errors", null, responseCountTimeUnit)
+
+ // We want the Java timer with the stop() method here, not the Scala wrapper with a time(foo) method:
+ lazy val responseTimes = metrics.metricsRegistry.newTimer(
+ getClass,
+ "response-times",
+ responseTimeUnit,
+ responseCountTimeUnit
+ )
+
+ def healthMetrics[T](inner: AsyncHttpService[T]): AsyncHttpService[T] = {
+ new AsyncCustomHttpService[T] {
+ def metadata = None
+ def service = {
+ (req: HttpRequest[T]) =>
+ inner.service(req) map { futureRes =>
+ val responseTimer = responseTimes.time()
+ futureRes.map { res: HttpResponse[T] =>
+ res.status.code match {
+ case _ : HttpSuccess => successes.mark()
+ case _ : HttpWarning => warnings.mark()
+ case _ : ClientError => clientErrors.mark()
+ case _ : ServerError => serverErrors.mark()
+ }
+ responseTimer.stop()
+ res
+ }
+ }
+ }
+ }
+ }
+}

0 comments on commit e5bbd20

Please sign in to comment.