aboutsummaryrefslogtreecommitdiff
path: root/kamon-core
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2017-09-15 02:49:36 +0200
committerIvan Topolnjak <ivantopo@gmail.com>2017-09-15 02:49:36 +0200
commitead4fd7743895ffe1d34e37c23eceab575fb907e (patch)
treef40bb164931b4b1d519193b7154b4a4e4501cf49 /kamon-core
parentfdbac7aa95a2103ee823894c44e5fc2f354cbd4a (diff)
downloadKamon-ead4fd7743895ffe1d34e37c23eceab575fb907e.tar.gz
Kamon-ead4fd7743895ffe1d34e37c23eceab575fb907e.tar.bz2
Kamon-ead4fd7743895ffe1d34e37c23eceab575fb907e.zip
create new DifferentialSource utility
Diffstat (limited to 'kamon-core')
-rw-r--r--kamon-core/src/main/scala/kamon/util/DifferentialSource.scala24
1 files changed, 24 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/util/DifferentialSource.scala b/kamon-core/src/main/scala/kamon/util/DifferentialSource.scala
new file mode 100644
index 00000000..f2c621b0
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/util/DifferentialSource.scala
@@ -0,0 +1,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)
+}