Skip to content

Commit

Permalink
Rewrote cached gauge (#52) to use FiniteDuration instead of Java's Ti…
Browse files Browse the repository at this point in the history
…meUnit, improved test.

Akka 2.3.4 -> 2.3.5.
Prepared for release of 3.2.1.
  • Loading branch information
erikvanoosten committed Aug 17, 2014
1 parent 167d266 commit 2a645e0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
TBD
===
v3.2.1: Aug 2014
================

* For future timing, invoke argument only once, closes #50. Bug report and solution from @JeffBellegarde.
* Added MetricBuilder.cachedGauge method (from @pasieronen).
* Added support for cached gauge, a nice pull request by @pasieronen (#52).
* For Akka 2.3 build: build against Akka 2.3.5.

v3.2.0: May 2014
================
Expand Down
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// See crossrelease.sh for valid combinations of akkaVersion and crossScalaVersion.

// Akka versions: 2.1.4, 2.2.3, 2.3.2
akkaVersion := "2.3.2"
// Build against Akka versions: 2.1.4, 2.2.3, 2.3.5,
// but developed against oldest supported version.
akkaVersion := "2.1.4"

organization := "nl.grons"

name := "metrics-scala"

lazy val baseVersion = "3.2.0"
lazy val baseVersion = "3.2.1"

version <<= (akkaVersion) { av =>
val akkaVersion = if (av.nonEmpty) "_a" + av.split('.').take(2).mkString(".") else ""
Expand All @@ -19,6 +20,7 @@ description <<= (scalaVersion, akkaVersion) { (sv, av) =>
"metrics-scala for " + akkaDescription + "Scala " + sbt.cross.CrossVersionUtil.binaryScalaVersion(sv)
}

// Developed with 2.10.0, build with 2.10.0 and 2.11.0.
scalaVersion := "2.10.0"

crossScalaVersions := Seq("2.10.0", "2.11.0")
Expand Down
2 changes: 1 addition & 1 deletion crossrelease.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ CMD=${1:-$DEFAULT_COMMAND}
sbt '; set akkaVersion := ""; set crossScalaVersions := Seq("2.10.0", "2.11.0")' +$CMD \
'; set akkaVersion := "2.1.4"; set crossScalaVersions := Seq("2.10.0")' +$CMD \
'; set akkaVersion := "2.2.4"; set crossScalaVersions := Seq("2.10.0")' +$CMD \
'; set akkaVersion := "2.3.4"; set crossScalaVersions := Seq("2.10.0", "2.11.0")' +$CMD
'; set akkaVersion := "2.3.5"; set crossScalaVersions := Seq("2.10.0", "2.11.0")' +$CMD
16 changes: 8 additions & 8 deletions src/main/scala/nl/grons/metrics/scala/MetricBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package nl.grons.metrics.scala

import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.MILLISECONDS
import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.{Gauge => CHGauge, CachedGauge => CHCachedGauge}
import scala.concurrent.duration.FiniteDuration

/**
* Builds and registering metrics.
Expand All @@ -35,15 +36,14 @@ class MetricBuilder(val baseName: MetricName, val registry: MetricRegistry) {
new Gauge[A](registry.register(metricName(name, scope), new CHGauge[A] { def getValue: A = f }))

/**
* Registers a new cached gauge metric.
* Registers a new gauge metric that caches its value for a given duration.
*
* @param name the name of the gauge
* @param timeout the timeout
* @param timeoutUnit the unit of <code>timeout</code>
* @param scope the scope of the gauge or null for no scope
* @param name the name of the gauge
* @param timeout the timeout
* @param scope the scope of the gauge or null for no scope
*/
def cachedGauge[A](name: String, timeout: Long, timeoutUnit: TimeUnit, scope: String = null)(f: => A): Gauge[A] =
new Gauge[A](registry.register(metricName(name, scope), new CHCachedGauge[A](timeout, timeoutUnit) { def loadValue: A = f }))
def cachedGauge[A](name: String, timeout: FiniteDuration, scope: String = null)(f: => A): Gauge[A] =
new Gauge[A](registry.register(metricName(name, scope), new CHCachedGauge[A](timeout.toMillis, MILLISECONDS) { def loadValue: A = f }))

/**
* Creates a new counter metric.
Expand Down
7 changes: 5 additions & 2 deletions src/test/scala/nl/grons/metrics/scala/MetricBuilderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.scalatest.OneInstancePerTest
import org.scalatest.FunSpec
import org.scalatest.junit.JUnitRunner
import com.codahale.metrics.MetricRegistry
import java.util.concurrent.TimeUnit
import scala.concurrent.duration._

@RunWith(classOf[JUnitRunner])
class MetricBuilderSpec extends FunSpec with OneInstancePerTest {
Expand All @@ -36,7 +36,7 @@ class MetricBuilderSpec extends FunSpec with OneInstancePerTest {
class UnderTest extends Instrumented {
val timer: Timer = metrics.timer("10ms")
val gauge: Gauge[Int] = metrics.gauge("the answer")(value)
val cachedGauge: Gauge[Int] = metrics.cachedGauge("cached", 1, TimeUnit.HOURS)(cachedValue)
val cachedGauge: Gauge[Int] = metrics.cachedGauge("cached", 300 milliseconds)(cachedValue)
val counter: Counter = metrics.counter("1..2..3..4")
val histogram: Histogram = metrics.histogram("histo")
val meter: Meter = metrics.meter("meter", "testscope")
Expand Down Expand Up @@ -81,6 +81,9 @@ class MetricBuilderSpec extends FunSpec with OneInstancePerTest {
underTest.cachedCalls should equal (1)
underTest.cachedGauge.value should equal (42)
underTest.cachedCalls should equal (1)
Thread.sleep(400L)
underTest.cachedGauge.value should equal (42)
underTest.cachedCalls should equal (2)
}

it("defines a counter") {
Expand Down

0 comments on commit 2a645e0

Please sign in to comment.