Skip to content

Commit

Permalink
multiplexing stats to a receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Kallen committed Jan 11, 2011
1 parent e6ed587 commit b964921
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -223,9 +223,13 @@ case class ClientBuilder(
val statsRepository = { val statsRepository = {
val statsRepository = new TimeWindowedStatsRepository( val statsRepository = new TimeWindowedStatsRepository(
_loadStatistics._1, _loadStatistics._2, timer) _loadStatistics._1, _loadStatistics._2, timer)
statsRepository.scope( val scoped = statsRepository.scope(
"service" -> _name.getOrElse(""), "service" -> _name.getOrElse(""),
"host" -> host.toString) "host" -> host.toString)
if (_statsReceiver.isDefined)
scoped.reportTo(_statsReceiver.get)
else
scoped
} }


val failureAccruingStatsRepo = { val failureAccruingStatsRepo = {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ trait ReadableCounter extends Counter {
def sum: Int def sum: Int
} }


trait ReadableGauge extends Gauge { /**
/** * An atomic snapshot of summary statistics.
* An atomic snapshot of summary statistics. */
*/ case class Summary(total: Float, count: Int) {
case class Summary(total: Float, count: Int) val mean = total / count
}


trait ReadableGauge extends Gauge {
/** /**
* Arithmetic mean * Arithmetic mean
*/ */
def mean = { def mean = summary.mean
val snapshot = summary
snapshot.total / snapshot.count
}


/** /**
* Get an atomic snapshot of summary statistics * Get an atomic snapshot of summary statistics
Expand Down Expand Up @@ -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
}
}
}
} }


/** /**
Expand Down

0 comments on commit b964921

Please sign in to comment.