@@ -15,6 +15,7 @@ import (
15
15
type Service struct {
16
16
counters map [string ]* prometheus.CounterVec
17
17
histograms map [string ]* prometheus.HistogramVec
18
+ gauge map [string ]* prometheus.GaugeVec
18
19
server * http.Server
19
20
wg sync.WaitGroup
20
21
}
@@ -24,6 +25,7 @@ func NewService(cfg *config.Prometheus) *Service {
24
25
var s Service
25
26
s .counters = make (map [string ]* prometheus.CounterVec )
26
27
s .histograms = make (map [string ]* prometheus.HistogramVec )
28
+ s .gauge = make (map [string ]* prometheus.GaugeVec )
27
29
28
30
if cfg != nil && cfg .URL != "" {
29
31
s .server = & http.Server {Addr : cfg .URL }
@@ -120,3 +122,46 @@ func (service *Service) AddHistogramValue(name string, labels map[string]string,
120
122
histogram .With (labels ).Observe (observe )
121
123
}
122
124
}
125
+
126
+ // RegisterGauge -
127
+ func (service * Service ) RegisterGauge (name , help string , labels ... string ) {
128
+ vec := prometheus .NewGaugeVec (prometheus.GaugeOpts {
129
+ Name : name ,
130
+ Help : help ,
131
+ }, labels )
132
+ service .gauge [name ] = vec
133
+ prometheus .MustRegister (vec )
134
+ }
135
+
136
+ // Gauge -
137
+ func (service * Service ) Gauge (name string ) * prometheus.GaugeVec {
138
+ gauge , ok := service .gauge [name ]
139
+ if ok {
140
+ return gauge
141
+ }
142
+ return nil
143
+ }
144
+
145
+ // SetGaugeValue -
146
+ func (service * Service ) SetGaugeValue (name string , labels map [string ]string , observe float64 ) {
147
+ gauge , ok := service .gauge [name ]
148
+ if ok {
149
+ gauge .With (labels ).Set (observe )
150
+ }
151
+ }
152
+
153
+ // IncGaugeValue -
154
+ func (service * Service ) IncGaugeValue (name string , labels map [string ]string ) {
155
+ gauge , ok := service .gauge [name ]
156
+ if ok {
157
+ gauge .With (labels ).Inc ()
158
+ }
159
+ }
160
+
161
+ // DecGaugeValue -
162
+ func (service * Service ) DecGaugeValue (name string , labels map [string ]string ) {
163
+ gauge , ok := service .gauge [name ]
164
+ if ok {
165
+ gauge .With (labels ).Dec ()
166
+ }
167
+ }
0 commit comments