Permalink
Browse files

path-dependent types for Gauge and Counter classes

  • Loading branch information...
1 parent 25b7ee8 commit 0b8a78659fb047f029a64662c83d9bc651ca5cb5 Nick Kallen committed Jan 11, 2011
View
19 finagle-core/src/main/scala/com/twitter/finagle/stats/JavaLoggerStatsReceiver.scala
@@ -8,24 +8,17 @@ import com.twitter.finagle.util.Conversions._
case class JavaLoggerStatsReceiver(logger: Logger) extends StatsReceiver {
val timer = new HashedWheelTimer()
- private[this] class Counter(description: Seq[(String, String)]) extends OCounter {
- def incr(delta: Int) {
- logger.info("%s incr %d".format(formatDescription(description), delta))
- }
-
- val sum = 0
- }
-
- private[this] class Gauge(description: Seq[(String, String)]) extends OGauge {
+ def gauge(description: (String, String)*): Gauge = new super.Gauge {
def measure(value: Float) {
logger.info("%s measure %f".format(formatDescription(description), value))
}
-
- val summary = Summary(0.0f, 0)
}
- def gauge(description: (String, String)*): OGauge = new Gauge(description)
- def counter(description: (String, String)*): OCounter = new Counter(description)
+ def counter(description: (String, String)*): Counter = new super.Counter {
+ def incr(delta: Int) {
+ logger.info("%s incr %d".format(formatDescription(description), delta))
+ }
+ }
def mkGauge(name: Seq[(String, String)], f: => Float) {
timer(10.seconds) {
View
8 finagle-core/src/main/scala/com/twitter/finagle/stats/SimpleStatsRepository.scala
@@ -5,14 +5,14 @@ import java.util.concurrent.atomic.AtomicInteger
import com.twitter.concurrent.Serialized
class SimpleStatsRepository extends StatsRepository {
- class Counter extends OCounter {
+ class Counter extends super.Counter {
private[this] val _sum = new AtomicInteger(0)
def incr(delta: Int) { _sum.addAndGet(delta) }
def sum = _sum.get
}
- class Gauge extends OGauge with Serialized {
+ class Gauge extends super.Gauge with Serialized {
@volatile private[this] var _summary = Summary(0.0f, 0)
def measure(value: Float) {
@@ -33,8 +33,8 @@ class SimpleStatsRepository extends StatsRepository {
config.compute { _ => new Gauge }
}
- def counter(path: (String, String)*): OCounter = counters(path)
- def gauge(path: (String, String)*): OGauge = gauges(path)
+ def counter(path: (String, String)*): Counter = counters(path)
+ def gauge(path: (String, String)*): Gauge = gauges(path)
/**
* Unsupported for now.
View
36 finagle-core/src/main/scala/com/twitter/finagle/stats/StatsReceiver.scala
@@ -1,27 +1,27 @@
package com.twitter.finagle.stats
-/**
- * A readable and writeable Counter. Only sums are kept of Counters.
- * An example Counter is "number of requests served".
- */
-trait Counter extends {
- def incr(delta: Int)
- def incr() { incr(1) }
-}
+trait StatsReceiver {
+ /**
+ * A readable and writeable Counter. Only sums are kept of Counters.
+ * An example Counter is "number of requests served".
+ */
+ trait Counter extends {
+ def incr(delta: Int)
+ def incr() { incr(1) }
+ }
-/**
- * A readable and writeable Gauge. Gauages are usually continuous
- * values that are measured at moments in time (e.g., the value
- * of a share of Twitter's stock).
- */
-trait Gauge {
/**
- * Record a measurement
+ * A readable and writeable Gauge. Gauages are usually continuous
+ * values that are measured at moments in time (e.g., the value
+ * of a share of Twitter's stock).
*/
- def measure(value: Float)
-}
+ trait Gauge {
+ /**
+ * Record a measurement
+ */
+ def measure(value: Float)
+ }
-trait StatsReceiver {
/**
* Get a Counter with the description
*/
View
77 finagle-core/src/main/scala/com/twitter/finagle/stats/StatsRepository.scala
@@ -1,59 +1,68 @@
package com.twitter.finagle.stats
-trait OCounter extends com.twitter.finagle.stats.Counter {
- def sum: Int
-}
-
-trait OGauge extends com.twitter.finagle.stats.Gauge {
- /**
- * An atomic snapshot of summary statistics.
- */
- case class Summary(total: Float, count: Int)
-
- /**
- * Arithmetic mean
- */
- def mean = {
- val snapshot = summary
- snapshot.total / snapshot.count
- }
-
- /**
- * Get an atomic snapshot of summary statistics
- */
- def summary: Summary
-}
-
-
/**
* A service for storing and collecting statistics. The kinds of data
* that can be measured include Counters (which maintains only a sum)
* and Gauges (which maintains a set of summary statistics such as
* mean).
*/
trait StatsRepository extends StatsReceiver {
+ trait Counter extends super.Counter {
+ def sum: Int
+ }
+
+ trait Gauge extends super.Gauge {
+ /**
+ * An atomic snapshot of summary statistics.
+ */
+ case class Summary(total: Float, count: Int)
+
+ /**
+ * Arithmetic mean
+ */
+ def mean = {
+ val snapshot = summary
+ snapshot.total / snapshot.count
+ }
+
+ /**
+ * Get an atomic snapshot of summary statistics
+ */
+ def summary: Summary
+ }
/**
* Get a Counter with the description
*/
- def counter(description: (String, String)*): OCounter
+ def counter(description: (String, String)*): Counter
/**
* Get a Gauge with the given description
*/
- def gauge(description: (String, String)*): OGauge
+ def gauge(description: (String, String)*): Gauge
/**
* Prepends a prefix description to all descriptions on this StatsRepository
*/
def scope(prefix: (String, String)*) = {
val self = this
new StatsRepository {
- def counter(description: (String, String)*): OCounter =
- self.counter(prefix ++ description: _*)
+ def counter(description: (String, String)*) = new super.Counter {
+ private[this] val underlying = self.counter(prefix ++ description: _*)
+
+ def incr(delta: Int) { underlying.incr(delta) }
+ def sum = underlying.sum
+ }
- def gauge(description: (String, String)*): OGauge =
- self.gauge(prefix ++ description: _*)
+ def gauge(description: (String, String)*) = new super.Gauge {
+ private[this] val underlying = self.gauge(prefix ++ description: _*)
+
+ def measure(value: Float) {
+ underlying.measure(value)
+ }
+
+ def summary = Summary(underlying.summary.total, underlying.summary.count)
+ }
def mkGauge(description: Seq[(String, String)], f: => Float) {
self.mkGauge(prefix ++ description, f)
@@ -66,17 +75,15 @@ trait StatsRepository extends StatsReceiver {
* A StatsRepository that discards all data
*/
class NullStatsRepository extends StatsRepository {
- private[this] class Gauge extends OGauge {
+ def gauge(description: (String, String)*) = new super.Gauge {
val summary = Summary(0.0f, 0)
def measure(value: Float) {}
}
- private[this] class Counter extends OCounter {
+ def counter(description: (String, String)*) = new super.Counter {
def incr(delta: Int) {}
val sum = 0
}
- def gauge(description: (String, String)*): OGauge = new Gauge
- def counter(description: (String, String)*): OCounter = new Counter
def mkGauge(description: Seq[(String, String)], f: => Float) {}
}
View
14 finagle-core/src/main/scala/com/twitter/finagle/stats/TimeWindowedStatsRepository.scala
@@ -25,29 +25,27 @@ class TimeWindowedStatsRepository(numIntervals: Int, interval: Duration, timer:
position += 1
}
- private[this] class Counter(path: (String, String)*) extends OCounter {
- private[this] def current = currentRepository.counter(path: _*)
+ def counter(description: (String, String)*) = new super.Counter {
+ private[this] def current = currentRepository.counter(description: _*)
def sum = repositories.foldLeft(0) { (total, repository) =>
- total + repository.counter(path: _*).sum
+ total + repository.counter(description: _*).sum
}
def incr(delta: Int) = current.incr(delta)
}
- private[this] class Gauge(path: (String, String)*) extends OGauge {
- private[this] def current = currentRepository.gauge(path: _*)
+ def gauge(description: (String, String)*) = new super.Gauge {
+ private[this] def current = currentRepository.gauge(description: _*)
def summary = repositories.foldLeft(Summary(0.0f, 0)) { (acc, repository) =>
- val summary = repository.gauge(path: _*).summary
+ val summary = repository.gauge(description: _*).summary
Summary(acc.total + summary.total, acc.count + summary.count)
}
def measure(value: Float) = current.measure(value)
}
- def counter(path: (String, String)*): OCounter = new Counter(path: _*)
- def gauge(path: (String, String)*): OGauge = new Gauge(path: _*)
def mkGauge(description: Seq[(String, String)], f: => Float) {
timer.schedule(Time.now, interval)(f)
}
View
2 finagle-ostrich/src/main/scala/com/twitter/finagle/stats/OstrichStatsReceiver.scala
@@ -28,4 +28,4 @@ class OstrichStatsReceiver extends StatsReceiver {
"%s_%s".format(key, value)
}.mkString("__")
}
-}
+}

0 comments on commit 0b8a786

Please sign in to comment.