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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
* =========================================================================================
* Copyright © 2013 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.metric
import akka.actor._
import com.typesafe.config.Config
import kamon.metric.SubscriptionsDispatcher.{ Subscribe, Unsubscribe }
import kamon.metric.instrument.Gauge.CurrentValueCollector
import kamon.metric.instrument.Histogram.DynamicRange
import kamon.metric.instrument._
import kamon.util.LazyActorRef
import scala.collection.concurrent.TrieMap
import scala.concurrent.duration.FiniteDuration
case class EntityRegistration[T <: EntityRecorder](entity: Entity, recorder: T)
trait Metrics {
def settings: MetricsSettings
def shouldTrack(entity: Entity): Boolean
def shouldTrack(entityName: String, category: String): Boolean =
shouldTrack(Entity(entityName, category))
//
// Histograms registration and removal
//
def histogram(name: String): Histogram =
registerHistogram(name)
def histogram(name: String, unitOfMeasurement: UnitOfMeasurement): Histogram =
registerHistogram(name, unitOfMeasurement = Some(unitOfMeasurement))
def histogram(name: String, dynamicRange: DynamicRange): Histogram =
registerHistogram(name, dynamicRange = Some(dynamicRange))
def histogram(name: String, unitOfMeasurement: UnitOfMeasurement, dynamicRange: DynamicRange): Histogram =
registerHistogram(name, unitOfMeasurement = Some(unitOfMeasurement), dynamicRange = Some(dynamicRange))
def histogram(name: String, tags: Map[String, String]): Histogram =
registerHistogram(name, tags)
def histogram(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement): Histogram =
registerHistogram(name, tags, Some(unitOfMeasurement))
def histogram(name: String, tags: Map[String, String], dynamicRange: DynamicRange): Histogram =
registerHistogram(name, tags, dynamicRange = Some(dynamicRange))
def histogram(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement, dynamicRange: DynamicRange): Histogram =
registerHistogram(name, tags, Some(unitOfMeasurement), Some(dynamicRange))
def removeHistogram(name: String): Boolean =
removeHistogram(name, Map.empty)
def registerHistogram(name: String, tags: Map[String, String] = Map.empty, unitOfMeasurement: Option[UnitOfMeasurement] = None,
dynamicRange: Option[DynamicRange] = None): Histogram
def removeHistogram(name: String, tags: Map[String, String]): Boolean
//
// MinMaxCounter registration and removal
//
def minMaxCounter(name: String): MinMaxCounter =
registerMinMaxCounter(name)
def minMaxCounter(name: String, unitOfMeasurement: UnitOfMeasurement): MinMaxCounter =
registerMinMaxCounter(name, unitOfMeasurement = Some(unitOfMeasurement))
def minMaxCounter(name: String, dynamicRange: DynamicRange): MinMaxCounter =
registerMinMaxCounter(name, dynamicRange = Some(dynamicRange))
def minMaxCounter(name: String, refreshInterval: FiniteDuration): MinMaxCounter =
registerMinMaxCounter(name, refreshInterval = Some(refreshInterval))
def minMaxCounter(name: String, dynamicRange: DynamicRange, refreshInterval: FiniteDuration): MinMaxCounter =
registerMinMaxCounter(name, dynamicRange = Some(dynamicRange), refreshInterval = Some(refreshInterval))
def minMaxCounter(name: String, unitOfMeasurement: UnitOfMeasurement, dynamicRange: DynamicRange): MinMaxCounter =
registerMinMaxCounter(name, unitOfMeasurement = Some(unitOfMeasurement), dynamicRange = Some(dynamicRange))
def minMaxCounter(name: String, tags: Map[String, String]): MinMaxCounter =
registerMinMaxCounter(name, tags)
def minMaxCounter(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement): MinMaxCounter =
registerMinMaxCounter(name, tags, Some(unitOfMeasurement))
def minMaxCounter(name: String, tags: Map[String, String], dynamicRange: DynamicRange): MinMaxCounter =
registerMinMaxCounter(name, tags, dynamicRange = Some(dynamicRange))
def minMaxCounter(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement, dynamicRange: DynamicRange): MinMaxCounter =
registerMinMaxCounter(name, tags, Some(unitOfMeasurement), Some(dynamicRange))
def removeMinMaxCounter(name: String): Boolean =
removeMinMaxCounter(name, Map.empty)
def removeMinMaxCounter(name: String, tags: Map[String, String]): Boolean
def registerMinMaxCounter(name: String, tags: Map[String, String] = Map.empty, unitOfMeasurement: Option[UnitOfMeasurement] = None,
dynamicRange: Option[DynamicRange] = None, refreshInterval: Option[FiniteDuration] = None): MinMaxCounter
//
// Gauge registration and removal
//
def gauge(name: String)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector)
def gauge(name: String, unitOfMeasurement: UnitOfMeasurement)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, unitOfMeasurement = Some(unitOfMeasurement))
def gauge(name: String, dynamicRange: DynamicRange)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, dynamicRange = Some(dynamicRange))
def gauge(name: String, refreshInterval: FiniteDuration)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, refreshInterval = Some(refreshInterval))
def gauge(name: String, dynamicRange: DynamicRange, refreshInterval: FiniteDuration)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, dynamicRange = Some(dynamicRange), refreshInterval = Some(refreshInterval))
def gauge(name: String, unitOfMeasurement: UnitOfMeasurement, dynamicRange: DynamicRange)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, unitOfMeasurement = Some(unitOfMeasurement), dynamicRange = Some(dynamicRange))
def gauge(name: String, tags: Map[String, String])(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, tags)
def gauge(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, tags, Some(unitOfMeasurement))
def gauge(name: String, tags: Map[String, String], dynamicRange: DynamicRange)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, tags, dynamicRange = Some(dynamicRange))
def gauge(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement, dynamicRange: DynamicRange)(valueCollector: CurrentValueCollector): Gauge =
registerGauge(name, valueCollector, tags, Some(unitOfMeasurement), Some(dynamicRange))
def removeGauge(name: String): Boolean =
removeGauge(name, Map.empty)
def removeGauge(name: String, tags: Map[String, String]): Boolean
def registerGauge(name: String, valueCollector: CurrentValueCollector, tags: Map[String, String] = Map.empty,
unitOfMeasurement: Option[UnitOfMeasurement] = None, dynamicRange: Option[DynamicRange] = None,
refreshInterval: Option[FiniteDuration] = None): Gauge
//
// Counters registration and removal
//
def counter(name: String): Counter =
registerCounter(name)
def counter(name: String, unitOfMeasurement: UnitOfMeasurement): Counter =
registerCounter(name, unitOfMeasurement = Some(unitOfMeasurement))
def counter(name: String, tags: Map[String, String]): Counter =
registerCounter(name, tags)
def counter(name: String, tags: Map[String, String], unitOfMeasurement: UnitOfMeasurement): Counter =
registerCounter(name, tags, Some(unitOfMeasurement))
def removeCounter(name: String): Boolean =
removeCounter(name, Map.empty)
def removeCounter(name: String, tags: Map[String, String]): Boolean
def registerCounter(name: String, tags: Map[String, String] = Map.empty, unitOfMeasurement: Option[UnitOfMeasurement] = None,
dynamicRange: Option[DynamicRange] = None): Counter
//
// Entities registration and removal
//
def entity[T <: EntityRecorder](recorderFactory: EntityRecorderFactory[T], name: String): T =
entity(recorderFactory, Entity(name, recorderFactory.category))
def entity[T <: EntityRecorder](recorderFactory: EntityRecorderFactory[T], name: String, tags: Map[String, String]): T =
entity(recorderFactory, Entity(name, recorderFactory.category, tags))
def removeEntity(name: String, category: String): Boolean =
removeEntity(Entity(name, category, Map.empty))
def removeEntity(name: String, category: String, tags: Map[String, String]): Boolean =
removeEntity(Entity(name, category, tags))
def removeEntity(entity: Entity): Boolean
def entity[T <: EntityRecorder](recorderFactory: EntityRecorderFactory[T], entity: Entity): T
def find(name: String, category: String): Option[EntityRecorder] =
find(Entity(name, category))
def find(entity: Entity): Option[EntityRecorder]
def subscribe(filter: SubscriptionFilter, subscriber: ActorRef): Unit =
subscribe(filter, subscriber, permanently = false)
def subscribe(category: String, selection: String, subscriber: ActorRef, permanently: Boolean): Unit =
subscribe(SubscriptionFilter(category, selection), subscriber, permanently)
def subscribe(category: String, selection: String, subscriber: ActorRef): Unit =
subscribe(SubscriptionFilter(category, selection), subscriber, permanently = false)
def subscribe(filter: SubscriptionFilter, subscriber: ActorRef, permanently: Boolean): Unit
def unsubscribe(subscriber: ActorRef): Unit
def buildDefaultCollectionContext: CollectionContext
def instrumentFactory(category: String): InstrumentFactory
}
private[kamon] class MetricsImpl(config: Config) extends Metrics {
import kamon.util.TriemapAtomicGetOrElseUpdate.Syntax
private val _trackedEntities = TrieMap.empty[Entity, EntityRecorder]
private val _subscriptions = new LazyActorRef
val settings = MetricsSettings(config)
def shouldTrack(entity: Entity): Boolean =
settings.entityFilters.get(entity.category).map {
filter ⇒ filter.accept(entity.name)
} getOrElse (settings.trackUnmatchedEntities)
def registerHistogram(name: String, tags: Map[String, String], unitOfMeasurement: Option[UnitOfMeasurement],
dynamicRange: Option[DynamicRange]): Histogram = {
val histogramEntity = Entity(name, "histogram", tags)
val recorder = _trackedEntities.atomicGetOrElseUpdate(histogramEntity, {
val factory = instrumentFactory(histogramEntity.category)
HistogramRecorder(HistogramKey(histogramEntity.category, unitOfMeasurement.getOrElse(UnitOfMeasurement.Unknown)),
factory.createHistogram(name, dynamicRange))
}, _.cleanup)
recorder.asInstanceOf[HistogramRecorder].instrument
}
def removeHistogram(name: String, tags: Map[String, String]): Boolean =
_trackedEntities.remove(Entity(name, "histogram", tags)).isDefined
def registerMinMaxCounter(name: String, tags: Map[String, String], unitOfMeasurement: Option[UnitOfMeasurement], dynamicRange: Option[DynamicRange],
refreshInterval: Option[FiniteDuration]): MinMaxCounter = {
val minMaxCounterEntity = Entity(name, "min-max-counter", tags)
val recorder = _trackedEntities.atomicGetOrElseUpdate(minMaxCounterEntity, {
val factory = instrumentFactory(minMaxCounterEntity.category)
MinMaxCounterRecorder(MinMaxCounterKey(minMaxCounterEntity.category, unitOfMeasurement.getOrElse(UnitOfMeasurement.Unknown)),
factory.createMinMaxCounter(name, dynamicRange, refreshInterval))
}, _.cleanup)
recorder.asInstanceOf[MinMaxCounterRecorder].instrument
}
def removeMinMaxCounter(name: String, tags: Map[String, String]): Boolean =
_trackedEntities.remove(Entity(name, "min-max-counter", tags)).isDefined
def registerGauge(name: String, valueCollector: CurrentValueCollector, tags: Map[String, String] = Map.empty,
unitOfMeasurement: Option[UnitOfMeasurement] = None, dynamicRange: Option[DynamicRange] = None,
refreshInterval: Option[FiniteDuration] = None): Gauge = {
val gaugeEntity = Entity(name, "gauge", tags)
val recorder = _trackedEntities.atomicGetOrElseUpdate(gaugeEntity, {
val factory = instrumentFactory(gaugeEntity.category)
GaugeRecorder(MinMaxCounterKey(gaugeEntity.category, unitOfMeasurement.getOrElse(UnitOfMeasurement.Unknown)),
factory.createGauge(name, dynamicRange, refreshInterval, valueCollector))
}, _.cleanup)
recorder.asInstanceOf[GaugeRecorder].instrument
}
def removeGauge(name: String, tags: Map[String, String]): Boolean =
_trackedEntities.remove(Entity(name, "gauge", tags)).isDefined
def registerCounter(name: String, tags: Map[String, String] = Map.empty, unitOfMeasurement: Option[UnitOfMeasurement] = None,
dynamicRange: Option[DynamicRange] = None): Counter = {
val counterEntity = Entity(name, "counter", tags)
val recorder = _trackedEntities.atomicGetOrElseUpdate(counterEntity, {
val factory = instrumentFactory(counterEntity.category)
CounterRecorder(CounterKey(counterEntity.category, unitOfMeasurement.getOrElse(UnitOfMeasurement.Unknown)),
factory.createCounter())
}, _.cleanup)
recorder.asInstanceOf[CounterRecorder].instrument
}
def removeCounter(name: String, tags: Map[String, String]): Boolean =
_trackedEntities.remove(Entity(name, "counter", tags)).isDefined
def entity[T <: EntityRecorder](recorderFactory: EntityRecorderFactory[T], entity: Entity): T = {
_trackedEntities.atomicGetOrElseUpdate(entity, {
recorderFactory.createRecorder(instrumentFactory(recorderFactory.category))
}, _.cleanup).asInstanceOf[T]
}
def removeEntity(entity: Entity): Boolean =
_trackedEntities.remove(entity).isDefined
def find(entity: Entity): Option[EntityRecorder] =
_trackedEntities.get(entity)
def subscribe(filter: SubscriptionFilter, subscriber: ActorRef, permanent: Boolean): Unit =
_subscriptions.tell(Subscribe(filter, subscriber, permanent))
def unsubscribe(subscriber: ActorRef): Unit =
_subscriptions.tell(Unsubscribe(subscriber))
def buildDefaultCollectionContext: CollectionContext =
CollectionContext(settings.defaultCollectionContextBufferSize)
def instrumentFactory(category: String): InstrumentFactory =
settings.instrumentFactories.getOrElse(category, settings.defaultInstrumentFactory)
private[kamon] def collectSnapshots(collectionContext: CollectionContext): Map[Entity, EntitySnapshot] = {
val builder = Map.newBuilder[Entity, EntitySnapshot]
_trackedEntities.foreach {
case (identity, recorder) ⇒ builder += ((identity, recorder.collect(collectionContext)))
}
builder.result()
}
/**
* Metrics Extension initialization.
*/
private var _system: ActorSystem = null
private lazy val _start = {
_subscriptions.point(_system.actorOf(SubscriptionsDispatcher.props(settings.tickInterval, this), "metrics"))
settings.pointScheduler(DefaultRefreshScheduler(_system.scheduler, _system.dispatcher))
}
def start(system: ActorSystem): Unit = synchronized {
_system = system
_start
_system = null
}
}
private[kamon] object MetricsImpl {
def apply(config: Config) =
new MetricsImpl(config)
}
|