diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2014-10-20 02:09:06 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2014-10-20 02:09:06 +0200 |
commit | 62d02deec6f21b62ad3c878aad4c0927b7bac1d0 (patch) | |
tree | 08a9b3a4164d95a6629b162edc84c37e79a624fb /kamon-core/src/main/scala/kamon/metric/instrument | |
parent | 290a3c92da938182fe1157efff00d4589203e678 (diff) | |
download | Kamon-62d02deec6f21b62ad3c878aad4c0927b7bac1d0.tar.gz Kamon-62d02deec6f21b62ad3c878aad4c0927b7bac1d0.tar.bz2 Kamon-62d02deec6f21b62ad3c878aad4c0927b7bac1d0.zip |
+ core: provide sum and percentile functions in Histogram.Snapshot, closes #85
Diffstat (limited to 'kamon-core/src/main/scala/kamon/metric/instrument')
-rw-r--r-- | kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala b/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala index 67db5d93..813a2a16 100644 --- a/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala +++ b/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala @@ -75,6 +75,8 @@ object Histogram { def numberOfMeasurements: Long def min: Long def max: Long + def sum: Long + def percentile(percentile: Double): Long def recordsIterator: Iterator[Record] def merge(that: Histogram.Snapshot, context: CollectionContext): Histogram.Snapshot } @@ -83,6 +85,8 @@ object Histogram { def empty(targetScale: Scale) = new Snapshot { override def min: Long = 0L override def max: Long = 0L + override def sum: Long = 0L + override def percentile(percentile: Double): Long = 0L override def recordsIterator: Iterator[Record] = Iterator.empty override def merge(that: Snapshot, context: CollectionContext): Snapshot = that override def scale: Scale = targetScale @@ -161,6 +165,22 @@ class CompactHdrSnapshot(val scale: Scale, val numberOfMeasurements: Long, compa def min: Long = if (compactRecords.length == 0) 0 else levelFromCompactRecord(compactRecords(0)) def max: Long = if (compactRecords.length == 0) 0 else levelFromCompactRecord(compactRecords(compactRecords.length - 1)) + def sum: Long = recordsIterator.foldLeft(0L)((a, r) ⇒ a + (r.count * r.level)) + + def percentile(p: Double): Long = { + val records = recordsIterator + val threshold = numberOfMeasurements * (p / 100D) + var countToCurrentLevel = 0L + var percentileLevel = 0L + + while (countToCurrentLevel < threshold && records.hasNext) { + val record = records.next() + countToCurrentLevel += record.count + percentileLevel = record.level + } + + percentileLevel + } def merge(that: Histogram.Snapshot, context: CollectionContext): Histogram.Snapshot = { if (that.isEmpty) this else if (this.isEmpty) that else { |