Skip to content

Instantly share code, notes, and snippets.

@jkpl
Created Jan 8, 2018
Embed
What would you like to do?
Dropwizard Graphite demo for Scala

DropWizard metrics on Graphite demo in Scala

This demo hooks up DropWizard based metrics to Graphite.

Running

Provide the following environment variables to run this demo:

  • APP_PORT: port in which the application is run
  • GRAPHITE_HOST: hostname for the Graphite instance where metrics are sent
  • GRAPHITE_PORT: port of the Graphite instance where metrics are sent

Use sbt to run this demo:

$ sbt run

Interacting

There's a "hello world" response exposed in the root path.

$ curl http://localhost:9000 # Assuming APP_PORT is 9000
Hello, world!
name := "dw-graphite-demo"
scalaVersion := "2.12.4"
// Metrics dependencies
val dropWizardVersion = "3.2.3"
libraryDependencies ++= Seq(
"io.dropwizard.metrics" % "metrics-core" % dropWizardVersion, // core metrics library
"io.dropwizard.metrics" % "metrics-graphite" % dropWizardVersion, // plugin for sending to Graphite
"io.dropwizard.metrics" % "metrics-jvm" % dropWizardVersion, // gathers JVM metrics
"nl.grons" %% "metrics-scala" % "3.5.9_a2.4" // Scala interface for metrics
)
// Other dependencies
libraryDependencies ++= Seq(
"com.twitter" %% "twitter-server" % "1.32.0"
)
package dwgraphitedemo
import java.lang.management.ManagementFactory
import java.util.concurrent.TimeUnit
import com.codahale.metrics.graphite.{Graphite, GraphiteReporter}
import com.codahale.metrics.SharedMetricRegistries
import com.codahale.metrics.jvm._
import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.finagle.{Http, Service}
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
import nl.grons.metrics.scala.DefaultInstrumented
object Main extends TwitterServer with DefaultInstrumented {
private val appPort = getEnv("APP_PORT")
private val graphiteHost = getEnv("GRAPHITE_HOST")
private val graphitePort = getEnv("GRAPHITE_PORT").toInt
private def getEnv(key: String): String =
sys.env.getOrElse(
key,
sys.error(s"Please specify env var $key")
)
private val helloWorldCounter = metrics.counter("helloworld")
override def failfastOnFlagsNotParsed: Boolean = true
private val service = new Service[Request, Response] {
override def apply(request: Request): Future[Response] = {
helloWorldCounter.inc()
log.debug("Got request to path " + request.path)
val response = Response(request.version, Status.Ok)
response.contentString = "Hello, world!\n"
Future.value(response)
}
}
def main(): Unit = {
setupMetrics()
val server = Http.server.serve(":" + appPort, service)
onExit {
server.close()
}
Await.ready(server)
}
private def setupMetrics(): Unit = {
val metricRegistry = SharedMetricRegistries.getOrCreate("default")
metricRegistry.register("jvm.gc", new GarbageCollectorMetricSet())
metricRegistry.register("jvm.memory", new MemoryUsageGaugeSet())
metricRegistry.register("jvm.threads", new ThreadStatesGaugeSet())
metricRegistry.register("jvm.fileDescriptor", new FileDescriptorRatioGauge())
metricRegistry.register("jvm.classLoading", new ClassLoadingGaugeSet())
metricRegistry.register("jvm.buffers", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer))
val graphite = new Graphite(graphiteHost, graphitePort)
val reporter = GraphiteReporter
.forRegistry(metricRegistry)
.prefixedWith("dwgraphitedemo")
.build(graphite)
reporter.start(10, TimeUnit.SECONDS)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment