Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #13 from mladens/maintenance-1.x
Browse files Browse the repository at this point in the history
Update Gauge transport format to one resembling histogram
  • Loading branch information
ivantopo authored Nov 26, 2019
2 parents 7016a32 + b7ad337 commit b694f56
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.9
sbt.version=1.2.8
5 changes: 3 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lazy val root: Project = project.in(file(".")).dependsOn(latestSbtUmbrella)
lazy val latestSbtUmbrella = uri("git://github.com/kamon-io/kamon-sbt-umbrella.git")
lazy val umbrella: ProjectRef = ProjectRef(uri("git://github.com/kamon-io/kamon-sbt-umbrella.git"), "kamon-sbt-umbrella")
lazy val root: Project = project.in(file(".")).dependsOn(umbrella)

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")

36 changes: 36 additions & 0 deletions src/main/scala/kamon/apm/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,40 @@ package object apm {
def tracingRoute = s"$ingestionApi/tracing/ingest"
}



/*
* Internal HDR Histogram state required to convert index to values and get bucket size information. These values
* correspond to a histogram configured to have 2 significant value digits prevision and a smallest discernible value
* of 1.
*/
def countsArrayIndex(value: Long): Int = {

val SubBucketHalfCountMagnitude = 7
val SubBucketHalfCount = 128
val UnitMagnitude = 0
val SubBucketCount = Math.pow(2, SubBucketHalfCountMagnitude + 1).toInt
val LeadingZeroCountBase = 64 - UnitMagnitude - SubBucketHalfCountMagnitude - 1
val SubBucketMask = (SubBucketCount.toLong - 1) << UnitMagnitude

def countsArrayIndex(bucketIndex: Int, subBucketIndex: Int): Int = {
assert(subBucketIndex < SubBucketCount)
assert(bucketIndex == 0 || (subBucketIndex >= SubBucketHalfCount))
val bucketBaseIndex = (bucketIndex + 1) << SubBucketHalfCountMagnitude
val offsetInBucket = subBucketIndex - SubBucketHalfCount
bucketBaseIndex + offsetInBucket
}

def getBucketIndex(value: Long): Int =
LeadingZeroCountBase - java.lang.Long.numberOfLeadingZeros(value | SubBucketMask)

def getSubBucketIndex(value: Long, bucketIndex: Long): Int =
Math.floor(value / Math.pow(2, (bucketIndex + UnitMagnitude))).toInt

if (value < 0) throw new ArrayIndexOutOfBoundsException("Histogram recorded value cannot be negative.")
val bucketIndex = getBucketIndex(value)
val subBucketIndex = getSubBucketIndex(value, bucketIndex)
countsArrayIndex(bucketIndex, subBucketIndex)
}

}
13 changes: 11 additions & 2 deletions src/main/scala/kamon/apm/reporters/KamonApmMetric.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package kamon.apm.reporters
package kamon.apm
package reporters

import java.nio.ByteBuffer
import java.time.Duration
Expand Down Expand Up @@ -120,7 +121,15 @@ private[apm] class KamonApmMetric(codeProvidedPlan: Option[Plan]) extends Metric

private def toIngestionMetricValue(metricType: InstrumentType)(metric: MetricValue): IngestionV1.Metric = {
valueBuffer.clear()
ZigZag.putLong(valueBuffer, metricScaler.scaleMetricValue(metric).value)
metricType match {
case COUNTER =>
ZigZag.putLong(valueBuffer, metricScaler.scaleMetricValue(metric).value)
case GAUGE =>
val offset = countsArrayIndex(metricScaler.scaleMetricValue(metric).value)
if(offset > 0) ZigZag.putLong(valueBuffer, -offset)
ZigZag.putLong(valueBuffer, 1)
}

valueBuffer.flip()

IngestionV1.Metric.newBuilder()
Expand Down

0 comments on commit b694f56

Please sign in to comment.