Easy way to send Finagle metrics to Codahale Metrics library.
finagle-metrics enables your finagle based application to send its metrics to Codahale Metrics library instead of the default metrics (finagle-stats).
$ git clone https://github.com/rlazoti/finagle-metrics.git
$ cd finagle-metrics
$ sbt package
$ sbt test
Finagle-metrics is available on OSS Sonatype.
So everything you need to do is add the sbt dependency like:
"com.github.rlazoti" %% "finagle-metrics" % "0.0.13"
or
"com.github.rlazoti" % "finagle-metrics_2.12" % "0.0.13"
Note that finagle-metrics is cross-published for both 2.11 and 2.12 so you can use 2.11 scala version as well.
If you're using twitter-server to create your finagle services, everything you need to do is just adding the finagle-metrics dependency to your project.
You don't need to add the finagle-stats to your project, but both libraries will work together without any issue if you need it.
You can include your own metrics through the statsReceiver field of TwitterServer, so your metrics will be sent to Codahale Metrics as well.
You can obtain an instance of MetricRegistry class through the field metrics of MetricsStatsReceiver.
import com.twitter.finagle.metrics.MetricsStatsReceiver
val myCustomMeter = MetricsStatsReceiver.metrics.meter("my-custom-meter")
myCustomMeter.mark()
Codahale Metrics library has reporters for many diferent outputs.
Let's take the GraphiteReporter as example.
import com.codahale.metrics.graphite.{ Graphite, GraphiteReporter }
import com.twitter.finagle.metrics.MetricsStatsReceiver
import java.util.concurrent.TimeUnit
val graphite = new Graphite(new InetSocketAddress("graphite.example.com", 2003))
val reporter = GraphiteReporter.forRegistry(MetricsStatsReceiver.metrics)
.prefixedWith("finagle-service.example.com")
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(graphite)
reporter.start(1, TimeUnit.MINUTES)
And an example using JmxReporter.
import com.codahale.metrics.JmxReporter
import com.twitter.finagle.metrics.MetricsStatsReceiver
val reporter: JmxReporter = JmxReporter.forRegistry(MetricsStatsReceiver.metrics)
.build()
reporter.start()
Let's create a full example that reports its metrics to console each five seconds (or take a look at the example
folder).
Firstly, let's create the build.sbt:
name := "finagle-metrics-example"
version := "0.0.1-SNAPSHOT"
scalaVersion := "2.12.1"
resolvers += "twttr" at "https://maven.twttr.com/"
resolvers += Resolver.sonatypeRepo("public")
libraryDependencies ++= Seq(
"com.twitter" %% "twitter-server" % "20.3.0",
"com.github.rlazoti" %% "finagle-metrics" % "0.0.13"
)
Then the App.scala:
import com.codahale.metrics.ConsoleReporter
import com.twitter.finagle.{ Http, Service }
import com.twitter.finagle.metrics.MetricsStatsReceiver
import com.twitter.finagle.http.{ Request, Response, Status }
import com.twitter.io.Charsets
import com.twitter.server.TwitterServer
import com.twitter.util.{ Await, Future }
import java.util.concurrent.TimeUnit
object App extends TwitterServer {
val service = new Service[Request, Response] {
def apply(request: Request) = {
val response = Response(request.version, Status.Ok)
response.contentString = "hello"
Future.value(response)
}
}
val reporter = ConsoleReporter
.forRegistry(MetricsStatsReceiver.metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build
def main() = {
val server = Http.serve(":8080", service)
reporter.start(5, TimeUnit.SECONDS)
onExit { server.close() }
Await.ready(server)
}
}
That's all Folks! :)
Rodrigo Lazoti - [email protected]