/metrics-scala

The scala API for Coda Hale's Metrics.

Primary LanguageScalaApache License 2.0Apache-2.0

Metrics-Scala

Capturing JVM- and application-level metrics. So you know what's going on.

This is the Scala API for Coda Hale's Metrics library.

Initially this project started out as a line for line copy of the Metrics-scala module, released for multiple scala versions. Metrics dropped the scala module in version 3.0.0 and this project continued separately with the help of @scullxbones.

Contents

  • Usage (version 3.x)
  • Usage (version 2.x)
  • Features
  • Available versions
  • Download
  • License

Usage (version 3.x)

In Metrics 3 you have to specify an application wide MetricRegistry. Create an Instrumented trait that refers to that registry and that extends the InstrumentedBuilder trait.

object YourApplication {
  /** The application wide metrics registry. */
  val metricRegistry = new com.codahale.metrics.MetricRegistry()
}
trait Instrumented extends nl.grons.metrics.scala.InstrumentedBuilder {
  val metricRegistry = YourApplication.metricRegistry
}

Now you can create metrics by using the metrics metrics builder.

class Example(db: Database) extends Instrumented {
  private[this] val loading = metrics.timer("loading")

  def loadStuff(): Seq[Row] = loading.time {
    db.fetchRows()
  }
}

There are Scala wrappers for each metric type: gauge, counter, histogram, meter and timer.

For more information on Metrics 3.x, please see the documentation.

See the change log for API changes compared to the 2.x versions.

Usage (version 2.x)

Metrics-scala provides the Instrumented trait for Scala applications. This trait gives you the metrics builder metrics.

import com.yammer.metrics.scala.Instrumented

class Example(db: Database) extends Instrumented {
  private[this] val loading = metrics.timer("loading")

  def loadStuff(): Seq[Row] = loading.time {
    db.fetchRows()
  }
}

There are Scala wrappers for each metric type: gauge, counter, histogram, meter and timer.

For more information on Metrics 2.x, please see the documentation in the Way Back Machine or read it directly from Metrics 2.2.0 git branch.

Features

  • Easy creation of all metrics types.
  • Almost invisible syntax for using timers (see example below).
  • Scala specific methods on metrics (e.g. += on counters).

Planned:

  • Health check support.
  • Actor support.
  • Future support.

Available versions

Please consult the table below to see which versions of metrics-scala are available for which scala versions.

Note that only the versions 2.1.4 and 2.1.5 support OSGI.

Metrics-
scala
version
Metrics-
core
version
Scala version
2.9.1 2.9.1-1 2.9.2 2.9.3 2.10.0-RC1 2.10.x
2.1.2 2.1.2
2.1.3 2.1.3
2.1.4 2.1.4
2.1.5 2.1.5
2.2.0 2.2.0
3.0.0 3.0.0

If you need another version mix please open an issue, or sent an email to the metrics mailing list.

Download

SBT:

libraryDependencies += "nl.grons" %% "metrics-scala" % "3.0.0"

Maven:

<properties>
    <scala.version>2.10.0</scala.version>
    <scala.dep.version>2.10</scala.dep.version>
</properties>
<dependency>
    <groupId>nl.grons</groupId>
    <artifactId>metrics-scala_${scala.dep.version}</artifactId>
    <version>3.0.0</version>
</dependency>

Note: For scala versions before 2.10, you need to use the full scala version in the artifact name; e.g. metrics-scala_2.9.1-1.

Note: If you depend on JMX: 2.2.0 has a small bug that makes it inconvenient to use JMX.

License

Copyright (c) 2010-2012 Coda Hale, Yammer.com (before 3.0.0)

Copyright (c) 2013 Erik van Oosten (3.0.0 and later)

Published under Apache Software License 2.0, see LICENSE