Skip to content

Commit 83f96c3

Browse files
Feature: prometheus histogram metric
1 parent e55722e commit 83f96c3

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

prometheus/prometheus.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import (
1313

1414
// Service -
1515
type Service struct {
16-
counters map[string]*prometheus.CounterVec
17-
server *http.Server
18-
wg sync.WaitGroup
16+
counters map[string]*prometheus.CounterVec
17+
histograms map[string]*prometheus.HistogramVec
18+
server *http.Server
19+
wg sync.WaitGroup
1920
}
2021

2122
// NewService -
2223
func NewService(cfg *config.Prometheus) *Service {
2324
var s Service
2425
s.counters = make(map[string]*prometheus.CounterVec)
26+
s.histograms = make(map[string]*prometheus.HistogramVec)
2527

2628
if cfg != nil && cfg.URL != "" {
2729
s.server = &http.Server{Addr: cfg.URL}
@@ -91,3 +93,30 @@ func (service *Service) IncrementCounter(name string, labels map[string]string)
9193
func (service *Service) RegisterGoBuildMetrics() {
9294
prometheus.MustRegister(collectors.NewBuildInfoCollector())
9395
}
96+
97+
// RegisterHistogram -
98+
func (service *Service) RegisterHistogram(name, help string, labels ...string) {
99+
vec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
100+
Name: name,
101+
Help: help,
102+
}, labels)
103+
service.histograms[name] = vec
104+
prometheus.MustRegister(vec)
105+
}
106+
107+
// Histogram -
108+
func (service *Service) Histogram(name string) *prometheus.HistogramVec {
109+
histogram, ok := service.histograms[name]
110+
if ok {
111+
return histogram
112+
}
113+
return nil
114+
}
115+
116+
// AddHistogramValue -
117+
func (service *Service) AddHistogramValue(name string, labels map[string]string, observe float64) {
118+
histogram, ok := service.histograms[name]
119+
if ok {
120+
histogram.With(labels).Observe(observe)
121+
}
122+
}

0 commit comments

Comments
 (0)