aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2017-06-11 10:02:22 +0200
committerIvan Topolnjak <ivantopo@gmail.com>2017-06-11 10:02:22 +0200
commite8d3e612dcf0fa396a25920a23f108f6ab8c2e61 (patch)
treee1d702aa2eabbfabc9690a5cdc3ca6ac59ae69b9 /kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala
parentde3e823cec6ec12d551f568b73d2ad1061944222 (diff)
downloadKamon-e8d3e612dcf0fa396a25920a23f108f6ab8c2e61.tar.gz
Kamon-e8d3e612dcf0fa396a25920a23f108f6ab8c2e61.tar.bz2
Kamon-e8d3e612dcf0fa396a25920a23f108f6ab8c2e61.zip
separate metrics from instruments and add default instrument for metrics
Diffstat (limited to 'kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala')
-rw-r--r--kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala61
1 files changed, 25 insertions, 36 deletions
diff --git a/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala b/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala
index 3c2bc131..e47df88e 100644
--- a/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala
+++ b/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala
@@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicReference
import com.typesafe.config.Config
import com.typesafe.scalalogging.Logger
+import kamon.metric.InstrumentFactory.{InstrumentType, InstrumentTypes}
import kamon.util.MeasurementUnit
import scala.collection.concurrent.TrieMap
@@ -28,25 +29,27 @@ import scala.concurrent.duration.Duration
class MetricRegistry(initialConfig: Config) extends MetricsSnapshotGenerator {
private val logger = Logger(classOf[MetricRegistry])
- private val metrics = TrieMap.empty[String, MetricEntry]
private val instrumentFactory = new AtomicReference[InstrumentFactory]()
+ private val metrics = TrieMap.empty[String, BaseMetric[_, _]]
+
reconfigure(initialConfig)
def reconfigure(config: Config): Unit = synchronized {
instrumentFactory.set(InstrumentFactory.fromConfig(config))
}
- def histogram(name: String, unit: MeasurementUnit, tags: Map[String, String], dynamicRange: Option[DynamicRange]): Histogram =
- lookupInstrument(name, unit, tags, InstrumentTypes.Histogram, instrumentFactory.get().buildHistogram(dynamicRange))
- def counter(name: String, unit: MeasurementUnit, tags: Map[String, String]): Counter =
- lookupInstrument(name, unit, tags, InstrumentTypes.Counter, instrumentFactory.get().buildCounter)
+ def histogram(name: String, unit: MeasurementUnit, dynamicRange: Option[DynamicRange]): HistogramMetric =
+ lookupMetric(name, unit, InstrumentTypes.Histogram)(new HistogramMetricImpl(name, unit, dynamicRange, instrumentFactory))
+
+ def counter(name: String, unit: MeasurementUnit): CounterMetric =
+ lookupMetric(name, unit, InstrumentTypes.Counter)(new CounterMetricImpl(name, unit, instrumentFactory))
- def gauge(name: String, unit: MeasurementUnit, tags: Map[String, String]): Gauge =
- lookupInstrument(name, unit, tags, InstrumentTypes.Gauge, instrumentFactory.get().buildGauge)
+ def gauge(name: String, unit: MeasurementUnit): GaugeMetric =
+ lookupMetric(name, unit, InstrumentTypes.Gauge)(new GaugeMetricImpl(name, unit, instrumentFactory))
- def minMaxCounter(name: String, unit: MeasurementUnit, tags: Map[String, String], dynamicRange: Option[DynamicRange], sampleInterval: Option[Duration]): MinMaxCounter =
- lookupInstrument(name, unit, tags, InstrumentTypes.MinMaxCounter, instrumentFactory.get().buildMinMaxCounter(dynamicRange, sampleInterval))
+ def minMaxCounter(name: String, unit: MeasurementUnit, dynamicRange: Option[DynamicRange], sampleInterval: Option[Duration]): MinMaxCounterMetric =
+ lookupMetric(name, unit, InstrumentTypes.MinMaxCounter)(new MinMaxCounterMetricImpl(name, unit, dynamicRange, sampleInterval, instrumentFactory))
override def snapshot(): MetricsSnapshot = synchronized {
@@ -55,15 +58,12 @@ class MetricRegistry(initialConfig: Config) extends MetricsSnapshotGenerator {
var counters = Seq.empty[MetricValue]
var gauges = Seq.empty[MetricValue]
- for {
- metricEntry <- metrics.values
- instrument <- metricEntry.instruments.values
- } {
+ for(metricEntry <- metrics.values) {
metricEntry.instrumentType match {
- case InstrumentTypes.Histogram => histograms = histograms :+ instrument.asInstanceOf[SnapshotableHistogram].snapshot()
- case InstrumentTypes.MinMaxCounter => mmCounters = mmCounters :+ instrument.asInstanceOf[SnapshotableMinMaxCounter].snapshot()
- case InstrumentTypes.Gauge => gauges = gauges :+ instrument.asInstanceOf[SnapshotableGauge].snapshot()
- case InstrumentTypes.Counter => counters = counters :+ instrument.asInstanceOf[SnapshotableCounter].snapshot()
+ case InstrumentTypes.Histogram => histograms = histograms ++ metricEntry.snapshot().asInstanceOf[Seq[MetricDistribution]]
+ case InstrumentTypes.MinMaxCounter => mmCounters = mmCounters ++ metricEntry.snapshot().asInstanceOf[Seq[MetricDistribution]]
+ case InstrumentTypes.Gauge => gauges = gauges ++ metricEntry.snapshot().asInstanceOf[Seq[MetricValue]]
+ case InstrumentTypes.Counter => counters = counters ++ metricEntry.snapshot().asInstanceOf[Seq[MetricValue]]
case other => logger.warn("Unexpected instrument type [{}] found in the registry", other )
}
}
@@ -71,29 +71,18 @@ class MetricRegistry(initialConfig: Config) extends MetricsSnapshotGenerator {
MetricsSnapshot(histograms, mmCounters, gauges, counters)
}
- private def lookupInstrument[T](name: String, measurementUnit: MeasurementUnit, tags: Map[String, String],
- instrumentType: InstrumentType, builder: (String, Map[String, String], MeasurementUnit) => T): T = {
+ private def lookupMetric[T <: BaseMetric[_, _]](name: String, unit: MeasurementUnit, instrumentType: InstrumentType)(metricBuilder: => T): T = {
+ val metric = metrics.atomicGetOrElseUpdate(name, metricBuilder)
- val entry = metrics.atomicGetOrElseUpdate(name, MetricEntry(instrumentType, measurementUnit, TrieMap.empty))
- if(entry.instrumentType != instrumentType)
- sys.error(s"Tried to use metric [$name] as a [${instrumentType.name}] but it is already defined as [${entry.instrumentType.name}] ")
+ if(metric.instrumentType != instrumentType)
+ sys.error(s"Cannot define metric [$name] as a [${instrumentType.name}], it is already defined as [${metric.instrumentType.name}] ")
- if(entry.unit != measurementUnit)
- logger.warn("Ignoring attempt to use measurement unit [{}] on metric [name={}, tags={}], the metric uses [{}]",
- measurementUnit.magnitude.name, name, tags.prettyPrint(), entry.unit.magnitude.name)
+ if(metric.unit != unit)
+ logger.warn("Ignoring attempt to register measurement unit [{}] on metric [{}], the metric uses already uses [{}]",
+ unit.magnitude.name, name, metric.unit.magnitude.name)
- entry.instruments.getOrElseUpdate(tags, builder(name, tags, measurementUnit)).asInstanceOf[T]
+ metric.asInstanceOf[T]
}
-
- private case class InstrumentType(name: String)
- private object InstrumentTypes {
- val Histogram = InstrumentType("Histogram")
- val MinMaxCounter = InstrumentType("MinMaxCounter")
- val Counter = InstrumentType("Counter")
- val Gauge = InstrumentType("Gauge")
- }
-
- private case class MetricEntry(instrumentType: InstrumentType, unit: MeasurementUnit, instruments: TrieMap[Map[String, String], Any])
}
trait MetricsSnapshotGenerator {