From eeed5e1614eb0435163506a8a740142b71cbe1f5 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Mon, 11 Jun 2018 11:18:07 +0200 Subject: ensure that histograms snapshots are immutable --- kamon-core/src/main/scala/kamon/metric/Histogram.scala | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kamon-core/src/main/scala/kamon/metric/Histogram.scala b/kamon-core/src/main/scala/kamon/metric/Histogram.scala index 592dae7d..f06e58c4 100644 --- a/kamon-core/src/main/scala/kamon/metric/Histogram.scala +++ b/kamon-core/src/main/scala/kamon/metric/Histogram.scala @@ -117,7 +117,7 @@ private[kamon] trait SnapshotCreation { val zigZagCounts = Array.ofDim[Byte](buffer.limit()) buffer.get(zigZagCounts) - val distribution = new ZigZagCountsDistribution(totalCount, minIndex, maxIndex, ByteBuffer.wrap(zigZagCounts), + val distribution = new ZigZagCountsDistribution(totalCount, minIndex, maxIndex, ByteBuffer.wrap(zigZagCounts).asReadOnlyBuffer(), protectedUnitMagnitude(), protectedSubBucketHalfCount(), protectedSubBucketHalfCountMagnitude()) MetricDistribution(name, tags, unit, dynamicRange, distribution) @@ -130,7 +130,7 @@ private[kamon] object SnapshotCreation { override def initialValue(): ByteBuffer = ByteBuffer.allocate(33792) } - class ZigZagCountsDistribution(val count: Long, minIndex: Int, maxIndex: Int, val zigZagCounts: ByteBuffer, + class ZigZagCountsDistribution(val count: Long, minIndex: Int, maxIndex: Int, zigZagCounts: ByteBuffer, unitMagnitude: Int, subBucketHalfCount: Int, subBucketHalfCountMagnitude: Int) extends Distribution { val min: Long = if(count == 0) 0 else bucketValueAtIndex(minIndex) @@ -211,6 +211,10 @@ private[kamon] object SnapshotCreation { builder.result() } + def countsArray(): ByteBuffer = { + zigZagCounts.duplicate() + } + @inline private def bucketValueAtIndex(index: Int): Long = { var bucketIndex: Int = (index >> subBucketHalfCountMagnitude) - 1 var subBucketIndex: Int = (index & (subBucketHalfCount - 1)) + subBucketHalfCount -- cgit v1.2.3