1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/* =========================================================================================
* Copyright © 2013-2017 the kamon project <http://kamon.io/>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
* =========================================================================================
*/
package kamon
package metric
import java.util.concurrent.atomic.AtomicReference
import com.typesafe.config.Config
import kamon.metric.InstrumentFactory.{InstrumentType, InstrumentTypes}
import kamon.util.MeasurementUnit
import kamon.util.MeasurementUnit.time
import scala.collection.concurrent.TrieMap
import java.time.Duration
import java.util.concurrent.ScheduledExecutorService
import org.slf4j.LoggerFactory
class MetricRegistry(initialConfig: Config, scheduler: ScheduledExecutorService) extends MetricsSnapshotGenerator {
private val logger = LoggerFactory.getLogger(classOf[MetricRegistry])
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, 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): GaugeMetric =
lookupMetric(name, unit, InstrumentTypes.Gauge)(new GaugeMetricImpl(name, unit, instrumentFactory))
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, scheduler))
def timer(name: String, dynamicRange: Option[DynamicRange]): TimerMetric =
new TimerMetricImpl(histogram(name, time.nanoseconds, dynamicRange))
override def snapshot(): MetricsSnapshot = synchronized {
var histograms = Seq.empty[MetricDistribution]
var mmCounters = Seq.empty[MetricDistribution]
var counters = Seq.empty[MetricValue]
var gauges = Seq.empty[MetricValue]
for(metricEntry <- metrics.values) {
metricEntry.instrumentType match {
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 )
}
}
MetricsSnapshot(histograms, mmCounters, gauges, counters)
}
private def lookupMetric[T <: BaseMetric[_, _]](name: String, unit: MeasurementUnit, instrumentType: InstrumentType)(metricBuilder: => T): T = {
val metric = metrics.atomicGetOrElseUpdate(name, metricBuilder)
if(metric.instrumentType != instrumentType)
sys.error(s"Cannot define metric [$name] as a [${instrumentType.name}], it is already defined as [${metric.instrumentType.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)
metric.asInstanceOf[T]
}
}
trait MetricsSnapshotGenerator {
def snapshot(): MetricsSnapshot
}
|