From 7c68d6324cfca0e88c1cd49e9c65b69f68edf55a Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Tue, 8 Jul 2014 23:06:42 -0300 Subject: + core: create the new KamonStandalone helper, closes #47 --- .../src/main/scala/kamon/metric/UserMetrics.scala | 24 ++++++++++ .../scala/kamon/standalone/KamonStandalone.scala | 54 +++++++++++++++++++++- .../test/scala/kamon/metric/UserMetricsSpec.scala | 23 +++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) (limited to 'kamon-core') diff --git a/kamon-core/src/main/scala/kamon/metric/UserMetrics.scala b/kamon-core/src/main/scala/kamon/metric/UserMetrics.scala index dea03968..f3803d37 100644 --- a/kamon-core/src/main/scala/kamon/metric/UserMetrics.scala +++ b/kamon-core/src/main/scala/kamon/metric/UserMetrics.scala @@ -35,6 +35,18 @@ class UserMetricsExtension(system: ExtendedActorSystem) extends Kamon.Extension def registerGauge(name: String, precision: Histogram.Precision, highestTrackableValue: Long, refreshInterval: FiniteDuration)(currentValueCollector: Gauge.CurrentValueCollector): Gauge = userMetricsRecorder.buildGauge(name, precision, highestTrackableValue, refreshInterval, currentValueCollector) + + def removeHistogram(name: String): Unit = + userMetricsRecorder.removeHistogram(name) + + def removeCounter(name: String): Unit = + userMetricsRecorder.removeCounter(name) + + def removeMinMaxCounter(name: String): Unit = + userMetricsRecorder.removeMinMaxCounter(name) + + def removeGauge(name: String): Unit = + userMetricsRecorder.removeGauge(name) } object UserMetrics extends ExtensionId[UserMetricsExtension] with ExtensionIdProvider with MetricGroupIdentity { @@ -86,6 +98,18 @@ object UserMetrics extends ExtensionId[UserMetricsExtension] with ExtensionIdPro def buildGauge(name: String)(currentValueCollector: Gauge.CurrentValueCollector): Gauge = gauges.getOrElseUpdate(name, Gauge.fromConfig(defaultGaugePrecisionConfig, system)(currentValueCollector)) + def removeHistogram(name: String): Unit = + histograms.remove(name) + + def removeCounter(name: String): Unit = + counters.remove(name) + + def removeMinMaxCounter(name: String): Unit = + minMaxCounters.remove(name).map(_.cleanup) + + def removeGauge(name: String): Unit = + gauges.remove(name).map(_.cleanup) + def collect(context: CollectionContext): UserMetricsSnapshot = { val histogramSnapshots = histograms.map { case (name, histogram) ⇒ diff --git a/kamon-core/src/main/scala/kamon/standalone/KamonStandalone.scala b/kamon-core/src/main/scala/kamon/standalone/KamonStandalone.scala index 258cc1b2..490bc127 100644 --- a/kamon-core/src/main/scala/kamon/standalone/KamonStandalone.scala +++ b/kamon-core/src/main/scala/kamon/standalone/KamonStandalone.scala @@ -1,11 +1,61 @@ package kamon.standalone import akka.actor.ActorSystem +import com.typesafe.config.Config +import kamon.Kamon +import kamon.metric.UserMetrics +import kamon.metric.instrument.{ Gauge, MinMaxCounter, Counter, Histogram } + +import scala.concurrent.duration.FiniteDuration + +trait KamonStandalone { + private[kamon] def system: ActorSystem + + def registerHistogram(name: String, precision: Histogram.Precision, highestTrackableValue: Long): Histogram = + Kamon(UserMetrics)(system).registerHistogram(name, precision, highestTrackableValue) + + def registerHistogram(name: String): Histogram = + Kamon(UserMetrics)(system).registerHistogram(name) + + def registerCounter(name: String): Counter = + Kamon(UserMetrics)(system).registerCounter(name) + + def registerMinMaxCounter(name: String, precision: Histogram.Precision, highestTrackableValue: Long, + refreshInterval: FiniteDuration): MinMaxCounter = + Kamon(UserMetrics)(system).registerMinMaxCounter(name, precision, highestTrackableValue, refreshInterval) + + def registerMinMaxCounter(name: String): MinMaxCounter = + Kamon(UserMetrics)(system).registerMinMaxCounter(name) + + def registerGauge(name: String)(currentValueCollector: Gauge.CurrentValueCollector): Gauge = + Kamon(UserMetrics)(system).registerGauge(name)(currentValueCollector) + + def registerGauge(name: String, precision: Histogram.Precision, highestTrackableValue: Long, + refreshInterval: FiniteDuration)(currentValueCollector: Gauge.CurrentValueCollector): Gauge = + Kamon(UserMetrics)(system).registerGauge(name, precision, highestTrackableValue, refreshInterval)(currentValueCollector) + + def removeHistogram(name: String): Unit = + Kamon(UserMetrics)(system).removeHistogram(name) + + def removeCounter(name: String): Unit = + Kamon(UserMetrics)(system).removeCounter(name) + + def removeMinMaxCounter(name: String): Unit = + Kamon(UserMetrics)(system).removeMinMaxCounter(name) + + def removeGauge(name: String): Unit = + Kamon(UserMetrics)(system).removeGauge(name) +} object KamonStandalone { - private lazy val system = ActorSystem("kamon-standalone") - def registerHistogram(name: String) = { + def buildFromConfig(config: Config): KamonStandalone = buildFromConfig(config, "kamon-standalone") + def buildFromConfig(config: Config, actorSystemName: String): KamonStandalone = new KamonStandalone { + val system: ActorSystem = ActorSystem(actorSystemName, config) } } + +object EmbeddedKamonStandalone extends KamonStandalone { + private[kamon] lazy val system = ActorSystem("kamon-standalone") +} \ No newline at end of file diff --git a/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala b/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala index 1790dc51..385c8404 100644 --- a/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala @@ -112,6 +112,29 @@ class UserMetricsSpec extends TestKitBase with WordSpecLike with Matchers with I } } + "allow unregistering metrics from the extension" in { + val userMetricsRecorder = Kamon(Metrics).register(UserMetrics, UserMetrics.Factory).get + val counter = Kamon(UserMetrics).registerCounter("counter-for-remove") + val histogram = Kamon(UserMetrics).registerHistogram("histogram-for-remove") + val minMaxCounter = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-for-remove") + val gauge = Kamon(UserMetrics).registerGauge("gauge-for-remove") { () ⇒ 2L } + + userMetricsRecorder.counters.keys should contain("counter-for-remove") + userMetricsRecorder.histograms.keys should contain("histogram-for-remove") + userMetricsRecorder.minMaxCounters.keys should contain("min-max-counter-for-remove") + userMetricsRecorder.gauges.keys should contain("gauge-for-remove") + + Kamon(UserMetrics).removeCounter("counter-for-remove") + Kamon(UserMetrics).removeHistogram("histogram-for-remove") + Kamon(UserMetrics).removeMinMaxCounter("min-max-counter-for-remove") + Kamon(UserMetrics).removeGauge("gauge-for-remove") + + userMetricsRecorder.counters.keys should not contain ("counter-for-remove") + userMetricsRecorder.histograms.keys should not contain ("histogram-for-remove") + userMetricsRecorder.minMaxCounters.keys should not contain ("min-max-counter-for-remove") + userMetricsRecorder.gauges.keys should not contain ("gauge-for-remove") + } + "generate a snapshot containing all the registered user metrics and reset all instruments" in { val context = Kamon(Metrics).buildDefaultCollectionContext val userMetricsRecorder = Kamon(Metrics).register(UserMetrics, UserMetrics.Factory).get -- cgit v1.2.3