Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using negative values in RangeSampler#increment and RangeSampler#decrement #532

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,36 @@ class RangeSamplerSpec extends WordSpec with Matchers {
snapshot.distribution.min should be(0)
snapshot.distribution.max should be(0)
}

"report correct min and max values if increment or decrement are used with negative values" in {
val rangeSampler = buildRangeSampler("report-increment-decrement-negative")

rangeSampler.increment(5)
rangeSampler.decrement(3)
rangeSampler.sample()

val firstSnapshot = rangeSampler.snapshot()
firstSnapshot.distribution.min should be(0)
firstSnapshot.distribution.max should be(5)

rangeSampler.increment(-1)
rangeSampler.increment(2)
rangeSampler.sample()

val secondSnapshot = rangeSampler.snapshot()
secondSnapshot.distribution.min should be(1)
secondSnapshot.distribution.max should be(3)

rangeSampler.decrement(-2)
rangeSampler.decrement(2)
rangeSampler.sample()

val thirdSnapshot = rangeSampler.snapshot()
thirdSnapshot.distribution.min should be(3)
thirdSnapshot.distribution.max should be(5)
}
}

def buildRangeSampler(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): SimpleRangeSampler =
new SimpleRangeSampler(name, tags, new AtomicHdrHistogram(name, tags, unit, dynamicRange = DynamicRange.Default), Duration.ofMillis(100))
}
}
2 changes: 2 additions & 0 deletions kamon-core/src/main/scala/kamon/metric/RangeSampler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ class SimpleRangeSampler(name: String, tags: Map[String, String], underlyingHist
def increment(times: Long): Unit = {
val currentValue = sum.addAndGet(times)
max.update(currentValue)
min.update(-currentValue)
}

def decrement(): Unit =
decrement(1L)

def decrement(times: Long): Unit = {
val currentValue = sum.addAndGet(-times)
max.update(currentValue)
min.update(-currentValue)
}

Expand Down