Skip to content

Commit

Permalink
Feature: prometheus gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jan 14, 2022
1 parent 83f96c3 commit f7f28dd
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type Service struct {
counters map[string]*prometheus.CounterVec
histograms map[string]*prometheus.HistogramVec
gauge map[string]*prometheus.GaugeVec
server *http.Server
wg sync.WaitGroup
}
Expand All @@ -24,6 +25,7 @@ func NewService(cfg *config.Prometheus) *Service {
var s Service
s.counters = make(map[string]*prometheus.CounterVec)
s.histograms = make(map[string]*prometheus.HistogramVec)
s.gauge = make(map[string]*prometheus.GaugeVec)

if cfg != nil && cfg.URL != "" {
s.server = &http.Server{Addr: cfg.URL}
Expand Down Expand Up @@ -120,3 +122,46 @@ func (service *Service) AddHistogramValue(name string, labels map[string]string,
histogram.With(labels).Observe(observe)
}
}

// RegisterGauge -
func (service *Service) RegisterGauge(name, help string, labels ...string) {
vec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: name,
Help: help,
}, labels)
service.gauge[name] = vec
prometheus.MustRegister(vec)
}

// Gauge -
func (service *Service) Gauge(name string) *prometheus.GaugeVec {
gauge, ok := service.gauge[name]
if ok {
return gauge
}
return nil
}

// SetGaugeValue -
func (service *Service) SetGaugeValue(name string, labels map[string]string, observe float64) {
gauge, ok := service.gauge[name]
if ok {
gauge.With(labels).Set(observe)
}
}

// IncGaugeValue -
func (service *Service) IncGaugeValue(name string, labels map[string]string) {
gauge, ok := service.gauge[name]
if ok {
gauge.With(labels).Inc()
}
}

// DecGaugeValue -
func (service *Service) DecGaugeValue(name string, labels map[string]string) {
gauge, ok := service.gauge[name]
if ok {
gauge.With(labels).Dec()
}
}

0 comments on commit f7f28dd

Please sign in to comment.