Skip to content

Commit

Permalink
add metrics, clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
andimiller committed Jul 14, 2015
1 parent 2f5ac4a commit d0d5a33
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ resolvers ++= Seq(

libraryDependencies += "io.backchat.hookup" % "hookup_2.10" % "0.4.0"
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.2"
libraryDependencies += "io.dropwizard.metrics" % "metrics-core" % "3.1.2"
libraryDependencies += "io.dropwizard.metrics" % "metrics-graphite" % "3.1.2"

mainClass := Some("SovRelay")
41 changes: 26 additions & 15 deletions src/main/scala/SovRelay.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import java.net.InetSocketAddress
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

import com.codahale.metrics.graphite.{Graphite, GraphiteReporter}
import com.codahale.metrics.{MetricFilter, ConsoleReporter, MetricRegistry}
import io.backchat.hookup._
import scala.concurrent.duration._
import dispatch._, Defaults._
Expand All @@ -8,29 +12,39 @@ import scala.util.{Failure, Try}

object SovRelay extends App {

case class Broadcast(x: String)
// set up metrics
val metrics = new MetricRegistry()
val connectedClients = metrics.counter("connected_clients")
val broadcastsSent = metrics.meter("broadcasts_sent")
val crestApiProblems = metrics.meter("crest_api_problems")

val reporter = GraphiteReporter.forRegistry(metrics)
.prefixedWith("moe.pizza.pizza-sov-relay")
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(new Graphite(new InetSocketAddress("localhost", 2003)))
reporter.start(1, TimeUnit.MINUTES)

// set up internal state
var lastUpdate: String = "{\"status\": \"starting up\"}"
var users = new AtomicInteger(0)

// set up websocket server
val server = HookupServer(8125) {
new HookupServerClient {
def receive = {
case Connected =>
send(lastUpdate)
users.incrementAndGet()
case TextMessage(text) =>
println(text)
send("{\"error\": \"this server cannot perform user-requested actions (except this one)\"}")
connectedClients.inc()
case TextMessage(text) => ()
case Disconnected(why) =>
println("user disconnected")
println(why)
users.decrementAndGet()
connectedClients.dec()
}
}
}
server.start

// set up the API poller
val system = akka.actor.ActorSystem("system")

implicit class EitherPimp[L <: Throwable,T](e:Either[L,T]) {
Expand All @@ -42,17 +56,14 @@ object SovRelay extends App {
val res = Http(svc OK as.String)
res.either.map {
case Right(r) =>
println(r)
lastUpdate = r
server.broadcast(new TextMessage(r))
broadcastsSent.mark()
case Left(t) =>
println(t.getMessage)
println(t)
server.broadcast(new TextMessage("{\"status\": \"ccp api is unavailable\"}"))
crestApiProblems.mark()
}
}

system.scheduler.schedule(1 seconds, 30 seconds)(pullLatest)
println("Scheduled puller")
server
system.scheduler.schedule(0 seconds, 30 seconds)(pullLatest)
}

0 comments on commit d0d5a33

Please sign in to comment.