Skip to content

Commit f7f28dd

Browse files
Feature: prometheus gauge
1 parent 83f96c3 commit f7f28dd

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

prometheus/prometheus.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
type Service struct {
1616
counters map[string]*prometheus.CounterVec
1717
histograms map[string]*prometheus.HistogramVec
18+
gauge map[string]*prometheus.GaugeVec
1819
server *http.Server
1920
wg sync.WaitGroup
2021
}
@@ -24,6 +25,7 @@ func NewService(cfg *config.Prometheus) *Service {
2425
var s Service
2526
s.counters = make(map[string]*prometheus.CounterVec)
2627
s.histograms = make(map[string]*prometheus.HistogramVec)
28+
s.gauge = make(map[string]*prometheus.GaugeVec)
2729

2830
if cfg != nil && cfg.URL != "" {
2931
s.server = &http.Server{Addr: cfg.URL}
@@ -120,3 +122,46 @@ func (service *Service) AddHistogramValue(name string, labels map[string]string,
120122
histogram.With(labels).Observe(observe)
121123
}
122124
}
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

Comments
 (0)