Permalink
Browse files

multiplexing stats to a receiver

  • Loading branch information...
Nick Kallen
Nick Kallen committed Jan 11, 2011
1 parent e6ed587 commit b964921819f8d122451e1a53845883c6e3f6bd7e
@@ -223,9 +223,13 @@ case class ClientBuilder(
val statsRepository = {
val statsRepository = new TimeWindowedStatsRepository(
_loadStatistics._1, _loadStatistics._2, timer)
- statsRepository.scope(
+ val scoped = statsRepository.scope(
"service" -> _name.getOrElse(""),
"host" -> host.toString)
+ if (_statsReceiver.isDefined)
+ scoped.reportTo(_statsReceiver.get)
+ else
+ scoped
}
val failureAccruingStatsRepo = {
@@ -4,19 +4,18 @@ trait ReadableCounter extends Counter {
def sum: Int
}
-trait ReadableGauge extends Gauge {
- /**
- * An atomic snapshot of summary statistics.
- */
- case class Summary(total: Float, count: Int)
+/**
+ * An atomic snapshot of summary statistics.
+ */
+case class Summary(total: Float, count: Int) {
+ val mean = total / count
+}
+trait ReadableGauge extends Gauge {
/**
* Arithmetic mean
*/
- def mean = {
- val snapshot = summary
- snapshot.total / snapshot.count
- }
+ def mean = summary.mean
/**
* Get an atomic snapshot of summary statistics
@@ -56,6 +55,41 @@ trait StatsRepository extends StatsReceiver {
}
}
}
+
+ /**
+ * Multiplex measurements to a StatsReceiver
+ */
+ def reportTo(receiver: StatsReceiver) = {
+ val self = this
+ new StatsRepository {
+ def mkGauge(description: Seq[(String, String)], f: => Float) {
+ self.mkGauge(description, f)
+ receiver.mkGauge(description, f)
+ }
+
+ def gauge(description: (String, String)*) = new ReadableGauge {
+ private[this] val underlying = self.gauge(description: _*)
+
+ def measure(value: Float) {
+ underlying.measure(value)
+ receiver.gauge(description: _*).measure(value)
+ }
+
+ def summary = underlying.summary
+ }
+
+ def counter(description: (String, String)*) = new ReadableCounter {
+ private[this] val underlying = self.counter(description: _*)
+
+ def incr(delta: Int) {
+ underlying.incr(delta)
+ receiver.counter(description: _*).incr(delta)
+ }
+
+ def sum = underlying.sum
+ }
+ }
+ }
}
/**

0 comments on commit b964921

Please sign in to comment.