Permalink
Browse files

remove path-dependent types. This is less scala-ish but the code is e…

…asier to understand
  • Loading branch information...
1 parent c284b4f commit 8c0718b1aeaa99c606eb51ae2e9f738df1c0ec53 Nick Kallen committed Jan 11, 2011
@@ -8,13 +8,13 @@ import com.twitter.finagle.util.Conversions._
case class JavaLoggerStatsReceiver(logger: Logger) extends StatsReceiver {
val timer = new HashedWheelTimer()
- def gauge(description: (String, String)*): Gauge = new super.Gauge {
+ def gauge(description: (String, String)*) = new Gauge {
def measure(value: Float) {
logger.info("%s measure %f".format(formatDescription(description), value))
}
}
- def counter(description: (String, String)*): Counter = new super.Counter {
+ def counter(description: (String, String)*) = new Counter {
def incr(delta: Int) {
logger.info("%s incr %d".format(formatDescription(description), delta))
}
@@ -5,14 +5,14 @@ import java.util.concurrent.atomic.AtomicInteger
import com.twitter.concurrent.Serialized
class SimpleStatsRepository extends StatsRepository {
- class Counter extends super.Counter {
+ private[this] class Counter extends ReadableCounter {
private[this] val _sum = new AtomicInteger(0)
def incr(delta: Int) { _sum.addAndGet(delta) }
def sum = _sum.get
}
- class Gauge extends super.Gauge with Serialized {
+ private[this] class Gauge extends ReadableGauge 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)*): Counter = counters(path)
- def gauge(path: (String, String)*): Gauge = gauges(path)
+ def counter(path: (String, String)*): ReadableCounter = counters(path)
+ def gauge(path: (String, String)*): ReadableGauge = gauges(path)
/**
* Unsupported for now.
@@ -1,27 +1,27 @@
package com.twitter.finagle.stats
-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 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 {
/**
- * 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).
+ * Record a measurement
*/
- trait Gauge {
- /**
- * Record a measurement
- */
- def measure(value: Float)
- }
+ def measure(value: Float)
+}
+trait StatsReceiver {
/**
* Get a Counter with the description
*/
@@ -1,68 +1,55 @@
package com.twitter.finagle.stats
+trait ReadableCounter extends Counter {
+ def sum: Int
+}
+
+trait ReadableGauge extends 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)*): Counter
+ def counter(description: (String, String)*): ReadableCounter
/**
* Get a Gauge with the given description
*/
- def gauge(description: (String, String)*): Gauge
+ def gauge(description: (String, String)*): ReadableGauge
/**
* 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)*) = 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)*) = new super.Gauge {
- private[this] val underlying = self.gauge(prefix ++ description: _*)
+ def counter(description: (String, String)*) = self.counter(prefix ++ description: _*)
- def measure(value: Float) = underlying.measure(value)
- def summary = {
- val snapshot = underlying.summary
- Summary(snapshot.total, snapshot.count)
- }
- }
+ def gauge(description: (String, String)*) = self.gauge(prefix ++ description: _*)
def mkGauge(description: Seq[(String, String)], f: => Float) {
self.mkGauge(prefix ++ description, f)
@@ -75,12 +62,12 @@ trait StatsRepository extends StatsReceiver {
* A StatsRepository that discards all data
*/
class NullStatsRepository extends StatsRepository {
- def gauge(description: (String, String)*) = new super.Gauge {
+ def gauge(description: (String, String)*) = new ReadableGauge {
val summary = Summary(0.0f, 0)
def measure(value: Float) {}
}
- def counter(description: (String, String)*) = new super.Counter {
+ def counter(description: (String, String)*) = new ReadableCounter {
def incr(delta: Int) {}
val sum = 0
}
@@ -25,7 +25,7 @@ class TimeWindowedStatsRepository(numIntervals: Int, interval: Duration, timer:
position += 1
}
- def counter(description: (String, String)*) = new super.Counter {
+ def counter(description: (String, String)*) = new ReadableCounter {
private[this] def current = currentRepository.counter(description: _*)
def sum = repositories.foldLeft(0) { (total, repository) =>
@@ -35,7 +35,7 @@ class TimeWindowedStatsRepository(numIntervals: Int, interval: Duration, timer:
def incr(delta: Int) = current.incr(delta)
}
- def gauge(description: (String, String)*) = new super.Gauge {
+ def gauge(description: (String, String)*) = new ReadableGauge {
private[this] def current = currentRepository.gauge(description: _*)
def summary = repositories.foldLeft(Summary(0.0f, 0)) { (acc, repository) =>

0 comments on commit 8c0718b

Please sign in to comment.