@@ -32,52 +32,109 @@ class LongGaugeMetric @JvmOverloads constructor(
32
32
/* * the namespace (prefix) of this metric */
33
33
namespace : String ,
34
34
/* * an optional initial value for this metric */
35
- internal val initialValue : Long = 0L
35
+ internal val initialValue : Long = 0L ,
36
+ /* * Label names for this metric. If non-empty, the initial value must be 0 and all get/update calls MUST
37
+ * specify values for the labels. Calls to simply get() or set() will fail with an exception. */
38
+ val labelNames : List <String > = emptyList()
36
39
) : Metric<Long>() {
37
- private val gauge = Gauge .build(name, help).namespace(namespace).create().apply { set(initialValue.toDouble()) }
40
+ private val gauge = run {
41
+ val builder = Gauge .build(name, help).namespace(namespace)
42
+ if (labelNames.isNotEmpty()) {
43
+ builder.labelNames(* labelNames.toTypedArray())
44
+ if (initialValue != 0L ) {
45
+ throw IllegalArgumentException (" Cannot set an initial value for a labeled gauge" )
46
+ }
47
+ }
48
+ builder.create().apply {
49
+ if (initialValue != 0L ) {
50
+ set(initialValue.toDouble())
51
+ }
52
+ }
53
+ }
38
54
55
+ /* * When we have labels [get()] throws an exception and the JSON format is not supported. */
56
+ override val supportsJson: Boolean = labelNames.isEmpty()
39
57
override fun get () = gauge.get().toLong()
58
+ fun get (labels : List <String >) = gauge.labels(* labels.toTypedArray()).get().toLong()
40
59
41
- override fun reset () = set(initialValue)
60
+ override fun reset () = synchronized(gauge) {
61
+ gauge.clear()
62
+ if (initialValue != 0L ) {
63
+ gauge.inc(initialValue.toDouble())
64
+ }
65
+ }
42
66
43
67
override fun register (registry : CollectorRegistry ) = this .also { registry.register(gauge) }
44
68
45
69
/* *
46
70
* Atomically sets the gauge to the given value.
47
71
*/
48
- fun set (newValue : Long ): Unit = synchronized(gauge) { gauge.set(newValue.toDouble()) }
72
+ fun set (newValue : Long , labels : List <String > = emptyList()): Unit = synchronized(gauge) {
73
+ if (labels.isEmpty()) {
74
+ gauge.set(newValue.toDouble())
75
+ } else {
76
+ gauge.labels(* labels.toTypedArray()).set(newValue.toDouble())
77
+ }
78
+ }
49
79
50
80
/* *
51
81
* Atomically increments the value of this gauge by one.
52
82
*/
53
- fun inc () = synchronized(gauge) { gauge.inc() }
83
+ fun inc (labels : List <String > = emptyList()) = synchronized(gauge) {
84
+ if (labels.isEmpty()) {
85
+ gauge.inc()
86
+ } else {
87
+ gauge.labels(* labels.toTypedArray()).inc()
88
+ }
89
+ }
54
90
55
91
/* *
56
92
* Atomically decrements the value of this gauge by one.
57
93
*/
58
- fun dec () = synchronized(gauge) { gauge.dec() }
94
+ fun dec (labels : List <String > = emptyList()) = synchronized(gauge) {
95
+ if (labels.isEmpty()) {
96
+ gauge.dec()
97
+ } else {
98
+ gauge.labels(* labels.toTypedArray()).dec()
99
+ }
100
+ }
59
101
60
102
/* *
61
103
* Atomically adds the given value to this gauge, returning the updated value.
62
104
*
63
105
* @return the updated value
64
106
*/
65
- fun addAndGet (delta : Long ): Long = synchronized(gauge) {
66
- gauge.inc(delta.toDouble())
67
- return gauge.get().toLong()
107
+ fun addAndGet (delta : Long , labels : List <String > = emptyList()): Long = synchronized(gauge) {
108
+ return if (labels.isEmpty()) {
109
+ gauge.inc(delta.toDouble())
110
+ gauge.get().toLong()
111
+ } else {
112
+ with (gauge.labels(* labels.toTypedArray())) {
113
+ inc(delta.toDouble())
114
+ get().toLong()
115
+ }
116
+ }
68
117
}
69
118
70
119
/* *
71
120
* Atomically increments the value of this gauge by one, returning the updated value.
72
121
*
73
122
* @return the updated value
74
123
*/
75
- fun incAndGet () = addAndGet(1 )
124
+ fun incAndGet (labels : List < String > = emptyList()) = addAndGet(1 , labels )
76
125
77
126
/* *
78
127
* Atomically decrements the value of this gauge by one, returning the updated value.
79
128
*
80
129
* @return the updated value
81
130
*/
82
- fun decAndGet () = addAndGet(- 1 )
131
+ fun decAndGet (labels : List <String > = emptyList()) = addAndGet(- 1 , labels)
132
+
133
+ /* * Remove the child with the given labels (the metric with those labels will stop being emitted) */
134
+ fun remove (labels : List <String > = emptyList()) = synchronized(gauge) {
135
+ if (labels.isNotEmpty()) {
136
+ gauge.remove(* labels.toTypedArray())
137
+ }
138
+ }
139
+ internal fun collect () = gauge.collect()
83
140
}
0 commit comments