19
19
package org .apache .pulsar .broker .stats .prometheus .metrics ;
20
20
21
21
import io .prometheus .client .Collector ;
22
+ import io .prometheus .client .CollectorRegistry ;
22
23
import io .prometheus .client .GaugeMetricFamily ;
23
24
import io .prometheus .client .SimpleCollector ;
24
25
import java .util .ArrayList ;
@@ -39,9 +40,11 @@ public Builder supplier(Supplier<Number> supplier) {
39
40
return this ;
40
41
}
41
42
42
- public ObserverGauge register () {
43
+ public ObserverGauge register (CollectorRegistry registry ) {
43
44
try {
44
- return super .register ();
45
+ ObserverGauge instance = super .register (registry );
46
+ instance .setRegistry (registry );
47
+ return instance ;
45
48
} catch (Exception e ) {
46
49
// Handle double registration errors in tests
47
50
return create ();
@@ -71,6 +74,7 @@ public static Builder build() {
71
74
}
72
75
73
76
private final Supplier <Number > supplier ;
77
+ private volatile CollectorRegistry registry ;
74
78
75
79
private ObserverGauge (Builder builder ) {
76
80
super (builder );
@@ -99,4 +103,15 @@ public List<MetricFamilySamples> collect() {
99
103
public List <MetricFamilySamples > describe () {
100
104
return Collections .singletonList (new GaugeMetricFamily (fullname , help , labelNames ));
101
105
}
106
+
107
+ protected void setRegistry (CollectorRegistry registry ) {
108
+ this .registry = registry ;
109
+ }
110
+
111
+ public void unregister () {
112
+ if (registry != null ) {
113
+ registry .unregister (this );
114
+ registry = null ;
115
+ }
116
+ }
102
117
}
0 commit comments