1
+ package kamon
2
+ package metric
3
+
4
+ import kamon .tag .TagSet
5
+
6
+ /**
7
+ * Utility class for handling groups of instruments that should be created and removed together. This becomes specially
8
+ * handy when using several instruments to track different aspects of the same component. For example, when tracking
9
+ * metrics on a thread pool you will want to request several instruments to track different aspects: the pool size, the
10
+ * number of submitted tasks, the queue size and so on, and all of those instruments should share common tags that are
11
+ * specific to the instrumented thread pool; additionally, once said thread pool is shutdown, all of those instruments
12
+ * should be removed together. This class makes it simpler to keep track of all of those related instruments and remove
13
+ * them together when necessary.
14
+ */
15
+ abstract class InstrumentGroup (val commonTags : TagSet ) {
16
+ private var _groupInstruments = List .empty[Instrument [_, _]]
17
+
18
+ /**
19
+ * Registers and returns an instrument of the provided metric with the common tags.
20
+ */
21
+ def register [Inst <: Instrument [Inst , Sett ], Sett <: Metric .Settings ](metric : Metric [Inst , Sett ]): Inst =
22
+ registerInstrument(metric, commonTags)
23
+
24
+ /**
25
+ * Registers and returns an instrument of the provided metric with the common tags and the additionally provided
26
+ * key/value pair.
27
+ */
28
+ def register [Inst <: Instrument [Inst , Sett ], Sett <: Metric .Settings ](metric : Metric [Inst , Sett ], key : String , value : String ): Inst =
29
+ registerInstrument(metric, commonTags.withTag(key, value))
30
+
31
+ /**
32
+ * Registers and returns an instrument of the provided metric with the common tags and the additionally provided
33
+ * key/value pair.
34
+ */
35
+ def register [Inst <: Instrument [Inst , Sett ], Sett <: Metric .Settings ](metric : Metric [Inst , Sett ], key : String , value : Long ): Inst =
36
+ registerInstrument(metric, commonTags.withTag(key, value))
37
+
38
+ /**
39
+ * Registers and returns an instrument of the provided metric with the common tags and the additionally provided
40
+ * key/value pair.
41
+ */
42
+ def register [Inst <: Instrument [Inst , Sett ], Sett <: Metric .Settings ](metric : Metric [Inst , Sett ], key : String , value : Boolean ): Inst =
43
+ registerInstrument(metric, commonTags.withTag(key, value))
44
+
45
+ /**
46
+ * Registers and returns an instrument of the provided metric with the common tags and the additionally provided tags.
47
+ */
48
+ def register [Inst <: Instrument [Inst , Sett ], Sett <: Metric .Settings ](metric : Metric [Inst , Sett ], extraTags : TagSet ): Inst =
49
+ registerInstrument(metric, commonTags.withTags(extraTags))
50
+
51
+
52
+ private def registerInstrument [Inst <: Instrument [Inst , Sett ], Sett <: Metric .Settings ](metric : Metric [Inst , Sett ],
53
+ tags : TagSet ): Inst = synchronized {
54
+ val instrument = metric.withTags(tags)
55
+ _groupInstruments = instrument :: _groupInstruments
56
+ instrument
57
+ }
58
+
59
+ /**
60
+ * Removes all instruments that were registered by this group.
61
+ */
62
+ def remove (): Unit = synchronized {
63
+ _groupInstruments foreach(_.remove())
64
+ }
65
+ }
0 commit comments