Skip to content

Commit

Permalink
Merge pull request #538 from ivantopo/ensure-snapshot-immutability
Browse files Browse the repository at this point in the history
Ensure that histograms snapshots are immutable
  • Loading branch information
ivantopo authored Jun 17, 2018
2 parents b03c554 + eeed5e1 commit 145a601
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions kamon-core/src/main/scala/kamon/metric/Histogram.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 145a601

Please sign in to comment.