Easy way to send Finagle metrics to Codahale Metrics library
Scala Shell
Clone or download
Rodrigo Lazoti
Latest commit 04a3e8f Oct 30, 2017
Permalink
Failed to load latest commit information.
example
project
src
.gitignore
.travis.yml
LICENSE Initial commit Jan 6, 2015
README.md
build.sbt
release.sh
sonatype.sbt
version.sbt

README.md

finagle-metrics

Easy way to send Finagle metrics to Codahale Metrics library.

Overview

finagle-metrics enables your finagle based application to send its metrics to Codahale Metrics library instead of the default metrics (finagle-stats).

Build

$ git clone https://github.com/rlazoti/finagle-metrics.git
$ cd finagle-metrics
$ sbt package

Test

$ sbt test

Setup

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.12"

or

"com.github.rlazoti" % "finagle-metrics_2.12" % "0.0.12"

Note that finagle-metrics is cross-published for both 2.11 and 2.12 so you can use 2.11 scala version as well.

Usage

Using twitter-server

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.

Including your own metrics

You can include your own metrics through the statsReceiver field of TwitterServer, so your metrics will be sent to Codahale Metrics as well.

Including your own metrics through Codahale Metrics

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()

Reporting

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()

Full Example

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/"

libraryDependencies ++= Seq(
  "com.twitter"        %% "twitter-server"  % "1.31.0",
  "com.github.rlazoti" %% "finagle-metrics" % "0.0.12"
)

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! :)

Author

Rodrigo Lazoti - rodrigolazoti@gmail.com