aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/util/DifferentialSource.scala
blob: f2c621b012d75ea1804d327db7b356f1ce81367f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package kamon.util

/**
  * Keeps track of the values produced by the source and produce the difference between the last two observed values
  * when calling get. This class assumes the source increases monotonically and any produced value that violates this
  * assumption will be dropped.
  *
  */
class DifferentialSource(source: () => Long) {
  private var previousValue = source()

  def get(): Long = synchronized {
    val currentValue = source()
    val diff = currentValue - previousValue
    previousValue = currentValue

    if(diff < 0) 0 else diff
  }
}

object DifferentialSource {
  def apply(source: () => Long): DifferentialSource =
    new DifferentialSource(source)
}