Skip to content

Commit 7bf8705

Browse files
authored
Merge pull request #1017 from SimunKaracic/add-reset-to-range-sampler
Add resetDistribution method to RangeSampler
2 parents e668e82 + 7538a1a commit 7bf8705

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

build.sbt

-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ def akkaHttpVersion(scalaVersion: String) = scalaVersion match {
426426
case _ => "10.2.3"
427427
}
428428

429-
430429
lazy val `kamon-akka-http` = (project in file("instrumentation/kamon-akka-http"))
431430
.enablePlugins(JavaAgent)
432431
.disablePlugins(AssemblyPlugin)

core/kamon-core-tests/src/test/scala/kamon/metric/RangeSamplerSpec.scala

+24-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
package kamon.metric
1818

1919
import java.time.Duration
20-
2120
import kamon.Kamon
2221
import kamon.testkit.InstrumentInspection
2322
import org.scalatest.{Matchers, WordSpec}
2423

24+
import scala.concurrent.duration.DurationInt
25+
2526
class RangeSamplerSpec extends WordSpec with Matchers with InstrumentInspection.Syntax {
2627

2728
"a RangeSampler" should {
@@ -84,6 +85,7 @@ class RangeSamplerSpec extends WordSpec with Matchers with InstrumentInspection.
8485
MeasurementUnit.none,
8586
Duration.ofMillis(1)
8687
).withoutTags()
88+
8789
rangeSampler.increment()
8890
rangeSampler.increment(3)
8991
rangeSampler.increment()
@@ -94,5 +96,25 @@ class RangeSamplerSpec extends WordSpec with Matchers with InstrumentInspection.
9496
snapshot.min should be(0)
9597
snapshot.max should be(5)
9698
}
99+
100+
"reset values to 0 after calling resetDistribution" in {
101+
val rangeSampler = Kamon.rangeSampler(
102+
"auto-update2",
103+
MeasurementUnit.none,
104+
Duration.ofMillis(1)
105+
).withoutTags()
106+
107+
rangeSampler.increment(5)
108+
rangeSampler.resetDistribution()
109+
110+
Thread.sleep(50)
111+
rangeSampler.resetDistribution()
112+
113+
val snapshot = rangeSampler.distribution()
114+
115+
snapshot.min should be(0)
116+
snapshot.max should be(0)
117+
snapshot.sum should be(0)
118+
}
97119
}
98-
}
120+
}

core/kamon-core/src/main/scala/kamon/metric/RangeSampler.scala

+21-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ trait RangeSampler extends Instrument[RangeSampler, Metric.Settings.ForDistribut
6565
*/
6666
def sample(): RangeSampler
6767

68+
/**
69+
* Resets the minimum, maximum and current value indicators.
70+
*/
71+
def resetDistribution(): RangeSampler
72+
6873
}
6974

7075

@@ -84,7 +89,7 @@ object RangeSampler {
8489

8590
private val _min = new AtomicLongMaxUpdater(new AtomicLong(0L))
8691
private val _max = new AtomicLongMaxUpdater(new AtomicLong(0L))
87-
private val _sum = new AtomicLong()
92+
private val _current = new AtomicLong()
8893

8994
override def increment(): RangeSampler =
9095
increment(1)
@@ -93,13 +98,20 @@ object RangeSampler {
9398
decrement(1)
9499

95100
override def increment(times: Long): RangeSampler = {
96-
val currentValue = _sum.addAndGet(times)
101+
val currentValue = _current.addAndGet(times)
97102
_max.update(currentValue)
98103
this
99104
}
100105

106+
override def resetDistribution(): RangeSampler = {
107+
_min.maxThenReset(0)
108+
_max.maxThenReset(0)
109+
_current.set(0)
110+
this
111+
}
112+
101113
override def decrement(times: Long): RangeSampler = {
102-
val currentValue = _sum.addAndGet(-times)
114+
val currentValue = _current.addAndGet(-times)
103115
_min.update(-currentValue)
104116
this
105117
}
@@ -112,7 +124,7 @@ object RangeSampler {
112124
override def sample(): RangeSampler = {
113125
try {
114126
val currentValue = {
115-
val value = _sum.get()
127+
val value = _current.get()
116128
if (value <= 0) 0 else value
117129
}
118130

@@ -153,7 +165,11 @@ object RangeSampler {
153165
def update(newMax: Long):Unit = {
154166
@tailrec def compare(): Long = {
155167
val currentMax = value.get()
156-
if(newMax > currentMax) if (!value.compareAndSet(currentMax, newMax)) compare() else newMax
168+
if(newMax > currentMax) {
169+
if (!value.compareAndSet(currentMax, newMax)) {
170+
compare()
171+
} else newMax
172+
}
157173
else currentMax
158174
}
159175
compare()

0 commit comments

Comments
 (0)