aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2015-02-04 05:32:45 +0100
committerIvan Topolnjak <ivantopo@gmail.com>2015-02-04 05:32:45 +0100
commit82a110b23ca57286e4e3dd0315c20ed99b5e8f88 (patch)
tree830dc660c5597707407ac7c280f332f7dd4c9ede
parent1b39bfb30e338c3d6c8284ae1276fb0c7eae7cc7 (diff)
downloadKamon-82a110b23ca57286e4e3dd0315c20ed99b5e8f88.tar.gz
Kamon-82a110b23ca57286e4e3dd0315c20ed99b5e8f88.tar.bz2
Kamon-82a110b23ca57286e4e3dd0315c20ed99b5e8f88.zip
! core: revised semantics of .register on Metrics module to match our docs.
-rw-r--r--kamon-core/src/main/scala/kamon/metric/EntityRecorder.scala2
-rw-r--r--kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala10
-rw-r--r--kamon-core/src/main/scala/kamon/util/TriemapAtomicGetOrElseUpdate.scala14
3 files changed, 21 insertions, 5 deletions
diff --git a/kamon-core/src/main/scala/kamon/metric/EntityRecorder.scala b/kamon-core/src/main/scala/kamon/metric/EntityRecorder.scala
index a7bba2e1..6e0a4248 100644
--- a/kamon-core/src/main/scala/kamon/metric/EntityRecorder.scala
+++ b/kamon-core/src/main/scala/kamon/metric/EntityRecorder.scala
@@ -38,7 +38,7 @@ abstract class GenericEntityRecorder(instrumentFactory: InstrumentFactory) exten
private val _instruments = TrieMap.empty[MetricKey, Instrument]
private def register[T <: Instrument](key: MetricKey, instrument: ⇒ T): T =
- _instruments.atomicGetOrElseUpdate(key, instrument).asInstanceOf[T]
+ _instruments.atomicGetOrElseUpdate(key, instrument, _.cleanup).asInstanceOf[T]
protected def histogram(name: String): Histogram =
register(HistogramKey(name), instrumentFactory.createHistogram(name))
diff --git a/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala b/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala
index c14d4e16..88352e21 100644
--- a/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala
+++ b/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala
@@ -86,14 +86,18 @@ class MetricsExtensionImpl(system: ExtendedActorSystem) extends MetricsExtension
if (shouldTrack(entity)) {
val instrumentFactory = settings.instrumentFactories.get(recorderFactory.category).getOrElse(settings.defaultInstrumentFactory)
- val recorder = _trackedEntities.atomicGetOrElseUpdate(entity, recorderFactory.createRecorder(instrumentFactory)).asInstanceOf[T]
+ val recorder = _trackedEntities.atomicGetOrElseUpdate(entity, recorderFactory.createRecorder(instrumentFactory), _.cleanup).asInstanceOf[T]
+
Some(EntityRegistration(entity, recorder))
} else None
}
def register[T <: EntityRecorder](entity: Entity, recorder: T): EntityRegistration[T] = {
- import TriemapAtomicGetOrElseUpdate.Syntax
- EntityRegistration(entity, _trackedEntities.atomicGetOrElseUpdate(entity, recorder).asInstanceOf[T])
+ _trackedEntities.put(entity, recorder).map { oldRecorder ⇒
+ oldRecorder.cleanup
+ }
+
+ EntityRegistration(entity, recorder)
}
def unregister(entity: Entity): Unit =
diff --git a/kamon-core/src/main/scala/kamon/util/TriemapAtomicGetOrElseUpdate.scala b/kamon-core/src/main/scala/kamon/util/TriemapAtomicGetOrElseUpdate.scala
index 5ad43c34..d1197a5a 100644
--- a/kamon-core/src/main/scala/kamon/util/TriemapAtomicGetOrElseUpdate.scala
+++ b/kamon-core/src/main/scala/kamon/util/TriemapAtomicGetOrElseUpdate.scala
@@ -24,10 +24,22 @@ object TriemapAtomicGetOrElseUpdate {
* why this is necessary can be found at [[https://issues.scala-lang.org/browse/SI-7943]].
*/
implicit class Syntax[K, V](val trieMap: TrieMap[K, V]) extends AnyVal {
+
def atomicGetOrElseUpdate(key: K, op: ⇒ V): V =
+ atomicGetOrElseUpdate(key, op, { v: V ⇒ Unit })
+
+ def atomicGetOrElseUpdate(key: K, op: ⇒ V, cleanup: V ⇒ Unit): V =
trieMap.get(key) match {
case Some(v) ⇒ v
- case None ⇒ val d = op; trieMap.putIfAbsent(key, d).getOrElse(d)
+ case None ⇒
+ val d = op
+ trieMap.putIfAbsent(key, d).map { oldValue ⇒
+ // If there was an old value then `d` was never added
+ // and thus need to be cleanup.
+ cleanup(d)
+ oldValue
+
+ } getOrElse (d)
}
}
}